permissionGuard.ts
5.86 KB
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import type { Router, RouteRecordRaw } from 'vue-router';
import { usePermissionStoreWithOut } from '/@/store/modules/permission';
import { PageEnum } from '/@/enums/pageEnum';
import { useUserStoreWithOut } from '/@/store/modules/user';
import { PAGE_NOT_FOUND_ROUTE } from '/@/router/routes/basic';
import { RootRoute } from '/@/router/routes';
import { router } from '/@/router';
import { getMenuList } from '/@/api/sys/menu';
import { USER_INFO_KEY } from '/@/enums/cacheEnum';
import { getAuthCache } from '/@/utils/auth';
import { ref } from 'vue';
const LOGIN_PATH = PageEnum.BASE_LOGIN;
const ROOT_PATH = RootRoute.path;
const whitePathList: PageEnum[] = [LOGIN_PATH];
// const userInfo1 = getAuthCache(USER_INFO_KEY);
// const userInfo = ref(userInfo1);
export function createPermissionGuard(router: Router) {
const userStore = useUserStoreWithOut();
const permissionStore = usePermissionStoreWithOut();
router.beforeEach(async (to, from, next) => {
const userInfo1 = await getAuthCache(USER_INFO_KEY);
const userInfo = ref(userInfo1);
if (
from.path === ROOT_PATH &&
to.path === PageEnum.BASE_HOME &&
userStore.getUserInfo.homePath &&
userStore.getUserInfo.homePath !== PageEnum.BASE_HOME
) {
next(userStore.getUserInfo.homePath);
return;
}
// Whitelist can be directly entered
if (whitePathList.includes(to.path as PageEnum)) {
next();
return;
}
const token = userStore.getJwtToken;
// token does not exist
if (!token) {
// You can access without permission. You need to set the routing meta.ignoreAuth to true
if (to.meta.ignoreAuth) {
next();
return;
}
// redirect login page
const redirectData: { path: string; replace: boolean; query?: Recordable<string> } = {
path: LOGIN_PATH,
replace: true,
};
if (to.path) {
redirectData.query = {
...redirectData.query,
redirect: to.path,
};
}
next(redirectData);
return;
}
if (from.path === LOGIN_PATH && userInfo.value?.needSetPwd == false) {
const getMenuListData = await getMenuList();
const getHomePage = getMenuListData.find((f) => {
return f.path == '/dashboard/workbench';
});
if (getHomePage?.path == '/dashboard/workbench') {
setTimeout(() => {
router.push('/dashboard/workbench');
}, 200);
} else {
const routeF = getMenuListData[0]?.children[0]?.path || getMenuListData[0].path;
setTimeout(() => {
router.push(routeF);
}, 200);
}
// setTimeout(() => {
// console.log('无需修改密码,跳到首页');
// router.push('/dashboard/workbench');
// }, 200);
}
if (from.path === LOGIN_PATH && userInfo.value?.needSetPwd == true) {
// console.log('需要修改密码,跳到修改密码');
setTimeout(() => {
router.push('/system/password');
}, 200);
}
// if (from.path === LOGIN_PATH) {
// const getMenuListData = await getMenuList();
// // getMenuList().then((res) => {
// if (getMenuListData) {
// const getHomePage = getMenuListData.map((f) => {
// if (f?.children) {
// const getFilterPath = f.children.filter((f1) => {
// return f1.path == '/system/password';
// });
// return getFilterPath;
// }
// });
// console.log(getHomePage?.at(-1)[0]?.path);
// // getHomePage?.at(-1)[0]?.path == '/system/password'
// if (userInfo.value?.needSetPwd == false) {
// setTimeout(() => {
// router.push('/dashboard/workbench');
// }, 10);
// }
// if (userInfo.value?.needSetPwd == true && getHomePage?.at(-1)[0]?.path == undefined) {
// setTimeout(() => {
// router.push('/system/password');
// }, 10);
// }
// }
// // });
// }
if (
from.path === LOGIN_PATH &&
to.name === PAGE_NOT_FOUND_ROUTE.name &&
to.fullPath !== (userStore.getUserInfo.homePath || PageEnum.BASE_HOME)
) {
// console.log('404页面', 'res');
// Jump to the 404 page after processing the login
const getMenuListData = await getMenuList();
// getMenuList().then((res) => {
if (getMenuListData) {
const getHomePage = getMenuListData.find((f) => {
return f.path == '/dashboard/workbench';
});
if (getHomePage?.path == '/dashboard/workbench') {
setTimeout(() => {
router.push('/dashboard/workbench');
}, 10);
} else {
const routeF = res[0]?.children[0]?.path || res[0].path;
setTimeout(() => {
router.push(routeF);
}, 10);
}
}
// });
next(userStore.getUserInfo.homePath || PageEnum.BASE_HOME);
return;
}
// get userinfo while last fetch time is empty
// if (userStore.getLastUpdateTime === 0) {
// await userStore.getUserInfoAction();
// }
if (permissionStore.getIsDynamicAddedRoute) {
next();
return;
}
const routes = await permissionStore.buildRoutesAction();
routes.forEach((route) => {
router.addRoute(route as unknown as RouteRecordRaw);
});
router.addRoute(PAGE_NOT_FOUND_ROUTE as unknown as RouteRecordRaw);
permissionStore.setDynamicAddedRoute(true);
if (to.name === PAGE_NOT_FOUND_ROUTE.name) {
// 动态添加路由后,此处应当重定向到fullPath,否则会加载404页面内容
next({ path: to.fullPath, replace: true, query: to.query });
} 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);
}
});
}