adminInfo = Session::get(Data::SESSION_KEY_ADMIN_INFO); $this->adminId = Session::get(Data::SESSION_KEY_ADMIN_ID); $this->module = App::getInstance()->http->getName(); $this->controller = un_camelize($this->request->controller()); $this->action = $this->request->action(); $this->auth = explode(",", $this->adminInfo['role_auth'] ?? ''); $this->nowAuthId = AdminAuth::getAuthId($this->module, $this->controller, $this->action); $this->model = $this->buildModel(); // 鉴权 $this->checkAuth(); // 多语言 $this->loadLang(); // 日志 $this->createLog(); } /** * 检验权限 */ protected function checkAuth() { // 不需要登录 if (in_array($this->action, $this->noNeedLogin) || $this->noNeedLogin == ['*'] || $this->noNeedLogin == "*") return true; // 验证登录 if (!self::isActive()) exit($this->failedNotice(lang("未登录"), "/admin/login/login")); // 权限验证 if ($this->nowAuthId == -1 || in_array($this->nowAuthId, $this->auth)) return true; exit($this->failed(lang('没有权限访问!'))); } /** * 加载语言文件 */ protected function loadLang() { Lang::load(App::getRootPath() . 'app/' . $this->module . '/lang/' . Lang::getLangSet() . '/' . $this->controller . '.php'); } /** * 验证登录 * @return bool */ protected static function isActive() { return Session::has(Data::SESSION_KEY_ADMIN_ID) && Session::has(Data::SESSION_KEY_ADMIN_INFO); } /** * 记录日志 * @return bool */ protected function createLog() { // 不需要登录不能记录日志 if (in_array($this->action, $this->noNeedLogin) || $this->noNeedLogin == ['*'] || $this->noNeedLogin == "*") return true; // 无需记录日志 if (in_array($this->action, $this->noNeedLog) || $this->noNeedLog == ['*'] || $this->noNeedLog == "*") return true; // 有操作权限,记录日志 if (in_array($this->nowAuthId, $this->auth)) event("AdminLog", [$this->adminInfo, $this->module, $this->controller, $this->action]); return false; } /** * 生成model路径 * @return object|\think\App|null */ protected function buildModel() { $path = explode(".", $this->request->controller()); $modelPath = "app\\common\\model"; //全部为common foreach ($path as $v) $modelPath .= "\\" . $v; if (class_exists($modelPath)) return app($modelPath); return null; } }