vben
authored
|
1
|
import { AppRouteModule } from '/@/router/types';
|
|
2
3
|
import type { MenuModule, Menu, AppRouteRecordRaw } from '/@/router/types';
|
vben
authored
|
4
|
import { findPath, forEach, treeMap } from '/@/utils/helper/treeHelper';
|
|
5
|
import { cloneDeep } from 'lodash-es';
|
vben
authored
|
6
|
import { isUrl } from '/@/utils/is';
|
|
7
8
9
10
11
12
|
export function getAllParentPath(treeData: any[], path: string) {
const menuList = findPath(treeData, (n) => n.path === path) as Menu[];
return (menuList || []).map((item) => item.path);
}
|
vben
authored
|
13
|
// 拼接父级路径
|
|
14
15
16
17
18
19
20
21
22
23
24
25
|
function joinParentPath(list: any, node: any) {
let allPaths = getAllParentPath(list, node.path);
allPaths = allPaths.slice(0, allPaths.length - 1);
let parentPath = '';
if (Array.isArray(allPaths) && allPaths.length >= 2) {
parentPath = allPaths[allPaths.length - 1];
} else {
allPaths.forEach((p) => {
parentPath += /^\//.test(p) ? p : `/${p}`;
});
}
|
vben
authored
|
26
|
node.path = `${/^\//.test(node.path) ? node.path : `${parentPath}/${node.path}`}`.replace(
|
|
27
28
29
30
31
32
|
/\/\//g,
'/'
);
return node;
}
|
vben
authored
|
33
|
// 解析菜单模块
|
|
34
35
36
37
38
|
export function transformMenuModule(menuModule: MenuModule): Menu {
const { menu } = menuModule;
const menuList = [menu];
forEach(menuList, (m) => {
|
vben
authored
|
39
|
!isUrl(m.path) && joinParentPath(menuList, m);
|
|
40
|
});
|
vben
authored
|
41
|
|
|
42
43
44
45
46
47
|
return menuList[0];
}
export function transformRouteToMenu(routeModList: AppRouteModule[]) {
const cloneRouteModList = cloneDeep(routeModList);
const routeList: AppRouteRecordRaw[] = [];
|
vben
authored
|
48
49
50
51
52
53
54
|
// cloneRouteModList = filter(cloneRouteModList, (node) => {
// if (Reflect.has(node?.meta ?? {}, 'hideMenu')) {
// return !node?.meta.hideMenu;
// }
// return true;
// });
|
|
55
|
cloneRouteModList.forEach((item) => {
|
vben
authored
|
56
57
58
|
if (item.meta?.single) {
const realItem = item?.children?.[0];
realItem && routeList.push(realItem);
|
vben
authored
|
59
|
} else {
|
vben
authored
|
60
|
routeList.push(item);
|
vben
authored
|
61
|
}
|
|
62
63
64
|
});
return treeMap(routeList, {
conversion: (node: AppRouteRecordRaw) => {
|
vben
authored
|
65
|
const { meta: { title, icon, hideMenu = false } = {} } = node;
|
vben
authored
|
66
67
|
!isUrl(node.path) && joinParentPath(routeList, node);
|
|
68
69
70
71
|
return {
name: title,
icon,
path: node.path,
|
vben
authored
|
72
|
hideMenu,
|
|
73
74
75
76
|
};
},
});
}
|