2020-09-28 20:19:10 +08:00
|
|
|
import type { Router, RouteRecordRaw } from 'vue-router';
|
|
|
|
|
|
2021-04-10 19:25:49 +08:00
|
|
|
import { usePermissionStoreWidthOut } 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-04-10 19:25:49 +08:00
|
|
|
import { useUserStoreWidthOut } 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
|
|
|
|
|
|
|
|
const LOGIN_PATH = PageEnum.BASE_LOGIN;
|
|
|
|
|
|
|
|
|
|
const whitePathList: PageEnum[] = [LOGIN_PATH];
|
|
|
|
|
|
|
|
|
|
export function createPermissionGuard(router: Router) {
|
2021-04-10 19:25:49 +08:00
|
|
|
const userStore = useUserStoreWidthOut();
|
|
|
|
|
const permissionStore = usePermissionStoreWidthOut();
|
2020-09-28 20:19: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(PageEnum.BASE_HOME);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Whitelist can be directly entered
|
|
|
|
|
if (whitePathList.includes(to.path as PageEnum)) {
|
|
|
|
|
next();
|
|
|
|
|
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
|
|
|
|
|
// || to.name === FULL_PAGE_NOT_FOUND_ROUTE.name
|
|
|
|
|
) {
|
|
|
|
|
next();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
// 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,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
next(redirectData);
|
2020-09-28 20:19:10 +08:00
|
|
|
return;
|
|
|
|
|
}
|
2021-04-10 19:25:49 +08:00
|
|
|
if (permissionStore.getIsDynamicAddedRoute) {
|
2020-09-28 20:19:10 +08:00
|
|
|
next();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
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
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const redirectPath = (from.query.redirect || to.path) as string;
|
|
|
|
|
const redirect = decodeURIComponent(redirectPath);
|
|
|
|
|
const nextData = to.path === redirect ? { ...to, replace: true } : { path: redirect };
|
2021-04-10 19:25:49 +08:00
|
|
|
permissionStore.setDynamicAddedRoute(true);
|
2020-09-28 20:19:10 +08:00
|
|
|
next(nextData);
|
|
|
|
|
});
|
|
|
|
|
}
|