98 lines
2.4 KiB
TypeScript
98 lines
2.4 KiB
TypeScript
// src/utils/menuUtils.ts
|
|
import { MenuVO } from "@/services/system/menu";
|
|
import type { MenuDataItem } from "@ant-design/pro-components";
|
|
// src/utils/menuUtils.ts
|
|
|
|
// src/utils/menuUtils.ts
|
|
// src/utils/route.ts
|
|
export function transformMenuToRoutes(menuData: MenuVO[]): any[] {
|
|
return menuData.map((item) => ({
|
|
path: item.path,
|
|
name: item.name,
|
|
icon: item.icon,
|
|
component: item.component,
|
|
routes: item.children ? transformMenuToRoutes(item.children) : undefined,
|
|
}));
|
|
}
|
|
|
|
// src/utils/route.ts
|
|
export function transformBackendMenuToFlatRoutes(menuData: any[]) {
|
|
const flatRoutes: any[] = [];
|
|
|
|
function processMenu(items: any[], parentRouteId = "ant-design-pro-layout") {
|
|
items.forEach((item) => {
|
|
const currentRouteId = `route-${item.id}`;
|
|
|
|
// 处理路径 - 如果是子路由,需要组合完整路径
|
|
let fullPath = item.path;
|
|
if (item.parentId !== 0 && !item.path.startsWith("/")) {
|
|
// 子路由需要相对路径
|
|
fullPath = item.path;
|
|
}
|
|
|
|
const route: any = {
|
|
id: currentRouteId,
|
|
path: fullPath,
|
|
name: item.name,
|
|
parentId: parentRouteId,
|
|
};
|
|
|
|
// 添加图标(如果不是 # 的话)
|
|
if (item.icon && item.icon !== "#") {
|
|
route.icon = item.icon;
|
|
}
|
|
|
|
// 添加组件路径
|
|
if (item.component) {
|
|
// 转换组件路径为动态导入格式
|
|
route.component = item.component;
|
|
}
|
|
|
|
// 其他属性
|
|
if (!item.visible) {
|
|
route.hideInMenu = true;
|
|
}
|
|
|
|
flatRoutes.push(route);
|
|
|
|
// 递归处理子菜单
|
|
if (item.children && item.children.length > 0) {
|
|
processMenu(item.children, currentRouteId);
|
|
}
|
|
});
|
|
}
|
|
|
|
processMenu(menuData);
|
|
return flatRoutes;
|
|
}
|
|
|
|
export function transformMenuData(menuData: any[]) {
|
|
const transformItem = (item: any, parentPath = "") => {
|
|
const fullPath = item.path.startsWith("/")
|
|
? item.path
|
|
: `${parentPath}/${item.path}`;
|
|
const result: any = {
|
|
path: fullPath,
|
|
name: item.name,
|
|
key: `${item.id}`,
|
|
};
|
|
if (item.icon && item.icon !== "#") {
|
|
result.icon = item.icon;
|
|
}
|
|
|
|
if (!item.visible) {
|
|
result.hideInMenu = true;
|
|
}
|
|
|
|
if (item.children && item.children.length > 0) {
|
|
result.children = item.children.map((child: any) =>
|
|
transformItem(child, fullPath)
|
|
);
|
|
}
|
|
|
|
return;
|
|
result;
|
|
};
|
|
return menuData.map((item) => transformItem(item));
|
|
}
|