model = new AdminModel; parent::_initialize(); $auth = $this->auth; //监听注册登录退出的事件 Hook::add('admin_login_successed', function ($user) use ($auth) { $expire = input('post.keeplogin') ? 30 * 86400 : 0; Cookie::set('adminid', $user->id, $expire); Cookie::set('admintoken', $auth->getToken(), $expire); }); Hook::add('admin_register_successed', function ($user) use ($auth) { Cookie::set('adminid', $user->id); Cookie::set('admintoken', $auth->getToken()); }); Hook::add('admin_delete_successed', function ($user) use ($auth) { Cookie::delete('adminid'); Cookie::delete('admintoken'); }); Hook::add('admin_logout_successed', function ($user) use ($auth) { Cookie::delete('adminid'); Cookie::delete('admintoken'); }); } /** * 管理员登录 * * @ApiMethod (POST) * @ApiParams (name="account", type="string", required=true, description="账号") * @ApiParams (name="password", type="string", required=true, description="密码") */ public function login() { $account = $this->request->post('account'); $password = $this->request->post('password'); if (!$account || !$password) { $this->error(__('Invalid parameters')); } $ret = $this->auth->login($account, $password); if ($ret) { $data = ['userinfo' => $this->auth->getUserinfo()]; $this->success(__('Logged in successful'), $data); } else { $this->error($this->auth->getError()); } } /** * 退出登录 * @ApiMethod (POST) */ public function logout() { // //设置contenttype不为表单格式防止触发宝塔防火墙 // header('Content-Type:application/json; charset=utf-8'); if (!$this->request->isPost()) { $this->error(__('Invalid parameters')); } // $password = $this->request->post('datatime'); $this->auth->logout(); $this->success(__('Logout successful')); } /** * 管理员菜单 * * @ApiMethod (GET) * @ApiParams (name="is_tree", type="string", required=true, description="是否是树形结构") */ public function menu() { $admin_id = $this->auth->id; $is_tree = $this->request->get('is_tree'); $menulist = (new AuthRule)->getMenulist($admin_id,["ismenu"=>1],$is_tree); $this->success('查询成功', $menulist); } /** * 权限校验(接口校验版-用于前端自行显示隐藏) * 返回null为无权限 * @ApiMethod (GET) * @ApiParams (name="auth_name", type="string", required=true, description="请求路径或权限标识") */ public function check_auth() { $admin_id = $this->auth->id; $auth_name = $this->request->get('auth_name',"") ?: "***"; $check = (new AuthRule)->authCheck($admin_id,$auth_name); $this->success('权限校验结果返回', $check); } /** * 权限校验(直接返回拥有的所有权限,前端自行比对判断) * * @ApiMethod (GET) */ public function have_auth() { $admin_id = $this->auth->id; $check = (new AuthRule)->getAllRules($admin_id); $this->success('权限列表返回', $check); } }