adminInfo = Session::get("adminInfo"); $this->adminId = Session::get("adminId"); $this->module = App::getInstance()->http->getName(); $this->controller = un_camelize($this->request->controller()); $this->action = $this->request->action(); $this->auth = explode(",", AdminRole::getAuth($this->adminInfo['role_id'] ?? 0)); $this->nowAuthId = AdminAuth::getAuthId($this->module,$this->controller,$this->action); $this->model = $this->buildModel($this->module,$this->request->controller()); // 鉴权 $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('adminId') && Session::has('adminInfo'); } /** * 记录日志 * @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路径 * @param string $module * @param string $controller * @return object|\think\App|null */ protected function buildModel(string $module, string $controller) { $path = explode(".", $this->request->controller()); $modelPath = "app\\{$this->module}\\model"; foreach ($path as $v) $modelPath .= "\\".$v; if (class_exists($modelPath)) return app($modelPath); return null; } }