vue-vben-admin/src/router/guard/permissionGuard.ts

95 lines
2.7 KiB
TypeScript
Raw Normal View History

2020-09-28 20:19:10 +08:00
import type { Router, RouteRecordRaw } from 'vue-router';
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';
import { useUserStoreWithOut } from '/@/store/modules/user';
2020-10-30 21:32:05 +08:00
import { PAGE_NOT_FOUND_ROUTE } from '/@/router/routes/basic';
2020-09-28 20:19:10 +08:00
import { RootRoute } from '/@/router/routes';
2020-09-28 20:19:10 +08:00
const LOGIN_PATH = PageEnum.BASE_LOGIN;
const ROOT_PATH = RootRoute.path;
2020-09-28 20:19:10 +08:00
const whitePathList: PageEnum[] = [LOGIN_PATH];
export function createPermissionGuard(router: Router) {
const userStore = useUserStoreWithOut();
const permissionStore = usePermissionStoreWithOut();
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;
}
if (
from.path === ROOT_PATH &&
to.path === PageEnum.BASE_HOME &&
userStore.getUserInfo.homePath &&
userStore.getUserInfo.homePath !== PageEnum.BASE_HOME
) {
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)) {
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
if (to.meta.ignoreAuth) {
next();
2020-09-28 20:19:10 +08:00
return;
}
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> } = {
path: LOGIN_PATH,
replace: true,
};
if (to.path) {
redirectData.query = {
...redirectData.query,
redirect: to.path,
};
}
next(redirectData);
return;
2020-09-28 20:19:10 +08:00
}
2021-04-10 19:25:49 +08:00
if (permissionStore.getIsDynamicAddedRoute) {
next();
2020-09-28 20:19:10 +08:00
return;
}
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
});
router.addRoute(PAGE_NOT_FOUND_ROUTE as unknown as RouteRecordRaw);
2021-04-10 19:25:49 +08:00
permissionStore.setDynamicAddedRoute(true);
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
});
}