调整菜单输出

This commit is contained in:
mzl
2025-07-22 15:29:54 +08:00
parent 32d394aa3c
commit e20ffa05c0
+61 -13
View File
@@ -54,14 +54,14 @@ class PermLogic
/** /**
* 构建目录/菜单树,节点包含权限与打开方式,拼接完整路径,并对子级菜单 component 处理 * 构建目录/菜单树
* *
* @param \Illuminate\Support\Collection $resources 全部资源集合 * @param \Illuminate\Support\Collection $resources 全部资源集合
* @param int $parentId 当前父级资源 ID * @param int $parentId 当前父级资源 ID
* @param string $parentPath 父级完整路径(用于递归) * @param string $parentPath 父级完整路径(用于递归)
* @return array * @return array
*/ */
public function buildTree($resources, int $parentId = 0, string $parentPath = ''): array public function buildTree_bak($resources, int $parentId = 0, string $parentPath = ''): array
{ {
$tree = []; $tree = [];
@@ -137,16 +137,7 @@ class PermLogic
return $tree; return $tree;
} }
public function buildTree_bak2($resources, int $parentId = 0, string $parentPath = ''): array
/**
* 构建目录/菜单树,节点包含权限与打开方式,拼接完整路径,并对子级菜单 component 处理
*
* @param \Illuminate\Support\Collection $resources 全部资源集合
* @param int $parentId 当前父级资源 ID
* @param string $parentPath 父级完整路径(用于递归)
* @return array
*/
public function buildTree_bak($resources, int $parentId = 0, string $parentPath = ''): array
{ {
$tree = []; $tree = [];
@@ -170,7 +161,7 @@ class PermLogic
// —— 递归构建子节点 —— // —— 递归构建子节点 ——
$children = []; $children = [];
if (in_array($node->type, ['directory', 'menu'])) { if ($node->type === 'directory') {
$children = $this->buildTree($resources, $node->id, $currentPath); $children = $this->buildTree($resources, $node->id, $currentPath);
} }
@@ -218,7 +209,64 @@ class PermLogic
return $tree; return $tree;
} }
public function buildTree($resources, int $parentId = 0, string $parentPath = ''): array
{
$tree = [];
foreach ($resources as $node) {
if ($node->parent_id !== $parentId) continue;
// 拼接完整路径
$currentPath = $parentPath;
if ($currentPath && substr($currentPath, -1) === '/') {
$currentPath = rtrim($currentPath, '/');
}
if (!empty($node->url)) {
if (!$currentPath) {
$currentPath = $node->url;
} else {
$url = ltrim($node->url, '/');
$currentPath = $currentPath . '/' . $url;
}
}
// 子节点递归
$children = [];
if ($node->type === 'directory') {
$children = $this->buildTree($resources, $node->id, $currentPath);
}
// 权限
$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();
// 判断是否有子级菜单(非权限)
$hasChildrenMenu = collect($resources)->where('parent_id', $node->id)
->whereIn('type', ['directory', 'menu'])->isNotEmpty();
$componentValue = $hasChildrenMenu ? '/index/index' : $currentPath;
$tree[] = [
'id' => $node->id,
'name' => $node->name,
'path' => $node->url,
'component' => $componentValue,
'meta' => [
'title' => $node->name,
'icon' => $node->icon,
'keepAlive' => true,
'authList' => $authList,
'openType' => $node->open_type,
],
'children' => $children,
];
}
return $tree;
}
/** /**
* 获取用户所有权限code * 获取用户所有权限code