重构权限管理逻辑,使用SystemMenuService获取用户权限菜单,优化菜单树构建方式,移除冗余代码,更新相关控制器以支持新逻辑。
This commit is contained in:
parent
9b96bae23e
commit
6856a94cd6
@ -327,47 +327,14 @@ class AuthController extends BaseController
|
|||||||
{
|
{
|
||||||
$user = $request->user();
|
$user = $request->user();
|
||||||
|
|
||||||
// 获取用户角色
|
// 使用SystemMenuService获取权限菜单
|
||||||
$userRoles = $user->roles()->where('status', 0)->get();
|
$menuService = app(\App\Services\System\SystemMenuService::class);
|
||||||
$roleCodes = $userRoles->pluck('code')->toArray();
|
|
||||||
|
|
||||||
// 判断是否为超级管理员(username为admin或角色编码包含admin相关)
|
// 获取菜单树(使用驼峰命名)
|
||||||
$isSuperAdmin = $user->username === 'admin' ||
|
$menuTree = $menuService->getMenuTreeByPermission($user, true);
|
||||||
in_array('admin', $roleCodes) ||
|
|
||||||
in_array('super_admin', $roleCodes);
|
|
||||||
|
|
||||||
// 获取菜单权限
|
// 获取用户角色编码
|
||||||
if ($isSuperAdmin) {
|
$roleCodes = $user->roles()->where('status', 0)->pluck('code')->toArray();
|
||||||
// 超级管理员获取所有菜单
|
|
||||||
$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());
|
|
||||||
|
|
||||||
return $this->SuccessObject([
|
return $this->SuccessObject([
|
||||||
'menus' => $menuTree,
|
'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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -19,12 +19,12 @@ class SystemMenuController extends BaseController
|
|||||||
/**
|
/**
|
||||||
* 获取系统菜单列表
|
* 获取系统菜单列表
|
||||||
*/
|
*/
|
||||||
public function list(SystemMenuRequest $request): JsonResponse
|
public function list(): JsonResponse
|
||||||
{
|
{
|
||||||
$params = $request->validated();
|
$user = request()->user();
|
||||||
$result = $this->systemMenuService->getList($params);
|
$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
|
public function tree(): JsonResponse
|
||||||
{
|
{
|
||||||
$result = $this->systemMenuService->getMenuTree();
|
$user = request()->user();
|
||||||
|
$result = $this->systemMenuService->getMenuTreeByPermission($user, true);
|
||||||
return $this->Success($result);
|
return $this->Success($result);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -103,4 +104,4 @@ class SystemMenuController extends BaseController
|
|||||||
$this->systemMenuService->batchDelete($params['ids']);
|
$this->systemMenuService->batchDelete($params['ids']);
|
||||||
return $this->Success();
|
return $this->Success();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -73,7 +73,7 @@ class SystemMenuService extends BaseService
|
|||||||
*/
|
*/
|
||||||
public function getMenuTree(): array
|
public function getMenuTree(): array
|
||||||
{
|
{
|
||||||
$menus = SystemMenu::where('status', 1)
|
$menus = SystemMenu::where('status', 0)
|
||||||
->orderBy('sort', 'asc')
|
->orderBy('sort', 'asc')
|
||||||
->get()
|
->get()
|
||||||
->toArray();
|
->toArray();
|
||||||
@ -81,13 +81,200 @@ class SystemMenuService extends BaseService
|
|||||||
return $this->buildTree($menus);
|
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
|
public function getParentMenus(): array
|
||||||
{
|
{
|
||||||
return SystemMenu::where('type', '!=', 3) // 排除按钮类型
|
return SystemMenu::where('type', '!=', 3) // 排除按钮类型
|
||||||
->where('status', 1)
|
->where('status', 0)
|
||||||
->select('id', 'name', 'parent_id')
|
->select('id', 'name', 'parent_id')
|
||||||
->orderBy('sort', 'asc')
|
->orderBy('sort', 'asc')
|
||||||
->get()
|
->get()
|
||||||
@ -152,4 +339,4 @@ class SystemMenuService extends BaseService
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user