vben
authored
|
1
|
import { AppRouteModule } from '/@/router/types';
|
|
2
|
import type { MenuModule, Menu, AppRouteRecordRaw } from '/@/router/types';
|
Vben
authored
|
3
|
import { findPath, treeMap } from '/@/utils/helper/treeHelper';
|
|
4
|
import { cloneDeep } from 'lodash-es';
|
vben
authored
|
5
|
import { isUrl } from '/@/utils/is';
|
|
6
7
|
import { RouteParams } from 'vue-router';
import { toRaw } from 'vue';
|
|
8
|
|
Vben
authored
|
9
|
export function getAllParentPath<T = Recordable>(treeData: T[], path: string) {
|
|
10
11
12
13
|
const menuList = findPath(treeData, (n) => n.path === path) as Menu[];
return (menuList || []).map((item) => item.path);
}
|
|
14
|
// 路径处理
|
Vben
authored
|
15
16
17
|
function joinParentPath(menus: Menu[], parentPath = '') {
for (let index = 0; index < menus.length; index++) {
const menu = menus[index];
|
Vben
authored
|
18
19
|
// https://next.router.vuejs.org/guide/essentials/nested-routes.html
// Note that nested paths that start with / will be treated as a root path.
|
|
20
|
// 请注意,以 / 开头的嵌套路径将被视为根路径。
|
Vben
authored
|
21
|
// This allows you to leverage the component nesting without having to use a nested URL.
|
|
22
|
// 这允许你利用组件嵌套,而无需使用嵌套 URL。
|
Vben
authored
|
23
24
|
if (!(menu.path.startsWith('/') || isUrl(menu.path))) {
// path doesn't start with /, nor is it a url, join parent path
|
|
25
|
// 路径不以 / 开头,也不是 url,加入父路径
|
Vben
authored
|
26
27
|
menu.path = `${parentPath}/${menu.path}`;
}
|
Vben
authored
|
28
|
if (menu?.children?.length) {
|
|
29
|
joinParentPath(menu.children, menu.meta?.hidePathForChildren ? parentPath : menu.path);
|
Vben
authored
|
30
|
}
|
|
31
32
33
|
}
}
|
Vben
authored
|
34
|
// Parsing the menu module
|
|
35
36
37
38
|
export function transformMenuModule(menuModule: MenuModule): Menu {
const { menu } = menuModule;
const menuList = [menu];
|
vben
authored
|
39
|
|
Vben
authored
|
40
|
joinParentPath(menuList);
|
|
41
42
43
|
return menuList[0];
}
|
|
44
|
// 将路由转换成菜单
|
Vben
authored
|
45
|
export function transformRouteToMenu(routeModList: AppRouteModule[], routerMapping = false) {
|
|
46
|
// 借助 lodash 深拷贝
|
|
47
48
|
const cloneRouteModList = cloneDeep(routeModList);
const routeList: AppRouteRecordRaw[] = [];
|
vben
authored
|
49
|
|
|
50
|
// 对路由项进行修改
|
|
51
|
cloneRouteModList.forEach((item) => {
|
Vben
authored
|
52
53
54
|
if (routerMapping && item.meta.hideChildrenInMenu && typeof item.redirect === 'string') {
item.path = item.redirect;
}
|
|
55
|
|
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
|
// 提取树指定结构
|
Vben
authored
|
64
|
const list = treeMap(routeList, {
|
|
65
|
conversion: (node: AppRouteRecordRaw) => {
|
Vben
authored
|
66
|
const { meta: { title, hideMenu = false } = {} } = node;
|
Vben
authored
|
67
|
|
|
68
|
return {
|
Vben
authored
|
69
|
...(node.meta || {}),
|
Vben
authored
|
70
|
meta: node.meta,
|
|
71
|
name: title,
|
vben
authored
|
72
|
hideMenu,
|
Vben
authored
|
73
|
path: node.path,
|
|
74
|
...(node.redirect ? { redirect: node.redirect } : {}),
|
|
75
76
77
|
};
},
});
|
|
78
|
// 路径处理
|
Vben
authored
|
79
|
joinParentPath(list);
|
Vben
authored
|
80
|
return cloneDeep(list);
|
|
81
|
}
|
|
82
83
84
85
|
/**
* config menu with given params
*/
|
Vben
authored
|
86
|
const menuParamRegex = /(?::)([\s\S]+?)((?=\/)|$)/g;
|
|
87
|
|
|
88
89
90
91
|
export function configureDynamicParamsMenu(menu: Menu, params: RouteParams) {
const { path, paramPath } = toRaw(menu);
let realPath = paramPath ? paramPath : path;
const matchArr = realPath.match(menuParamRegex);
|
Vben
authored
|
92
|
|
|
93
|
matchArr?.forEach((it) => {
|
Vben
authored
|
94
95
96
|
const realIt = it.substr(1);
if (params[realIt]) {
realPath = realPath.replace(`:${realIt}`, params[realIt] as string);
|
|
97
98
99
100
101
102
103
104
105
106
|
}
});
// save original param path.
if (!paramPath && matchArr && matchArr.length > 0) {
menu.paramPath = path;
}
menu.path = realPath;
// children
menu.children?.forEach((item) => configureDynamicParamsMenu(item, params));
}
|