|
1
2
|
import type { Router, RouteRecordRaw } from 'vue-router';
|
vben
authored
|
3
|
import { usePermissionStoreWithOut } from '/@/store/modules/permission';
|
vben
authored
|
4
|
|
|
5
|
import { PageEnum } from '/@/enums/pageEnum';
|
vben
authored
|
6
|
import { useUserStoreWithOut } from '/@/store/modules/user';
|
vben
authored
|
7
|
|
Vben
authored
|
8
|
import { PAGE_NOT_FOUND_ROUTE } from '/@/router/routes/basic';
|
|
9
|
|
|
10
11
|
import { RootRoute } from '/@/router/routes';
|
|
12
13
|
const LOGIN_PATH = PageEnum.BASE_LOGIN;
|
|
14
15
|
const ROOT_PATH = RootRoute.path;
|
|
16
17
18
|
const whitePathList: PageEnum[] = [LOGIN_PATH];
export function createPermissionGuard(router: Router) {
|
vben
authored
|
19
20
|
const userStore = useUserStoreWithOut();
const permissionStore = usePermissionStoreWithOut();
|
|
21
|
router.beforeEach(async (to, from, next) => {
|
|
22
23
24
25
26
27
|
if (
from.path === ROOT_PATH &&
to.path === PageEnum.BASE_HOME &&
userStore.getUserInfo.homePath &&
userStore.getUserInfo.homePath !== PageEnum.BASE_HOME
) {
|
|
28
29
|
next(userStore.getUserInfo.homePath);
return;
|
|
30
31
|
}
|
|
32
33
|
const token = userStore.getToken;
|
|
34
35
|
// Whitelist can be directly entered
if (whitePathList.includes(to.path as PageEnum)) {
|
|
36
37
38
39
40
41
42
43
44
45
|
if (to.path === LOGIN_PATH && token) {
const isSessionTimeout = userStore.getSessionTimeout;
try {
await userStore.afterLoginAction();
if (!isSessionTimeout) {
next((to.query?.redirect as string) || '/');
return;
}
} catch {}
}
|
|
46
|
next();
|
|
47
48
49
50
51
52
|
return;
}
// token does not exist
if (!token) {
// You can access without permission. You need to set the routing meta.ignoreAuth to true
|
vben
authored
|
53
|
if (to.meta.ignoreAuth) {
|
|
54
|
next();
|
|
55
56
|
return;
}
|
vben
authored
|
57
|
|
|
58
|
// redirect login page
|
Vben
authored
|
59
|
const redirectData: { path: string; replace: boolean; query?: Recordable<string> } = {
|
vben
authored
|
60
61
62
63
64
65
66
67
68
|
path: LOGIN_PATH,
replace: true,
};
if (to.path) {
redirectData.query = {
...redirectData.query,
redirect: to.path,
};
}
|
|
69
70
|
next(redirectData);
return;
|
|
71
|
}
|
vben
authored
|
72
|
|
|
73
74
75
76
77
78
79
80
81
82
|
// Jump to the 404 page after processing the login
if (
from.path === LOGIN_PATH &&
to.name === PAGE_NOT_FOUND_ROUTE.name &&
to.fullPath !== (userStore.getUserInfo.homePath || PageEnum.BASE_HOME)
) {
next(userStore.getUserInfo.homePath || PageEnum.BASE_HOME);
return;
}
|
|
83
84
|
// get userinfo while last fetch time is empty
if (userStore.getLastUpdateTime === 0) {
|
|
85
86
87
88
89
90
|
try {
await userStore.getUserInfoAction();
} catch (err) {
next();
return;
}
|
|
91
92
|
}
|
Vben
authored
|
93
|
if (permissionStore.getIsDynamicAddedRoute) {
|
|
94
|
next();
|
|
95
96
|
return;
}
|
vben
authored
|
97
|
|
|
98
|
const routes = await permissionStore.buildRoutesAction();
|
vben
authored
|
99
|
|
|
100
|
routes.forEach((route) => {
|
Vben
authored
|
101
|
router.addRoute(route as unknown as RouteRecordRaw);
|
|
102
103
|
});
|
|
104
105
|
router.addRoute(PAGE_NOT_FOUND_ROUTE as unknown as RouteRecordRaw);
|
Vben
authored
|
106
|
permissionStore.setDynamicAddedRoute(true);
|
|
107
108
109
|
if (to.name === PAGE_NOT_FOUND_ROUTE.name) {
// 动态添加路由后,此处应当重定向到fullPath,否则会加载404页面内容
|
|
110
|
next({ path: to.fullPath, replace: true, query: to.query });
|
|
111
112
113
114
115
116
|
} else {
const redirectPath = (from.query.redirect || to.path) as string;
const redirect = decodeURIComponent(redirectPath);
const nextData = to.path === redirect ? { ...to, replace: true } : { path: redirect };
next(nextData);
}
|
|
117
118
|
});
}
|