branch = session('jjjshop_branch'); // 当前路由信息 $this->getRouteinfo(); // 验证登录状态 $this->checkLogin(); // 写入操作日志 $this->saveOptLog(); // 验证当前页面权限 $this->checkPrivilege(); } /** * 操作日志 */ private function saveOptLog(){ if(empty($this->branch) || $this->branch['user'] == null){ return; } $branch_user_id = $this->branch['user']['branch_user_id']; if(!$branch_user_id){ return; } // 如果不记录查询日志 $config = Setting::getItem('store'); if(!$config || !$config['is_get_log']){ return; } $model = new OptLogModel(); $model->save([ 'branch_user_id' => $branch_user_id, 'ip' => \request()->ip(), 'request_type' => $this->request->isGet()?'Get':'Post', 'url' => $this->routeUri, 'content' => json_encode($this->request->param(), JSON_UNESCAPED_UNICODE), 'browser' => get_client_browser(), 'agent' => $_SERVER['HTTP_USER_AGENT'], 'title' => AuthService::getAccessNameByPath($this->routeUri, $this->branch['app']['app_id']), 'app_id' => $this->branch['user']['app_id'], 'branch_id' => $this->branch['user']['branch_id'], ]); } /** * 解析当前路由参数 (分组名称、控制器名称、方法名) */ protected function getRouteinfo() { // 控制器名称 $this->controller = strtolower($this->request->controller()); $this->controller = str_replace(".","/",$this->controller); // 方法名称 $this->action = Request()->action(); // 控制器分组 (用于定义所属模块) $groupstr = strstr($this->controller, '.', true); $this->group = $groupstr !== false ? $groupstr : $this->controller; // 当前uri $this->routeUri = '/' . $this->controller . '/' . $this->action; } /** * 验证登录状态 */ private function checkLogin() { // 验证当前请求是否在白名单 if (in_array($this->routeUri, $this->allowAllAction)) { return true; } // 验证登录状态 if (!empty($this->branch) && $this->branch['is_login'] == 1) { return true; } throw new BaseException(['code' => -1, 'msg' => 'not_login']); return false; } /** * 获取分会id */ protected function getBranchId(){ return $this->branch['user']['branch_id']; } /** * 验证当前页面权限 */ private function checkPrivilege() { if(empty($this->branch) || $this->branch['user'] == null){ return; } if (!AuthService::getInstance()->checkPrivilege($this->routeUri)) { throw new BaseException(['msg' => '很抱歉,没有访问权限']); } return true; } }