feat: 路由登录 动态路由重定向

This commit is contained in:
2026-01-26 17:59:52 +08:00
parent 502c236b0d
commit a5d3342d93
3 changed files with 102 additions and 194 deletions

View File

@@ -2,7 +2,10 @@ import { Spin } from 'antd';
import React from 'react';
import type { MenuVO } from '@/services/system/menu';
export const loopMenuItem = (menus: MenuVO[], pId: number | string): any[] => {
export const loopMenuItem = (
menus: MenuVO[],
pId: number | string = '/',
): any[] => {
return menus.map((item) => {
let Component: React.ComponentType<any> | null = null;
if (item.component && item.component.trim().length > 0) {
@@ -59,113 +62,3 @@ export const loopMenuItem = (menus: MenuVO[], pId: number | string): any[] => {
return routeItem;
});
};
// return menus.flatMap((item) => {
// let Component: React.ComponentType<any> | null = null;
// if (item.component && item.component.length > 0) {
// // 防止配置了路由,但本地暂未添加对应的页面,产生的错误
// Component = React.lazy(() => {
// const importComponent = () => import(`@/pages/${item.component}`);
// const import404 = () => import("@/pages/404");
// return importComponent().catch(import404);
// });
// }
// if (item.children && item.children.length > 0) {
// return [
// {
// path: item.path,
// hideInMenu: false,
// parentId: pId,
// id: item.id,
// children: [...loopMenuItem(item.children, item.id)], // 添加缺失的 children 属性
// },
// ];
// } else {
// return [
// {
// path: item.path,
// name: item.name,
// // icon: item.icon,
// id: item.id,
// parentId: pId,
// hideInMenu: !item.visible,
// element: (
// <React.Suspense
// fallback={<Spin style={{ width: "100%", height: "100%" }} />}
// >
// {Component && <Component />}
// </React.Suspense>
// ),
// },
// ];
// }
// });
// return [];
// };
// function getFirstLeafPath(menus: any[], parentPath: string): string {
// const firstMenu = menus[0];
// const currentPath = `${parentPath}/${firstMenu.path}`;
// if (firstMenu.children && firstMenu.children.length > 0) {
// if (!firstMenu.hideInMenu) {
// return getFirstLeafPath(firstMenu.children, currentPath);
// } else {
// return getFirstLeafPath(firstMenu.children, parentPath);
// }
// } else {
// return currentPath;
// }
// }
// src/utils/route.ts
import { lazy } from 'react';
export interface RouteItem {
id: number;
parentId: number;
name: string;
path: string;
component: string | null;
componentName: string;
icon: string;
visible: boolean;
keepAlive: boolean;
alwaysShow: boolean;
children: RouteItem[] | null;
}
/**
* 转换后端路由数据为 Ant Design Pro 路由格式
*/
export function transformRoutes(routes: MenuVO[]): any[] {
return routes
.filter((route) => route.visible) // 只显示可见路由
.map((route) => {
const routeConfig: any = {
path: route.path,
name: route.name,
icon: route.icon || undefined,
};
// 处理组件
if (route.component) {
routeConfig.component = lazy(
() =>
import(`@/pages/${route.component}`).catch(
() => import('@/pages/404'),
), // 组件不存在时显示404
);
}
// 处理子路由
if (route.children && route.children.length > 0) {
routeConfig.routes = transformRoutes(route.children);
}
// 隐藏菜单但保留路由
if (!route.visible) {
routeConfig.hideInMenu = true;
}
return routeConfig;
});
}