2020-09-28 20:19:10 +08:00
|
|
|
|
import type { Router, RouteRecordRaw } from 'vue-router';
|
|
|
|
|
|
|
2021-06-27 23:58:14 +08:00
|
|
|
|
import { usePermissionStoreWithOut } from '/@/store/modules/permission';
|
2020-10-30 21:32:05 +08:00
|
|
|
|
|
2020-09-28 20:19:10 +08:00
|
|
|
|
import { PageEnum } from '/@/enums/pageEnum';
|
2021-06-27 23:58:14 +08:00
|
|
|
|
import { useUserStoreWithOut } from '/@/store/modules/user';
|
2020-10-30 21:32:05 +08:00
|
|
|
|
|
2021-03-17 00:10:16 +08:00
|
|
|
|
import { PAGE_NOT_FOUND_ROUTE } from '/@/router/routes/basic';
|
2020-09-28 20:19:10 +08:00
|
|
|
|
|
2021-07-13 14:10:31 +08:00
|
|
|
|
import { RootRoute } from '/@/router/routes';
|
|
|
|
|
|
|
2020-09-28 20:19:10 +08:00
|
|
|
|
const LOGIN_PATH = PageEnum.BASE_LOGIN;
|
|
|
|
|
|
|
2021-07-13 14:10:31 +08:00
|
|
|
|
const ROOT_PATH = RootRoute.path;
|
|
|
|
|
|
|
2020-09-28 20:19:10 +08:00
|
|
|
|
const whitePathList: PageEnum[] = [LOGIN_PATH];
|
|
|
|
|
|
|
|
|
|
|
|
export function createPermissionGuard(router: Router) {
|
2021-06-27 23:58:14 +08:00
|
|
|
|
const userStore = useUserStoreWithOut();
|
|
|
|
|
|
const permissionStore = usePermissionStoreWithOut();
|
2021-07-14 21:37:10 +08:00
|
|
|
|
router.beforeEach(async (to, from, next) => {
|
|
|
|
|
|
// Jump to the 404 page after processing the login
|
|
|
|
|
|
if (from.path === LOGIN_PATH && to.name === PAGE_NOT_FOUND_ROUTE.name) {
|
|
|
|
|
|
next(userStore.getUserInfo.homePath || PageEnum.BASE_HOME);
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-07-13 14:10:31 +08:00
|
|
|
|
if (
|
|
|
|
|
|
from.path === ROOT_PATH &&
|
|
|
|
|
|
to.path === PageEnum.BASE_HOME &&
|
|
|
|
|
|
userStore.getUserInfo.homePath &&
|
|
|
|
|
|
userStore.getUserInfo.homePath !== PageEnum.BASE_HOME
|
|
|
|
|
|
) {
|
2021-07-14 21:37:10 +08:00
|
|
|
|
next(userStore.getUserInfo.homePath);
|
|
|
|
|
|
return;
|
2020-09-28 20:19:10 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Whitelist can be directly entered
|
|
|
|
|
|
if (whitePathList.includes(to.path as PageEnum)) {
|
2021-07-14 21:37:10 +08:00
|
|
|
|
next();
|
2020-09-28 20:19:10 +08:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-04-10 19:25:49 +08:00
|
|
|
|
const token = userStore.getToken;
|
2020-09-28 20:19:10 +08:00
|
|
|
|
|
|
|
|
|
|
// token does not exist
|
|
|
|
|
|
if (!token) {
|
|
|
|
|
|
// You can access without permission. You need to set the routing meta.ignoreAuth to true
|
2021-06-27 14:11:04 +08:00
|
|
|
|
if (to.meta.ignoreAuth) {
|
2021-07-14 21:37:10 +08:00
|
|
|
|
next();
|
2020-09-28 20:19:10 +08:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
2021-06-27 14:11:04 +08:00
|
|
|
|
|
2020-09-28 20:19:10 +08:00
|
|
|
|
// redirect login page
|
2021-03-26 22:22:58 +08:00
|
|
|
|
const redirectData: { path: string; replace: boolean; query?: Recordable<string> } = {
|
2020-11-10 21:30:06 +08:00
|
|
|
|
path: LOGIN_PATH,
|
|
|
|
|
|
replace: true,
|
|
|
|
|
|
};
|
|
|
|
|
|
if (to.path) {
|
|
|
|
|
|
redirectData.query = {
|
|
|
|
|
|
...redirectData.query,
|
|
|
|
|
|
redirect: to.path,
|
|
|
|
|
|
};
|
|
|
|
|
|
}
|
2021-07-14 21:37:10 +08:00
|
|
|
|
next(redirectData);
|
|
|
|
|
|
return;
|
2020-09-28 20:19:10 +08:00
|
|
|
|
}
|
2021-06-27 14:11:04 +08:00
|
|
|
|
|
2021-04-10 19:25:49 +08:00
|
|
|
|
if (permissionStore.getIsDynamicAddedRoute) {
|
2021-07-14 21:37:10 +08:00
|
|
|
|
next();
|
2020-09-28 20:19:10 +08:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
2021-06-27 14:11:04 +08:00
|
|
|
|
|
2020-09-28 20:19:10 +08:00
|
|
|
|
const routes = await permissionStore.buildRoutesAction();
|
2021-02-05 01:07:36 +08:00
|
|
|
|
|
2020-09-28 20:19:10 +08:00
|
|
|
|
routes.forEach((route) => {
|
2021-06-09 22:36:30 +08:00
|
|
|
|
router.addRoute(route as unknown as RouteRecordRaw);
|
2020-09-28 20:19:10 +08:00
|
|
|
|
});
|
|
|
|
|
|
|
2021-07-14 21:37:10 +08:00
|
|
|
|
router.addRoute(PAGE_NOT_FOUND_ROUTE as unknown as RouteRecordRaw);
|
|
|
|
|
|
|
2021-04-10 19:25:49 +08:00
|
|
|
|
permissionStore.setDynamicAddedRoute(true);
|
2021-07-14 21:37:10 +08:00
|
|
|
|
|
|
|
|
|
|
if (to.name === PAGE_NOT_FOUND_ROUTE.name) {
|
|
|
|
|
|
// 动态添加路由后,此处应当重定向到fullPath,否则会加载404页面内容
|
|
|
|
|
|
next({ path: to.fullPath, replace: true });
|
|
|
|
|
|
} 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);
|
|
|
|
|
|
}
|
2020-09-28 20:19:10 +08:00
|
|
|
|
});
|
|
|
|
|
|
}
|