From 6856a94cd68bca8c9bedb1f523b12a0fa6376c97 Mon Sep 17 00:00:00 2001 From: zijing <903204312@qq.com> Date: Tue, 15 Jul 2025 20:23:52 +0800 Subject: [PATCH] =?UTF-8?q?=E9=87=8D=E6=9E=84=E6=9D=83=E9=99=90=E7=AE=A1?= =?UTF-8?q?=E7=90=86=E9=80=BB=E8=BE=91=EF=BC=8C=E4=BD=BF=E7=94=A8SystemMen?= =?UTF-8?q?uService=E8=8E=B7=E5=8F=96=E7=94=A8=E6=88=B7=E6=9D=83=E9=99=90?= =?UTF-8?q?=E8=8F=9C=E5=8D=95=EF=BC=8C=E4=BC=98=E5=8C=96=E8=8F=9C=E5=8D=95?= =?UTF-8?q?=E6=A0=91=E6=9E=84=E5=BB=BA=E6=96=B9=E5=BC=8F=EF=BC=8C=E7=A7=BB?= =?UTF-8?q?=E9=99=A4=E5=86=97=E4=BD=99=E4=BB=A3=E7=A0=81=EF=BC=8C=E6=9B=B4?= =?UTF-8?q?=E6=96=B0=E7=9B=B8=E5=85=B3=E6=8E=A7=E5=88=B6=E5=99=A8=E4=BB=A5?= =?UTF-8?q?=E6=94=AF=E6=8C=81=E6=96=B0=E9=80=BB=E8=BE=91=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Http/Controllers/Admin/AuthController.php | 81 +------- .../Admin/System/SystemMenuController.php | 13 +- app/Services/System/SystemMenuService.php | 193 +++++++++++++++++- 3 files changed, 203 insertions(+), 84 deletions(-) diff --git a/app/Http/Controllers/Admin/AuthController.php b/app/Http/Controllers/Admin/AuthController.php index ceb164e..559eab4 100644 --- a/app/Http/Controllers/Admin/AuthController.php +++ b/app/Http/Controllers/Admin/AuthController.php @@ -327,47 +327,14 @@ class AuthController extends BaseController { $user = $request->user(); - // 获取用户角色 - $userRoles = $user->roles()->where('status', 0)->get(); - $roleCodes = $userRoles->pluck('code')->toArray(); + // 使用SystemMenuService获取权限菜单 + $menuService = app(\App\Services\System\SystemMenuService::class); - // 判断是否为超级管理员(username为admin或角色编码包含admin相关) - $isSuperAdmin = $user->username === 'admin' || - in_array('admin', $roleCodes) || - in_array('super_admin', $roleCodes); + // 获取菜单树(使用驼峰命名) + $menuTree = $menuService->getMenuTreeByPermission($user, true); - // 获取菜单权限 - if ($isSuperAdmin) { - // 超级管理员获取所有菜单 - $menus = \App\Models\System\SystemMenu::where('status', 0) - ->where('visible', 1) - ->whereIn('type', [1, 2]) - ->orderBy('sort', 'asc') - ->orderBy('id', 'asc') - ->get(); - } else { - // 普通用户根据角色权限获取菜单 - $roleIds = $userRoles->pluck('id')->toArray(); - if (empty($roleIds)) { - $menus = collect([]); - } else { - $menuIds = \App\Models\System\SystemRoleMenu::whereIn('role_id', $roleIds) - ->pluck('menu_id') - ->unique() - ->toArray(); - - $menus = \App\Models\System\SystemMenu::whereIn('id', $menuIds) - ->where('status', 0) - ->where('visible', 1) - ->whereIn('type', [1, 2]) - ->orderBy('sort', 'asc') - ->orderBy('id', 'asc') - ->get(); - } - } - - // 构建菜单树形结构,使用表字段名称而不是驼峰命名 - $menuTree = $this->buildMenuTree($menus->toArray()); + // 获取用户角色编码 + $roleCodes = $user->roles()->where('status', 0)->pluck('code')->toArray(); return $this->SuccessObject([ 'menus' => $menuTree, @@ -382,41 +349,5 @@ class AuthController extends BaseController ]); } - /** - * 构建菜单树形结构 - * - * @param array $menus - * @param int $parentId - * @return array - */ - private function buildMenuTree(array $menus, int $parentId = 0): array - { - $tree = []; - foreach ($menus as $menu) { - if ($menu['parent_id'] == $parentId) { - // 使用表字段名称,不使用驼峰命名 - $menuItem = [ - 'id' => $menu['id'], - 'parentId' => $menu['parent_id'], - 'name' => $menu['name'], - 'component' => $menu['component'], - 'componentName' => $menu['component_name'], - 'icon' => $menu['icon'], - 'keepAlive' => $menu['keep_alive'], - 'visible' => $menu['visible'], - 'alwaysShow' => $menu['always_show'], - 'path' => $menu['path'], - ]; - - // 递归获取子菜单 - $children = $this->buildMenuTree($menus, $menu['id']); - $menuItem['children'] = $children; - - $tree[] = $menuItem; - } - } - - return $tree; - } } diff --git a/app/Http/Controllers/Admin/System/SystemMenuController.php b/app/Http/Controllers/Admin/System/SystemMenuController.php index 8b6eec5..a6a9763 100644 --- a/app/Http/Controllers/Admin/System/SystemMenuController.php +++ b/app/Http/Controllers/Admin/System/SystemMenuController.php @@ -19,12 +19,12 @@ class SystemMenuController extends BaseController /** * 获取系统菜单列表 */ - public function list(SystemMenuRequest $request): JsonResponse + public function list(): JsonResponse { - $params = $request->validated(); - $result = $this->systemMenuService->getList($params); + $user = request()->user(); + $result = $this->systemMenuService->getMenuTreeByPermission($user, true, false); - return $this->SuccessPage($result->items(), $result->total()); + return $this->Success($result); } /** @@ -41,7 +41,8 @@ class SystemMenuController extends BaseController */ public function tree(): JsonResponse { - $result = $this->systemMenuService->getMenuTree(); + $user = request()->user(); + $result = $this->systemMenuService->getMenuTreeByPermission($user, true); return $this->Success($result); } @@ -103,4 +104,4 @@ class SystemMenuController extends BaseController $this->systemMenuService->batchDelete($params['ids']); return $this->Success(); } -} \ No newline at end of file +} diff --git a/app/Services/System/SystemMenuService.php b/app/Services/System/SystemMenuService.php index 91bf6fd..2633c2b 100644 --- a/app/Services/System/SystemMenuService.php +++ b/app/Services/System/SystemMenuService.php @@ -73,7 +73,7 @@ class SystemMenuService extends BaseService */ public function getMenuTree(): array { - $menus = SystemMenu::where('status', 1) + $menus = SystemMenu::where('status', 0) ->orderBy('sort', 'asc') ->get() ->toArray(); @@ -81,13 +81,200 @@ class SystemMenuService extends BaseService return $this->buildTree($menus); } + /** + * 根据用户权限获取菜单树 + * + * @param \App\Models\User $user 用户对象 + * @param bool $convertToCamelCase 是否转换为驼峰命名,默认true + * @return array + */ + public function getMenuTreeByPermission(\App\Models\User $user, bool $convertToCamelCase = true, $isTree = true): array + { + // 获取用户角色 + $userRoles = $user->roles()->where('status', 0)->get(); + $roleCodes = $userRoles->pluck('code')->toArray(); + + // 判断是否为超级管理员(username为admin或角色编码包含admin相关) + $isSuperAdmin = $user->username === 'admin' || + in_array('admin', $roleCodes) || + in_array('super_admin', $roleCodes); + + // 获取菜单权限 + if ($isSuperAdmin) { + // 超级管理员获取所有菜单 + $menus = SystemMenu::where('status', 0) + ->where('visible', 1) + ->whereIn('type', [1, 2]) + ->orderBy('sort', 'asc') + ->orderBy('id', 'asc') + ->get(); + } else { + // 普通用户根据角色权限获取菜单 + $roleIds = $userRoles->pluck('id')->toArray(); + if (empty($roleIds)) { + $menus = collect([]); + } else { + $menuIds = \App\Models\System\SystemRoleMenu::whereIn('role_id', $roleIds) + ->pluck('menu_id') + ->unique() + ->toArray(); + + $menus = SystemMenu::whereIn('id', $menuIds) + ->where('status', 0) + ->where('visible', 1) + ->whereIn('type', [1, 2]) + ->orderBy('sort', 'asc') + ->orderBy('id', 'asc') + ->get(); + } + } + if($isTree){ + // 构建菜单树形结构 + return $this->buildPermissionMenuTree($menus->toArray(), 0, $convertToCamelCase); + }else{ + $menuItem = []; + foreach($menus as $menu){ + if ($convertToCamelCase) { + $menuItem[] = [ + 'id' => $menu['id'], + 'parentId' => $menu['parent_id'], + 'name' => $menu['name'], + 'component' => $menu['component'], + 'componentName' => $menu['component_name'], + 'icon' => $menu['icon'], + 'keepAlive' => $menu['keep_alive'], + 'visible' => $menu['visible'], + 'alwaysShow' => $menu['always_show'], + 'path' => $menu['path'], + ]; + } else { + // 使用表字段命名 + $menuItem[] = [ + 'id' => $menu['id'], + 'parent_id' => $menu['parent_id'], + 'name' => $menu['name'], + 'component' => $menu['component'], + 'component_name' => $menu['component_name'], + 'icon' => $menu['icon'], + 'keep_alive' => $menu['keep_alive'], + 'visible' => $menu['visible'], + 'always_show' => $menu['always_show'], + 'path' => $menu['path'], + ]; + } + } + return $menuItem; + } + + } + + /** + * 构建权限菜单树形结构 + * + * @param array $menus 菜单数组 + * @param int $parentId 父级菜单ID + * @param bool $convertToCamelCase 是否转换为驼峰命名 + * @return array + */ + private function buildPermissionMenuTree(array $menus, int $parentId = 0, bool $convertToCamelCase = true): array + { + $tree = []; + + foreach ($menus as $menu) { + if ($menu['parent_id'] == $parentId) { + // 根据参数决定字段命名格式 + if ($convertToCamelCase) { + $menuItem = [ + 'id' => $menu['id'], + 'parentId' => $menu['parent_id'], + 'name' => $menu['name'], + 'component' => $menu['component'], + 'componentName' => $menu['component_name'], + 'icon' => $menu['icon'], + 'keepAlive' => $menu['keep_alive'], + 'visible' => $menu['visible'], + 'alwaysShow' => $menu['always_show'], + 'path' => $menu['path'], + ]; + } else { + // 使用表字段命名 + $menuItem = [ + 'id' => $menu['id'], + 'parent_id' => $menu['parent_id'], + 'name' => $menu['name'], + 'component' => $menu['component'], + 'component_name' => $menu['component_name'], + 'icon' => $menu['icon'], + 'keep_alive' => $menu['keep_alive'], + 'visible' => $menu['visible'], + 'always_show' => $menu['always_show'], + 'path' => $menu['path'], + ]; + } + + // 递归获取子菜单 + $children = $this->buildPermissionMenuTree($menus, $menu['id'], $convertToCamelCase); + $menuItem['children'] = $children; + + $tree[] = $menuItem; + } + } + + return $tree; + } + + /** + * 检查用户是否为超级管理员 + * + * @param \App\Models\User $user + * @return bool + */ + public function isSuperAdmin(\App\Models\User $user): bool + { + if ($user->username === 'admin') { + return true; + } + + $roleCodes = $user->roles()->where('status', 0)->pluck('code')->toArray(); + return in_array('admin', $roleCodes) || in_array('superadmin', $roleCodes); + } + + /** + * 获取用户有权限的菜单ID列表 + * + * @param \App\Models\User $user + * @return array + */ + public function getUserMenuIds(\App\Models\User $user): array + { + if ($this->isSuperAdmin($user)) { + // 超级管理员返回所有菜单ID + return SystemMenu::where('status', 0) + ->where('visible', 1) + ->whereIn('type', [1, 2]) + ->pluck('id') + ->toArray(); + } + + // 普通用户根据角色权限获取菜单ID + $roleIds = $user->roles()->where('status', 0)->pluck('id')->toArray(); + if (empty($roleIds)) { + return []; + } + + return \App\Models\System\SystemRoleMenu::whereIn('role_id', $roleIds) + ->pluck('menu_id') + ->unique() + ->toArray(); + } + /** * 获取父级菜单列表 */ public function getParentMenus(): array { return SystemMenu::where('type', '!=', 3) // 排除按钮类型 - ->where('status', 1) + ->where('status', 0) ->select('id', 'name', 'parent_id') ->orderBy('sort', 'asc') ->get() @@ -152,4 +339,4 @@ class SystemMenuService extends BaseService ); } } -} \ No newline at end of file +}