From f9355e7df5e5f197833157f75edf831709725e7f Mon Sep 17 00:00:00 2001 From: luofei <604446095@qq.com> Date: Mon, 3 Jun 2024 16:14:59 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B0=83=E6=95=B4=E7=AE=A1=E7=90=86=E5=91=98?= =?UTF-8?q?=E8=AF=A6=E6=83=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/ExceptionHandler.php | 11 +++ app/store/controller/LoginController.php | 2 + app/store/controller/auth/AdminController.php | 99 +++++++++++-------- app/store/logic/auth/AdminLogic.php | 6 +- app/store/logic/auth/AuthLogic.php | 2 +- app/store/logic/auth/MenuLogic.php | 5 +- app/store/middleware/LoginMiddleware.php | 4 +- app/store/service/AdminTokenService.php | 12 +-- config/middleware.php | 2 +- 9 files changed, 85 insertions(+), 58 deletions(-) diff --git a/app/ExceptionHandler.php b/app/ExceptionHandler.php index 397f6f37..65315340 100644 --- a/app/ExceptionHandler.php +++ b/app/ExceptionHandler.php @@ -22,6 +22,17 @@ class ExceptionHandler extends Handler return \response(self::convertToHtml($exception)); } elseif ($exception instanceof ErrorException) { return response($exception->getMessage(), 401); + } elseif ($exception instanceof \Exception) { + $isDebug = config('app.debug'); + $error = [ + 'code' => $isDebug ? $exception->getCode() : 500, + 'msg' => $isDebug ? $exception->getMessage() : '服务器内部错误', + ]; + if ($isDebug) { + $error['file'] = $exception->getFile(); + $error['line'] = $exception->getLine(); + } + return response(json_encode($error, JSON_UNESCAPED_UNICODE)); } // 非json请求则返回一个页面 return new Response(200, [], $exception->getMessage()); diff --git a/app/store/controller/LoginController.php b/app/store/controller/LoginController.php index 31dab92f..151e52bb 100644 --- a/app/store/controller/LoginController.php +++ b/app/store/controller/LoginController.php @@ -56,6 +56,8 @@ class LoginController extends BaseAdminController ApiDoc\Title('退出登录'), ApiDoc\url('/store/login/logout'), ApiDoc\Method('POST'), + ApiDoc\NotHeaders(), + ApiDoc\Header(name: "token", type: "string", require: true, desc: "token"), ApiDoc\ResponseSuccess("data", type: "array"), ] public function logout() diff --git a/app/store/controller/auth/AdminController.php b/app/store/controller/auth/AdminController.php index 571999a0..50c75f33 100644 --- a/app/store/controller/auth/AdminController.php +++ b/app/store/controller/auth/AdminController.php @@ -26,26 +26,31 @@ use hg\apidoc\annotation as ApiDoc; * Class AdminController * @package app\store\controller\auth */ -#[ApiDoc\NotParse()] +#[ApiDoc\title('管理员信息')] class AdminController extends BaseAdminController { - /** - * @notes 查看管理员列表 - * @author 乔峰 - * @date 2021/12/29 9:55 - */ + #[ + ApiDoc\Title("查看管理员列表"), + ApiDoc\url('/store/auth/admin/lists'), + ApiDoc\Method('GET'), + ApiDoc\NotHeaders(), + ApiDoc\Header(name: "token", type: "string", require: true, desc: "token"), + ApiDoc\ResponseSuccess("data", type: "array"), + ] public function lists() { return $this->dataLists(new AdminLists()); } - - /** - * @notes 添加管理员 - * @author 乔峰 - * @date 2021/12/29 10:21 - */ + #[ + ApiDoc\Title("添加管理员"), + ApiDoc\url('/store/auth/admin/add'), + ApiDoc\Method('GET'), + ApiDoc\NotHeaders(), + ApiDoc\Header(name: "token", type: "string", require: true, desc: "token"), + ApiDoc\ResponseSuccess("data", type: "array"), + ] public function add() { $params = (new AdminValidate())->post()->goCheck('add'); @@ -56,12 +61,14 @@ class AdminController extends BaseAdminController return $this->fail(AdminLogic::getError()); } - - /** - * @notes 编辑管理员 - * @author 乔峰 - * @date 2021/12/29 11:03 - */ + #[ + ApiDoc\Title("编辑管理员"), + ApiDoc\url('/store/auth/admin/edit'), + ApiDoc\Method('GET'), + ApiDoc\NotHeaders(), + ApiDoc\Header(name: "token", type: "string", require: true, desc: "token"), + ApiDoc\ResponseSuccess("data", type: "array"), + ] public function edit() { $params = (new AdminValidate())->post()->goCheck('edit'); @@ -72,12 +79,14 @@ class AdminController extends BaseAdminController return $this->fail(AdminLogic::getError()); } - - /** - * @notes 删除管理员 - * @author 乔峰 - * @date 2021/12/29 11:03 - */ + #[ + ApiDoc\Title("删除管理员"), + ApiDoc\url('/store/auth/admin/delete'), + ApiDoc\Method('GET'), + ApiDoc\NotHeaders(), + ApiDoc\Header(name: "token", type: "string", require: true, desc: "token"), + ApiDoc\ResponseSuccess("data", type: "array"), + ] public function delete() { $params = (new AdminValidate())->post()->goCheck('delete'); @@ -88,12 +97,14 @@ class AdminController extends BaseAdminController return $this->fail(AdminLogic::getError()); } - - /** - * @notes 查看管理员详情 - * @author 乔峰 - * @date 2021/12/29 11:07 - */ + #[ + ApiDoc\Title("查看管理员详情"), + ApiDoc\url('/store/auth/admin/detail'), + ApiDoc\Method('GET'), + ApiDoc\NotHeaders(), + ApiDoc\Header(name: "token", type: "string", require: true, desc: "token"), + ApiDoc\ResponseSuccess("data", type: "array"), + ] public function detail() { $params = (new AdminValidate())->goCheck('detail'); @@ -101,24 +112,28 @@ class AdminController extends BaseAdminController return $this->data($result); } - - /** - * @notes 获取当前管理员信息 - * @author 乔峰 - * @date 2021/12/31 10:53 - */ + #[ + ApiDoc\Title("获取当前管理员信息"), + ApiDoc\url('/store/auth/admin/mySelf'), + ApiDoc\Method('GET'), + ApiDoc\NotHeaders(), + ApiDoc\Header(name: "token", type: "string", require: true, desc: "token"), + ApiDoc\ResponseSuccess("data", type: "array"), + ] public function mySelf() { $result = AdminLogic::detail(['id' => $this->adminId], 'auth'); return $this->data($result); } - - /** - * @notes 编辑超级管理员信息 - * @author 乔峰 - * @date 2022/4/8 17:54 - */ + #[ + ApiDoc\Title("编辑超级管理员信息"), + ApiDoc\url('/store/auth/admin/editSelf'), + ApiDoc\Method('GET'), + ApiDoc\NotHeaders(), + ApiDoc\Header(name: "token", type: "string", require: true, desc: "token"), + ApiDoc\ResponseSuccess("data", type: "array"), + ] public function editSelf() { $params = (new editSelfValidate())->post()->goCheck('', ['admin_id' => $this->adminId]); diff --git a/app/store/logic/auth/AdminLogic.php b/app/store/logic/auth/AdminLogic.php index 174fd8c8..2f3d081c 100644 --- a/app/store/logic/auth/AdminLogic.php +++ b/app/store/logic/auth/AdminLogic.php @@ -23,6 +23,7 @@ use app\common\model\auth\AdminJobs; use app\common\model\auth\AdminRole; use app\common\model\auth\AdminSession; use app\common\cache\AdminTokenCache; +use app\common\model\system_store\SystemStoreStaff; use app\common\service\FileService; use Webman\Config; use think\facade\Db; @@ -223,9 +224,8 @@ class AdminLogic extends BaseLogic */ public static function detail($params, $action = 'detail'): array { - $admin = Admin::field([ - 'id', 'account', 'name', 'disable', 'root', - 'multipoint_login', 'avatar', + $admin = SystemStoreStaff::field([ + 'id', 'account', 'staff_name', 'avatar', 'is_admin', 'is_manager' ])->findOrEmpty($params['id'])->toArray(); if ($action == 'detail') { diff --git a/app/store/logic/auth/AuthLogic.php b/app/store/logic/auth/AuthLogic.php index 602d5071..61aab460 100644 --- a/app/store/logic/auth/AuthLogic.php +++ b/app/store/logic/auth/AuthLogic.php @@ -54,7 +54,7 @@ class AuthLogic */ public static function getBtnAuthByRoleId($admin) { - if ($admin['root']) { + if ($admin['is_admin'] || $admin['is_manager']) { return ['*']; } diff --git a/app/store/logic/auth/MenuLogic.php b/app/store/logic/auth/MenuLogic.php index b18c7e10..e9c09816 100644 --- a/app/store/logic/auth/MenuLogic.php +++ b/app/store/logic/auth/MenuLogic.php @@ -20,6 +20,7 @@ use app\common\logic\BaseLogic; use app\common\model\auth\Admin; use app\common\model\auth\SystemMenu; use app\common\model\auth\SystemRoleMenu; +use app\common\model\system_store\SystemStoreStaff; /** @@ -43,13 +44,13 @@ class MenuLogic extends BaseLogic */ public static function getMenuByAdminId($adminId) { - $admin = Admin::findOrEmpty($adminId); + $admin = SystemStoreStaff::findOrEmpty($adminId); $where = []; $where[] = ['type', 'in', ['M', 'C']]; $where[] = ['is_disable', '=', 0]; - if ($admin['root'] != 1) { + if ($admin['is_admin'] != 1 || $admin['is_manager'] != 1) { $roleMenu = SystemRoleMenu::whereIn('role_id', $admin['role_id'])->column('menu_id'); $where[] = ['id', 'in', $roleMenu]; } diff --git a/app/store/middleware/LoginMiddleware.php b/app/store/middleware/LoginMiddleware.php index 95998a2a..6ebff66c 100644 --- a/app/store/middleware/LoginMiddleware.php +++ b/app/store/middleware/LoginMiddleware.php @@ -4,6 +4,7 @@ namespace app\store\middleware; +use app\common\cache\StaffTokenCache; use app\store\service\AdminTokenService; use app\common\cache\AdminTokenCache; use app\common\service\JsonService; @@ -35,7 +36,7 @@ class LoginMiddleware implements MiddlewareInterface return JsonService::fail('请求参数缺token', [], 0, 0); } - $adminInfo = (new AdminTokenCache())->getAdminInfo($token); + $adminInfo = (new StaffTokenCache())->getAdminInfo($token); if (empty($adminInfo) && !$isNotNeedLogin) { //token过期无效并且该地址需要登录才能访问 return JsonService::fail('登录超时,请重新登录', [], -1); @@ -58,7 +59,6 @@ class LoginMiddleware implements MiddlewareInterface //给request赋值,用于控制器 $request->adminInfo = $adminInfo; $request->adminId = $adminInfo['admin_id'] ?? 0; - $request->supplierId = $adminInfo['supplier_id'] ?? 0; return $handler($request); } diff --git a/app/store/service/AdminTokenService.php b/app/store/service/AdminTokenService.php index 94f2b18c..96fb996f 100644 --- a/app/store/service/AdminTokenService.php +++ b/app/store/service/AdminTokenService.php @@ -4,9 +4,7 @@ namespace app\store\service; -use app\common\cache\AdminTokenCache; use app\common\cache\StaffTokenCache; -use app\common\model\auth\AdminSession; use app\common\model\system_store\SystemStoreStaffSession; use Webman\Config; @@ -32,13 +30,13 @@ class AdminTokenService //获取token延长过期的时间 $expireTime = $time + Config::get('project.admin_token.expire_duration'); - $adminTokenCache = new StaffTokenCache(); + $staffTokenCache = new StaffTokenCache(); //token处理 if ($adminSession) { if ($adminSession->expire_time < $time || $multipointLogin === 0) { //清空缓存 - $adminTokenCache->deleteAdminInfo($adminSession->token); + $staffTokenCache->deleteAdminInfo($adminSession->token); //如果token过期或账号设置不支持多处登录,更新token $adminSession->token = create_token($adminId); } @@ -56,7 +54,7 @@ class AdminTokenService ]); } - return $adminTokenCache->setAdminInfo($adminSession->token); + return $staffTokenCache->setAdminInfo($adminSession->token); } /** @@ -72,7 +70,7 @@ class AdminTokenService public static function overtimeToken($token) { $time = time(); - $adminSession = AdminSession::where('token', '=', $token)->findOrEmpty(); + $adminSession = SystemStoreStaffSession::where('token', '=', $token)->findOrEmpty(); if ($adminSession->isEmpty()) { return false; } @@ -80,7 +78,7 @@ class AdminTokenService $adminSession->expire_time = $time + Config::get('project.admin_token.expire_duration'); $adminSession->update_time = $time; $adminSession->save(); - return (new AdminTokenCache())->setAdminInfo($adminSession->token); + return (new StaffTokenCache())->setAdminInfo($adminSession->token); } /** diff --git a/config/middleware.php b/config/middleware.php index 5af3bd96..6bc9bfc9 100644 --- a/config/middleware.php +++ b/config/middleware.php @@ -37,6 +37,6 @@ return [ app\common\http\middleware\AdminAllowMiddleware::class, app\store\middleware\InitMiddleware::class, app\store\middleware\LoginMiddleware::class, - app\store\middleware\AuthMiddleware::class, +// app\store\middleware\AuthMiddleware::class, ] ];