调整菜单方法
This commit is contained in:
@@ -25,48 +25,7 @@ class PermLogic
|
||||
}
|
||||
|
||||
/**
|
||||
* 构建目录/菜单树,节点包含权限与打开方式
|
||||
*/
|
||||
// public function buildTree($resources, int $parentId = 0): array
|
||||
// {
|
||||
// $tree = [];
|
||||
// foreach ($resources as $node) {
|
||||
// if ($node->parent_id !== $parentId) continue;
|
||||
|
||||
// // 子节点
|
||||
// $children = [];
|
||||
// if ($node->type === 'directory') {
|
||||
// $children = $this->buildTree($resources, $node->id);
|
||||
// }
|
||||
|
||||
// // 本级权限列表
|
||||
// $authList = $resources
|
||||
// ->filter(fn($r) => $r->type === 'permission' && $r->parent_id === $node->id)
|
||||
// ->map(fn($r) => [
|
||||
// 'id' => $r->id,
|
||||
// 'title' => $r->name,
|
||||
// 'auth_mark' => $r->code,
|
||||
// ])->values()->all();
|
||||
|
||||
// $tree[] = [
|
||||
// 'id' => $node->id,
|
||||
// 'name' => $node->name,
|
||||
// 'path' => $node->url,
|
||||
// 'component' => $node->type === 'directory' ? 'Layout' : 'Page',
|
||||
// 'meta' => [
|
||||
// 'title' => $node->name,
|
||||
// 'icon' => $node->icon,
|
||||
// 'keepAlive' => true,
|
||||
// 'authList' => $authList,
|
||||
// 'openType' => $node->open_type, // 'internal' | 'external' | 'iframe'
|
||||
// ],
|
||||
// 'children' => $children,
|
||||
// ];
|
||||
// }
|
||||
// return $tree;
|
||||
// }
|
||||
/**
|
||||
* 构建目录/菜单树,节点包含权限与打开方式,并拼接完整路径到 component 字段
|
||||
* 构建目录/菜单树,节点包含权限与打开方式,拼接完整路径,并对子级菜单component处理
|
||||
* @param \Illuminate\Support\Collection $resources
|
||||
* @param int $parentId
|
||||
* @param string $parentPath
|
||||
@@ -80,23 +39,19 @@ class PermLogic
|
||||
|
||||
// 拼接完整路径
|
||||
$currentPath = $parentPath;
|
||||
// 去掉结尾斜杠,防止重复
|
||||
if ($currentPath && substr($currentPath, -1) === '/') {
|
||||
$currentPath = rtrim($currentPath, '/');
|
||||
}
|
||||
// 子菜单的 path 不要补全斜杠
|
||||
if (!empty($node->url)) {
|
||||
// 如果当前是根节点或父路径为空,直接用自身
|
||||
if (!$currentPath) {
|
||||
$currentPath = $node->url;
|
||||
} else {
|
||||
// 防止 url 以 / 开头导致双斜杠
|
||||
$url = ltrim($node->url, '/');
|
||||
$currentPath = $currentPath . '/' . $url;
|
||||
}
|
||||
}
|
||||
|
||||
// 子节点
|
||||
// 子节点递归
|
||||
$children = [];
|
||||
if ($node->type === 'directory') {
|
||||
$children = $this->buildTree($resources, $node->id, $currentPath);
|
||||
@@ -111,11 +66,20 @@ class PermLogic
|
||||
'auth_mark' => $r->code,
|
||||
])->values()->all();
|
||||
|
||||
// 判断是否有子级菜单(非权限)
|
||||
$hasChildrenMenu = collect($resources)->where('parent_id', $node->id)
|
||||
->whereIn('type', ['directory', 'menu'])->isNotEmpty();
|
||||
|
||||
// component 字段规则:
|
||||
// 1. 存在子菜单,固定 '/index/index'
|
||||
// 2. 否则为当前完整路径(或其它你想要的)
|
||||
$componentValue = $hasChildrenMenu ? '/index/index' : $currentPath;
|
||||
|
||||
$tree[] = [
|
||||
'id' => $node->id,
|
||||
'name' => $node->name,
|
||||
'path' => $node->url,
|
||||
'component' => $currentPath, // 这里就是全路径
|
||||
'component' => $componentValue,
|
||||
'meta' => [
|
||||
'title' => $node->name,
|
||||
'icon' => $node->icon,
|
||||
@@ -129,7 +93,6 @@ class PermLogic
|
||||
return $tree;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 获取用户所有权限code
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user