|
|
@@ -13,7 +13,7 @@ export interface RouteItem {
|
|
|
title?: string;
|
|
|
icon?: string;
|
|
|
auth?: string[];
|
|
|
- menu?:boolean;
|
|
|
+ menu?: boolean;
|
|
|
};
|
|
|
children?: RouteItem[];
|
|
|
}
|
|
|
@@ -22,7 +22,7 @@ export interface TreeNode {
|
|
|
id: number;
|
|
|
title: string;
|
|
|
icon?: string;
|
|
|
- type?:string,
|
|
|
+ type?: string,
|
|
|
children?: TreeNode[];
|
|
|
[key: string]: any; // 允许任意其他属性
|
|
|
}
|
|
|
@@ -33,6 +33,51 @@ let idCounter = ref(0);
|
|
|
const generateId = (): number => {
|
|
|
return ++idCounter.value;
|
|
|
};
|
|
|
+// function extractImportPath(func: any) {
|
|
|
+// if (!func) {
|
|
|
+// console.log("No function provided");
|
|
|
+// return null;
|
|
|
+// }
|
|
|
+
|
|
|
+// if (typeof func !== "function") {
|
|
|
+// console.error("Provided value is not a function");
|
|
|
+// return null;
|
|
|
+// }
|
|
|
+// // 将函数转换为字符串
|
|
|
+// const funcStr = func.toString();
|
|
|
+
|
|
|
+// // 如果字符串中包含 "layouts",返回 "layout"
|
|
|
+// if (funcStr.includes("layouts")) {
|
|
|
+// return "layout";
|
|
|
+// }
|
|
|
+
|
|
|
+// // 检查 import() 的结构是否存在
|
|
|
+// const startIndex = funcStr.indexOf('import("') + 8;
|
|
|
+// const endIndex = funcStr.indexOf('")', startIndex);
|
|
|
+
|
|
|
+// if (startIndex > 7 && endIndex > startIndex) {
|
|
|
+// let path = funcStr.substring(startIndex, endIndex);
|
|
|
+
|
|
|
+// // 如果路径中存在时间戳(例如 ?t=xxx),将其去掉
|
|
|
+// if (path.includes("?")) {
|
|
|
+// path = path.split("?")[0];
|
|
|
+// }
|
|
|
+
|
|
|
+// // 如果路径中存在 .vue 后缀,将其移除
|
|
|
+// if (path.endsWith(".vue")) {
|
|
|
+// path = path.slice(0, -4);
|
|
|
+// }
|
|
|
+
|
|
|
+// // 去掉路径中的 /src/views 前缀
|
|
|
+// const prefixToRemove = "/src/views";
|
|
|
+// if (path.startsWith(prefixToRemove)) {
|
|
|
+// path = path.slice(prefixToRemove.length);
|
|
|
+// }
|
|
|
+
|
|
|
+// return path;
|
|
|
+// }
|
|
|
+// return null;
|
|
|
+// }
|
|
|
|
|
|
// 预处理函数:确保 children 和 auth 类型正确
|
|
|
export const preprocessRoutes = (routes: any[]): RouteItem[] => {
|
|
|
@@ -46,8 +91,8 @@ export const preprocessRoutes = (routes: any[]): RouteItem[] => {
|
|
|
auth: Array.isArray(route.meta?.auth)
|
|
|
? route.meta.auth
|
|
|
: route.meta?.auth
|
|
|
- ? [route.meta.auth]
|
|
|
- : [],
|
|
|
+ ? [route.meta.auth]
|
|
|
+ : [],
|
|
|
},
|
|
|
// 确保 children 存在且是数组
|
|
|
children: route.children ? preprocessRoutes(route.children) : [],
|
|
|
@@ -74,20 +119,20 @@ const parseAuthToChildren = (authList: string[]): TreeNode[] => {
|
|
|
};
|
|
|
|
|
|
// 将 RouteItem 转换为 TreeNode
|
|
|
-export const convertToTreeData = (id:number,routes: RouteItem[]): TreeNode[] => {
|
|
|
- idCounter.value=id;
|
|
|
+export const convertToTreeData = (id: number, routes: RouteItem[]): TreeNode[] => {
|
|
|
+ idCounter.value = id;
|
|
|
return routes.filter(route => route.meta?.menu !== false).map(route => {
|
|
|
const node: TreeNode = {
|
|
|
id: generateId(),
|
|
|
title: route.meta?.title || route.name || '',
|
|
|
icon: route.meta?.icon,
|
|
|
- type:route.path?"1":'0',
|
|
|
+ type: route.path ? "1" : '0',
|
|
|
+
|
|
|
};
|
|
|
|
|
|
if (route.children && route.children.length > 0) {
|
|
|
- node.children = convertToTreeData(idCounter.value,route.children);
|
|
|
+ node.children = convertToTreeData(idCounter.value, route.children);
|
|
|
}
|
|
|
-
|
|
|
if (route.meta?.auth) {
|
|
|
const authNodes = parseAuthToChildren(route.meta.auth);
|
|
|
node.children = node.children ? node.children.concat(authNodes) : authNodes;
|