Explorar o código

fix: filterChildrenTree 精准过滤泄漏的二级子路由

上一版用 > 0 会把合法的顶层叶子路由(如 layer-template)也误删,
导致侧边栏为空。改为精准判断:只有 path 是另一个顶级路由
子路径且自身无 children 的条目才被移除。
合法的顶层叶子路由(path 不以任何同级路由为前缀)保留。

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
skyline hai 1 semana
pai
achega
48961ece9e
Modificáronse 1 ficheiros con 17 adicións e 2 borrados
  1. 17 2
      haha-admin-web/src/router/utils.ts

+ 17 - 2
haha-admin-web/src/router/utils.ts

@@ -63,9 +63,24 @@ function filterTree(data: RouteComponent[]) {
   return newTree;
 }
 
-/** 过滤没有 children 的目录,v?.children?.length > 0 确保 undefined 和空数组都被过滤 */
+/** 过滤没有 children 的目录,以及泄漏到一级的二级子路由 */
 function filterChildrenTree(data: RouteComponent[]) {
-  const newTree = cloneDeep(data).filter((v: any) => v?.children?.length > 0);
+  const newTree = cloneDeep(data).filter((v: any) => {
+    // 有非空 children → 保留(正常的父级菜单)
+    if (v?.children?.length > 0) return true;
+    // children 为空数组 → 移除
+    if (Array.isArray(v?.children)) return false;
+    // children 为 undefined:可能是合法的顶层叶子路由,也可能是泄漏的二级子路由
+    // 如果它的 path 是另一个顶级路由的子路径,说明是泄漏的二级路由 → 移除
+    const isChild = data.some(
+      (parent: any) =>
+        parent !== v &&
+        parent.path &&
+        v.path &&
+        v.path.startsWith(parent.path + "/")
+    );
+    return !isChild;
+  });
   newTree.forEach(
     (v: { children }) => v.children && (v.children = filterTree(v.children))
   );