1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import { Router, createRouter, createWebHashHistory } from 'vue-router';
import {
View404,
LoginView,
useViewStack,
} from '@ibiz-template/mob-vue3-components';
import { Modal, ViewMode } from '@ibiz-template/runtime';
import { isNilOrEmpty } from 'qx-util';
import { AppRedirectView } from '@ibiz-template/vue3-util';
import { AuthGuard } from '../guard';
import { RouterShell, HomeView } from '@/components';
const getPropsCallback = (depth: number) => {
if (depth === 1) {
return () => ({});
}
return () => ({
modal: new Modal({
mode: ViewMode.ROUTE,
viewUsage: 1,
routeDepth: depth,
}),
});
};
export class AppRouter {
private static router?: Router;
/**
* 创建vue路由对象
* @author lxm
* @date 2023-06-29 07:56:11
* @protected
* @static
* @return {*}
*/
protected static createRouter() {
// 导航守卫
const beforeEnter = async () => {
// 判断是否已经登录
if (!isNilOrEmpty(ibiz.appData)) {
return true;
}
const authority = await AuthGuard();
return authority;
};
const placeholder = ibiz.env.routePlaceholder;
// 参数正则,占位符或者以等号相隔的参数键值对
const paramReg = `[^/]+=[^/]+|${placeholder}`;
// 视图正则,非=/的字符串
const viewReg = `[^=/]+`;
// 二级之后的子路由
const children = [
{
path: '404',
name: '404View2',
meta: { preset: true },
component: View404,
},
{
path: `:view2(${viewReg})/:params2(${paramReg})`,
component: RouterShell,
props: getPropsCallback(2),
children: [
{
path: '404',
name: '404View3',
meta: { preset: true },
component: View404,
},
{
path: `:view3(${viewReg})/:params3(${paramReg})`,
component: RouterShell,
props: getPropsCallback(3),
children: [
{
path: '404',
name: '404View4',
meta: { preset: true },
component: View404,
},
{
path: `:view4(${viewReg})/:params4(${paramReg})`,
component: RouterShell,
props: getPropsCallback(4),
},
{
path: ':pathMatch(.*)*',
redirect: { name: '404View4' },
},
],
},
{
path: ':pathMatch(.*)*',
redirect: { name: '404View3' },
},
],
},
{
path: ':pathMatch(.*)*',
redirect: { name: '404View2' },
},
];
return createRouter({
// 4. 内部提供了 history 模式的实现。为了简单起见,我们在这里使用 hash 模式。
history: createWebHashHistory(),
routes: [
{
path: '/',
redirect: `/${placeholder}/index/${placeholder}`,
},
{
path: '/login',
name: 'loginView',
meta: { preset: true },
component: LoginView,
},
{
path: '/404',
name: '404View1',
meta: { preset: true },
component: View404,
},
{
path: '/appredirectview',
name: 'appRedirectView',
meta: { preset: true },
beforeEnter,
component: AppRedirectView,
},
{
path: `/:appContext(${paramReg})/home/:params1(${paramReg})`,
meta: { home: true },
beforeEnter,
component: HomeView,
children,
},
{
path: `/:appContext(${paramReg})/:view1(${viewReg})/:params1(${paramReg})`,
beforeEnter,
component: RouterShell,
children,
},
{
path: '/:pathMatch(.*)*',
redirect: { name: '404View1' },
},
],
});
}
/**
* 获取路由对象
* @author lxm
* @date 2023-06-29 07:56:05
* @static
* @return {*}
*/
static getRouter(): Router {
if (!this.router) {
this.router = this.createRouter();
// 初始化视图堆栈,监听路由
const { init } = useViewStack();
init(this.router);
}
return this.router;
}
}