vben
authored
5 years ago
1
import type { AppRouteModule, AppRouteRecordRaw } from '/@/router/types';
Vben
authored
4 years ago
2
import type { Router, RouteRecordNormalized } from 'vue-router';
vben
authored
5 years ago
3
4
import { getParentLayout, LAYOUT } from '/@/router/constant';
5
import { cloneDeep, omit } from 'lodash-es';
vben
authored
5 years ago
6
import { warn } from '/@/utils/log';
Vben
authored
4 years ago
7
import { createRouter, createWebHashHistory } from 'vue-router';
vben
authored
5 years ago
8
vben
authored
5 years ago
9
export type LayoutMapKey = 'LAYOUT';
10
const IFRAME = () => import('/@/views/sys/iframe/FrameBlank.vue');
vben
authored
5 years ago
11
vben
authored
4 years ago
12
const LayoutMap = new Map<string, () => Promise<typeof import('*.vue')>>();
13
14
15
LayoutMap.set('LAYOUT', LAYOUT);
LayoutMap.set('IFRAME', IFRAME);
vben
authored
5 years ago
16
Vben
authored
4 years ago
17
let dynamicViewsModules: Record<string, () => Promise<Recordable>>;
vben
authored
5 years ago
18
Vben
authored
4 years ago
19
// Dynamic introduction
vben
authored
5 years ago
20
function asyncImportRoute(routes: AppRouteRecordRaw[] | undefined) {
Vben
authored
4 years ago
21
dynamicViewsModules = dynamicViewsModules || import.meta.glob('../../views/**/*.{vue,tsx}');
vben
authored
5 years ago
22
23
if (!routes) return;
routes.forEach((item) => {
24
25
26
if (!item.component && item.meta?.frameSrc) {
item.component = 'IFRAME';
}
vben
authored
5 years ago
27
28
29
const { component, name } = item;
const { children } = item;
if (component) {
vben
authored
4 years ago
30
const layoutFound = LayoutMap.get(component as string);
31
32
33
34
35
if (layoutFound) {
item.component = layoutFound;
} else {
item.component = dynamicImport(dynamicViewsModules, component as string);
}
vben
authored
5 years ago
36
} else if (name) {
Vben
authored
4 years ago
37
item.component = getParentLayout();
vben
authored
5 years ago
38
39
40
41
42
}
children && asyncImportRoute(children);
});
}
vben
authored
5 years ago
43
function dynamicImport(
Vben
authored
4 years ago
44
dynamicViewsModules: Record<string, () => Promise<Recordable>>,
vben
authored
5 years ago
45
46
47
component: string
) {
const keys = Object.keys(dynamicViewsModules);
vben
authored
5 years ago
48
const matchKeys = keys.filter((key) => {
Vben
authored
4 years ago
49
50
51
52
let k = key.replace('../../views', '');
const lastIndex = k.lastIndexOf('.');
k = k.substring(0, lastIndex);
return k === component;
vben
authored
5 years ago
53
54
55
});
if (matchKeys?.length === 1) {
const matchKey = matchKeys[0];
vben
authored
5 years ago
56
return dynamicViewsModules[matchKey];
vben
authored
5 years ago
57
58
59
60
61
62
63
64
65
}
if (matchKeys?.length > 1) {
warn(
'Please do not create `.vue` and `.TSX` files with the same file name in the same hierarchical directory under the views folder. This will cause dynamic introduction failure'
);
return;
}
}
vben
authored
5 years ago
66
67
68
// Turn background objects into routing objects
export function transformObjToRoute<T = AppRouteModule>(routeList: AppRouteModule[]): T[] {
routeList.forEach((route) => {
vben
authored
4 years ago
69
70
71
72
const component = route.component as string;
if (component) {
if (component.toUpperCase() === 'LAYOUT') {
route.component = LayoutMap.get(component.toUpperCase());
vben
authored
5 years ago
73
74
75
76
77
78
79
80
81
82
83
84
85
} else {
route.children = [cloneDeep(route)];
route.component = LAYOUT;
route.name = `${route.name}Parent`;
route.path = '';
const meta = route.meta || {};
meta.single = true;
meta.affix = false;
route.meta = meta;
}
}
route.children && asyncImportRoute(route.children);
});
86
return routeList as unknown as T[];
vben
authored
5 years ago
87
88
}
Vben
authored
4 years ago
89
90
91
/**
* Convert multi-level routing to level 2 routing
*/
Vben
authored
4 years ago
92
93
94
95
export function flatMultiLevelRoutes(routeModules: AppRouteModule[]) {
const modules: AppRouteModule[] = cloneDeep(routeModules);
for (let index = 0; index < modules.length; index++) {
const routeModule = modules[index];
Vben
authored
4 years ago
96
97
98
99
100
if (!isMultipleRoute(routeModule)) {
continue;
}
promoteRouteLevel(routeModule);
}
Vben
authored
4 years ago
101
return modules;
Vben
authored
4 years ago
102
103
104
105
106
107
}
// Routing level upgrade
function promoteRouteLevel(routeModule: AppRouteModule) {
// Use vue-router to splice menus
let router: Router | null = createRouter({
108
routes: [routeModule as unknown as RouteRecordNormalized],
Vben
authored
4 years ago
109
110
111
112
history: createWebHashHistory(),
});
const routes = router.getRoutes();
Vben
authored
4 years ago
113
addToChildren(routes, routeModule.children || [], routeModule);
Vben
authored
4 years ago
114
115
router = null;
116
routeModule.children = routeModule.children?.map((item) => omit(item, 'children'));
Vben
authored
4 years ago
117
118
119
120
121
122
123
124
125
126
127
}
// Add all sub-routes to the secondary route
function addToChildren(
routes: RouteRecordNormalized[],
children: AppRouteRecordRaw[],
routeModule: AppRouteModule
) {
for (let index = 0; index < children.length; index++) {
const child = children[index];
const route = routes.find((item) => item.name === child.name);
Vben
authored
4 years ago
128
129
130
131
132
if (!route) {
continue;
}
routeModule.children = routeModule.children || [];
if (!routeModule.children.find((item) => item.name === route.name)) {
133
routeModule.children?.push(route as unknown as AppRouteModule);
Vben
authored
4 years ago
134
135
136
}
if (child.children?.length) {
addToChildren(routes, child.children, routeModule);
Vben
authored
4 years ago
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
}
}
}
// Determine whether the level exceeds 2 levels
function isMultipleRoute(routeModule: AppRouteModule) {
if (!routeModule || !Reflect.has(routeModule, 'children') || !routeModule.children?.length) {
return false;
}
const children = routeModule.children;
let flag = false;
for (let index = 0; index < children.length; index++) {
const child = children[index];
if (child.children?.length) {
flag = true;
break;
}
}
return flag;
vben
authored
5 years ago
158
}