From fef9422da0d9d1322db1a5c559da4b2ad372e376 Mon Sep 17 00:00:00 2001 From: mzl Date: Fri, 11 Jul 2025 15:00:21 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B0=83=E6=95=B4=E8=8F=9C=E5=8D=95=E6=96=B9?= =?UTF-8?q?=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/common/logic/PermLogic.php | 61 +++++++--------------------------- 1 file changed, 12 insertions(+), 49 deletions(-) diff --git a/app/common/logic/PermLogic.php b/app/common/logic/PermLogic.php index 889470e..b4fe819 100644 --- a/app/common/logic/PermLogic.php +++ b/app/common/logic/PermLogic.php @@ -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 */