From b828d486726c597680324a2dda24acbac99c03f2 Mon Sep 17 00:00:00 2001 From: ctexthuang Date: Sun, 27 Oct 2024 23:45:20 +0800 Subject: [PATCH] feat: menu --- app/Cache/Redis/Admin/AdminRedisKey.php | 29 ++++++++ app/Cache/Redis/Admin/MenuCache.php | 81 ++++++++++++++++++++++ app/Constants/Admin/AuthCode.php | 26 +++++++ app/Controller/Admin/AuthController.php | 5 +- app/Model/AdminMenu.php | 9 +++ app/Request/Admin/AuthRequest.php | 5 +- app/Service/Admin/User/RoleMenuService.php | 70 +++++++++++++++++-- app/Service/Admin/User/RoleService.php | 5 +- sync/http/admin/auth.http | 6 ++ 9 files changed, 226 insertions(+), 10 deletions(-) create mode 100644 app/Cache/Redis/Admin/MenuCache.php create mode 100644 app/Constants/Admin/AuthCode.php diff --git a/app/Cache/Redis/Admin/AdminRedisKey.php b/app/Cache/Redis/Admin/AdminRedisKey.php index c2338f9..d6b9027 100644 --- a/app/Cache/Redis/Admin/AdminRedisKey.php +++ b/app/Cache/Redis/Admin/AdminRedisKey.php @@ -13,4 +13,33 @@ class AdminRedisKey { return 'admin:token:user:'.$userId; } + + /** + * 权限集合 + * @return string + */ + public static function adminMenuList(): string + { + return '__system:admin:menu:list:role_id:all'; + } + + /** + * 角色集合 menu id + * @param int $roleId + * @return string + */ + public static function adminMenuArrByRoleId(int $roleId): string + { + return '__system:admin:menu:arr:role_id:'.$roleId; + } + + /** + * 角色权限集合 + * @param $roleId + * @return string + */ + public static function adminMenuListByRoleId($roleId): string + { + return '__system:admin:menu:list:role_id:'.$roleId; + } } \ No newline at end of file diff --git a/app/Cache/Redis/Admin/MenuCache.php b/app/Cache/Redis/Admin/MenuCache.php new file mode 100644 index 0000000..69e5652 --- /dev/null +++ b/app/Cache/Redis/Admin/MenuCache.php @@ -0,0 +1,81 @@ +redis->exists($key,'system')) { + return json_decode($this->redis->get($key,'system'),true); + } + + $allMenuList = $this->adminMenuModel->getAllMenu(); + + $data = $this->getDbMenu($allMenuList); + + $this->redis->set($key,json_encode($data)); + + return $data; + } + + /** + * 递归生成合适的数据 + * @param array $allMenuList + * @param int $parentId + * @return array + */ + private function getDbMenu(array $allMenuList, int $parentId = 0): array + { + $menuList = []; + foreach ($allMenuList as $menu) { + if ($menu['parent_id'] == $parentId) { + $children = $this->getDbMenu($allMenuList, (int)$menu['id']); + if (!empty($children)) { + //获取第一个 type 如何是菜单 + if ($children[0]['type'] == AuthCode::MENU_TYPE_LIST) { + $menu['children'] = $children; + } else { + foreach ($children as $child) { + $menu['permissionList'][] = [ + 'id' => $child['id'], + 'label' => $child['title'], + 'value' => $child['value'], + ]; + } + } + } + $menuList[] = $menu; + } + } + return $menuList; + } +} \ No newline at end of file diff --git a/app/Constants/Admin/AuthCode.php b/app/Constants/Admin/AuthCode.php new file mode 100644 index 0000000..a09be8f --- /dev/null +++ b/app/Constants/Admin/AuthCode.php @@ -0,0 +1,26 @@ +del(); } + #[RequestMapping(path: "menu_list", methods: "GET")] public function menu_list() { return (new RoleMenuService)->handle(); } - public function mean() + #[RequestMapping(path: "menu", methods: "GET")] + #[Scene(scene: "menu_info")] + public function menu(AuthRequest $request) { return (new RoleMenuService)->details(); } diff --git a/app/Model/AdminMenu.php b/app/Model/AdminMenu.php index a2a26a5..6c9229c 100644 --- a/app/Model/AdminMenu.php +++ b/app/Model/AdminMenu.php @@ -4,6 +4,7 @@ declare(strict_types=1); namespace App\Model; +use App\Constants\Admin\AuthCode; use Hyperf\DbConnection\Model\Model; /** @@ -42,4 +43,12 @@ class AdminMenu extends Model const UPDATED_AT = 'update_time'; + /** + * 获取所有 + * @return array + */ + public function getAllMenu(): array + { + return $this->where('status', AuthCode::MENU_STATUS_ENABLE)->orderBy('sort', 'desc')->get()->toArray(); + } } diff --git a/app/Request/Admin/AuthRequest.php b/app/Request/Admin/AuthRequest.php index 94598ce..f638cd7 100644 --- a/app/Request/Admin/AuthRequest.php +++ b/app/Request/Admin/AuthRequest.php @@ -23,17 +23,20 @@ class AuthRequest extends FormRequest { return [ 'role_id' => 'required|integer', + 'menu_id' => 'required|integer', ]; } public function messages(): array { return [ - '' + 'role_id.required' => 'id必填', + 'menu_id.required' => 'id必填', ]; } protected array $scenes = [ 'role_info' => 'role_id', + 'menu_info' => 'menu_id', ]; } diff --git a/app/Service/Admin/User/RoleMenuService.php b/app/Service/Admin/User/RoleMenuService.php index 0ee694c..0b1bb00 100644 --- a/app/Service/Admin/User/RoleMenuService.php +++ b/app/Service/Admin/User/RoleMenuService.php @@ -10,13 +10,43 @@ declare(strict_types=1); namespace App\Service\Admin\User; +use App\Cache\Redis\Admin\MenuCache; +use App\Constants\Admin\AuthCode; +use App\Exception\AdminException; +use App\Model\AdminMenu; use App\Service\Admin\BaseService; +use Hyperf\Di\Annotation\Inject; +use Psr\Container\ContainerExceptionInterface; +use Psr\Container\NotFoundExceptionInterface; +use RedisException; class RoleMenuService extends BaseService { - public function handle() + /** + * 注入缓存 + * @var MenuCache $menuCache + */ + #[Inject] + protected MenuCache $menuCache; + + /** + * @var AdminMenu $adminMenuModel + */ + #[Inject] + protected AdminMenu $adminMenuModel; + + /** + * 列表 + * @return array + * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface + * @throws RedisException + */ + public function handle(): array { - return $this->return->success(); + $data = $this->menuCache->getMenu(); + + return $this->return->success('success', ['list' => $data]); } public function add() @@ -34,8 +64,40 @@ class RoleMenuService extends BaseService return $this->return->success(); } - public function details() + /** + * 详情 + * @return array + */ + public function details(): array { - return $this->return->success(); + $menuId = $this->request->input('menu_id'); + $res = $this->adminMenuModel->where('id',$menuId)->first(); + if (!$res) throw new AdminException('角色不存在'); + $res = $res->toArray(); + + //闭包函数获取子集 + $res = function() use($res,$menuId) { + $children = $this->adminMenuModel->where('parent_id',$menuId)->select(['type','value','title','id'])->get(); + $res['permissionList'] = []; + if (!empty($children)) { + $children = $children->toArray(); + if ($children[0]['type'] == AuthCode::MENU_TYPE_LIST) { + return $res; + } + + foreach ($children as $one) + { + $res['permissionList'][] = [ + 'id' => $one['id'], + 'label' => $one['title'], + 'value' => $one['value'], + ]; + } + } + + return $res; + }; + + return $this->return->success('success',['info' => $res]); } } \ No newline at end of file diff --git a/app/Service/Admin/User/RoleService.php b/app/Service/Admin/User/RoleService.php index cd4bb98..44de9a9 100644 --- a/app/Service/Admin/User/RoleService.php +++ b/app/Service/Admin/User/RoleService.php @@ -11,7 +11,6 @@ declare(strict_types=1); namespace App\Service\Admin\User; use App\Exception\AdminException; -use App\Lib\Log; use App\Model\AdminRole; use App\Service\Admin\BaseService; use Exception; @@ -133,8 +132,6 @@ class RoleService extends BaseService $res = $this->adminRoleModel->where('id',$roleId)->first($this->field); if (!$res) throw new AdminException('角色不存在'); - return $this->return->success('success',[ - 'info' => $res->toArray() - ]); + return $this->return->success('success',['info' => $res->toArray()]); } } \ No newline at end of file diff --git a/sync/http/admin/auth.http b/sync/http/admin/auth.http index 073e751..39ac6bd 100644 --- a/sync/http/admin/auth.http +++ b/sync/http/admin/auth.http @@ -8,6 +8,12 @@ account=13632877014&password=123456 client.global.set("admin_token", response.body.data.token); %} +### 权限单列表 +GET {{host}}/admin/auth/menu_list +Content-Type: application/json +Authorization: Bearer {{admin_token}} + + ### 角色详情 GET {{host}}/admin/auth/role?role_id=1 Content-Type: application/json