permissionGuard.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import type { Router, RouteRecordRaw } from 'vue-router';
  2. import { usePermissionStoreWithOut } from '/@/store/modules/permission';
  3. import { PageEnum } from '/@/enums/pageEnum';
  4. import { useUserStoreWithOut } from '/@/store/modules/user';
  5. import { PAGE_NOT_FOUND_ROUTE } from '/@/router/routes/basic';
  6. import { RootRoute } from '/@/router/routes';
  7. const LOGIN_PATH = PageEnum.BASE_LOGIN;
  8. const ROOT_PATH = RootRoute.path;
  9. const whitePathList: PageEnum[] = [LOGIN_PATH];
  10. export function createPermissionGuard(router: Router) {
  11. const userStore = useUserStoreWithOut();
  12. const permissionStore = usePermissionStoreWithOut();
  13. router.beforeEach(async (to, from, next) => {
  14. if (
  15. from.path === ROOT_PATH &&
  16. to.path === PageEnum.BASE_HOME &&
  17. userStore.getUserInfo.homePath &&
  18. userStore.getUserInfo.homePath !== PageEnum.BASE_HOME
  19. ) {
  20. next(userStore.getUserInfo.homePath);
  21. return;
  22. }
  23. // Whitelist can be directly entered
  24. if (whitePathList.includes(to.path as PageEnum)) {
  25. next();
  26. return;
  27. }
  28. const token = userStore.getToken;
  29. // token does not exist
  30. if (!token) {
  31. // You can access without permission. You need to set the routing meta.ignoreAuth to true
  32. if (to.meta.ignoreAuth) {
  33. next();
  34. return;
  35. }
  36. // redirect login page
  37. const redirectData: { path: string; replace: boolean; query?: Recordable<string> } = {
  38. path: LOGIN_PATH,
  39. replace: true,
  40. };
  41. if (to.path) {
  42. redirectData.query = {
  43. ...redirectData.query,
  44. redirect: to.path,
  45. };
  46. }
  47. next(redirectData);
  48. return;
  49. }
  50. // Jump to the 404 page after processing the login
  51. if (
  52. from.path === LOGIN_PATH &&
  53. to.name === PAGE_NOT_FOUND_ROUTE.name &&
  54. to.fullPath !== (userStore.getUserInfo.homePath || PageEnum.BASE_HOME)
  55. ) {
  56. next(userStore.getUserInfo.homePath || PageEnum.BASE_HOME);
  57. console.log({ from, to });
  58. return;
  59. }
  60. if (permissionStore.getIsDynamicAddedRoute) {
  61. next();
  62. return;
  63. }
  64. const routes = await permissionStore.buildRoutesAction();
  65. routes.forEach((route) => {
  66. router.addRoute(route as unknown as RouteRecordRaw);
  67. });
  68. router.addRoute(PAGE_NOT_FOUND_ROUTE as unknown as RouteRecordRaw);
  69. permissionStore.setDynamicAddedRoute(true);
  70. if (to.name === PAGE_NOT_FOUND_ROUTE.name) {
  71. // 动态添加路由后,此处应当重定向到fullPath,否则会加载404页面内容
  72. next({ path: to.fullPath, replace: true });
  73. } else {
  74. const redirectPath = (from.query.redirect || to.path) as string;
  75. const redirect = decodeURIComponent(redirectPath);
  76. const nextData = to.path === redirect ? { ...to, replace: true } : { path: redirect };
  77. next(nextData);
  78. }
  79. });
  80. }