Sfoglia il codice sorgente

fix: 过滤隐藏菜单

周玉环 2 settimane fa
parent
commit
1ce33971db
1 ha cambiato i file con 22 aggiunte e 14 eliminazioni
  1. 22 14
      src/router/index.tsx

+ 22 - 14
src/router/index.tsx

@@ -3,7 +3,11 @@
  */
 
 import { lazy, Suspense } from "react";
-import { createBrowserRouter, Navigate, type RouteObject } from "react-router-dom";
+import {
+  createBrowserRouter,
+  Navigate,
+  type RouteObject,
+} from "react-router-dom";
 import MainLayout from "@/layouts/MainLayout/index";
 import LoginPage from "@/pages/login/index";
 
@@ -42,24 +46,24 @@ function loadComponent(componentPath: string, title: string) {
   const cleanPath = componentPath
     .replace(/^\//, "")
     .replace(/\.(tsx|vue|jsx|js)$/, "");
-  
+
   // 构建完整的模块路径
   const modulePath = `../pages/${cleanPath}.tsx`;
-  
+
   console.log(`[Router] Loading: "${cleanPath}" (${title})`);
   console.log(`[Router] Module path: ${modulePath}`);
-  
+
   // 检查模块是否存在
   const moduleLoader = pageModules[modulePath];
-  
+
   if (!moduleLoader) {
     console.warn(`[Router] ✗ Module not found: ${modulePath}`);
     console.warn(`[Router] Available modules:`, Object.keys(pageModules));
     return <DefaultPage title={title} />;
   }
-  
+
   // 使用 lazy 加载组件
-  const Component = lazy(() => 
+  const Component = lazy(() =>
     moduleLoader()
       .then((module: any) => {
         console.log(`[Router] ✓ Loaded: ${cleanPath}`);
@@ -89,7 +93,7 @@ function menuToRoute(menu: JeecgMenu, isChild = false): RouteObject | null {
 
   // 处理路径
   let routePath = menu.path || "";
-  
+
   // 如果是子路由且路径是绝对路径,只取最后一段作为相对路径
   if (isChild && routePath.startsWith("/")) {
     const segments = routePath.split("/").filter(Boolean);
@@ -110,8 +114,10 @@ function menuToRoute(menu: JeecgMenu, isChild = false): RouteObject | null {
     if (childRoutes.length > 0) {
       // 有子菜单,父路由作为容器,使用 Outlet
       // 获取第一个可见子菜单
-      const firstChild = menu.children.find((child) => !child.hidden && !child.meta?.hideMenu);
-      
+      const firstChild = menu.children.find(
+        (child) => !child.hidden && !child.meta?.hideMenu
+      );
+
       if (firstChild) {
         // 获取第一个子路由的相对路径
         const firstChildPath = firstChild.path || "";
@@ -157,7 +163,9 @@ function menuToRoute(menu: JeecgMenu, isChild = false): RouteObject | null {
 
 // 根据菜单数据生成路由
 export function generateRoutes(menus: JeecgMenu[]): RouteObject[] {
-  const routes = menus
+  const blackList = ["dashboard-analysis", "isystem"];
+  const renderMenus = menus.filter((item) => !blackList.includes(item.name));
+  const routes = renderMenus
     .map((menu) => menuToRoute(menu))
     .filter((r): r is RouteObject => r !== null);
 
@@ -168,7 +176,7 @@ export function generateRoutes(menus: JeecgMenu[]): RouteObject[] {
       children: [
         {
           index: true,
-          element: <Navigate to={menus[0]?.path || "/content-model"} replace />,
+          element: <Navigate to={renderMenus[0]?.path || "/content-model"} replace />,
         },
         ...routes,
       ],
@@ -183,7 +191,7 @@ export function generateRoutes(menus: JeecgMenu[]): RouteObject[] {
 // 创建路由器(初始为空路由)
 export function createAppRouter(menus: JeecgMenu[]) {
   const routes = generateRoutes(menus);
-  
+
   // 调试:打印路由路径结构
   console.log("=== Generated Routes ===");
   routes.forEach((route) => {
@@ -199,7 +207,7 @@ export function createAppRouter(menus: JeecgMenu[]) {
       });
     }
   });
-  
+
   // 创建路由器,不使用 basename(因为 Navbar 会添加 /spaces)
   return createBrowserRouter(routes, {
     basename: "/spaces",