router-guards.ts 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. import type { RouteRecordRaw } from 'vue-router';
  2. import { isNavigationFailure, Router } from 'vue-router';
  3. import { useUserStoreWidthOut } from '@/store/modules/user';
  4. import { useAsyncRouteStoreWidthOut } from '@/store/modules/asyncRoute';
  5. // import { downloadJsonStoreWidthOut } from '@/store/modules/downloadJson';
  6. import { ACCESS_TOKEN } from '@/store/mutation-types';
  7. import { storage } from '@/utils/Storage';
  8. import { PageEnum } from '@/enums/pageEnum';
  9. import { ErrorPageRoute } from '@/router/base';
  10. import { getBaseLoginUrl } from '@/utils/env';
  11. // const LOGIN_PATH = PageEnum.BASE_LOGIN;
  12. // const FEISHU_LOGIN_PATH = PageEnum.BASE_FEISHU_LOGIN;
  13. // const SYSTEM_SELECT_PATH = PageEnum.SYSTEM_SELECT_PAGE;
  14. const whitePathList = [
  15. // LOGIN_PATH,
  16. // FEISHU_LOGIN_PATH,
  17. // SYSTEM_SELECT_PATH
  18. ]; // no redirect whitelist
  19. export function createRouterGuards(router: Router) {
  20. const userStore = useUserStoreWidthOut();
  21. const asyncRouteStore = useAsyncRouteStoreWidthOut();
  22. // const downloadJsonStore = downloadJsonStoreWidthOut();
  23. router.beforeEach(async (to, from, next) => {
  24. const Loading = window['$loading'] || null;
  25. Loading && Loading.start();
  26. // if (from.path === LOGIN_PATH && to.name === 'errorPage') {
  27. // next(PageEnum.BASE_HOME);
  28. // return;
  29. // }
  30. console.log('to.path:', to.path);
  31. // Whitelist can be directly entered
  32. if (whitePathList.includes(to.path as PageEnum)) {
  33. next();
  34. return;
  35. }
  36. const token = storage.get(ACCESS_TOKEN);
  37. if (!token) {
  38. window.open(getBaseLoginUrl(), '_self');
  39. next(PageEnum.BASE_LOGIN);
  40. return;
  41. // You can access without permissions. You need to set the routing meta.ignoreAuth to true
  42. if (to.meta.ignoreAuth) {
  43. next();
  44. return;
  45. }
  46. // redirect login page
  47. const redirectData: { path: string; replace: boolean; query?: Recordable<string> } = {
  48. // path: LOGIN_PATH,
  49. path: PageEnum.BASE_HOME,
  50. replace: true,
  51. };
  52. if (to.path) {
  53. redirectData.query = {
  54. ...redirectData.query,
  55. redirect: to.path,
  56. };
  57. }
  58. next(redirectData);
  59. return;
  60. }
  61. if (asyncRouteStore.getIsDynamicAddedRoute) {
  62. next();
  63. return;
  64. }
  65. const userInfo = await userStore.GetInfo();
  66. await userStore.GetConfig();
  67. const routes = await asyncRouteStore.generateRoutes(userInfo);
  68. // @ts-ignore
  69. // downloadJsonStore.downloadALL(config.json_version);
  70. routes.forEach((item) => {
  71. router.addRoute(item as unknown as RouteRecordRaw);
  72. });
  73. const isErrorPage = router.getRoutes().findIndex((item) => item.name === ErrorPageRoute.name);
  74. if (isErrorPage === -1) {
  75. router.addRoute(ErrorPageRoute as unknown as RouteRecordRaw);
  76. }
  77. const redirectPath = (from.query.redirect || to.path) as string;
  78. const redirect = decodeURIComponent(redirectPath);
  79. const nextData = to.path === redirect ? { ...to, replace: true } : { path: redirect };
  80. asyncRouteStore.setDynamicAddedRoute(true);
  81. next(nextData);
  82. Loading && Loading.finish();
  83. });
  84. router.afterEach((to, _, failure) => {
  85. document.title = (to?.meta?.title as string) || document.title;
  86. if (isNavigationFailure(failure)) {
  87. //console.log('failed navigation', failure)
  88. }
  89. const asyncRouteStore = useAsyncRouteStoreWidthOut();
  90. // 在这里设置需要缓存的组件名称
  91. const keepAliveComponents = asyncRouteStore.keepAliveComponents;
  92. const currentComName: any = to.matched.find((item) => item.name == to.name)?.name;
  93. if (currentComName && !keepAliveComponents.includes(currentComName) && to.meta?.keepAlive) {
  94. // 需要缓存的组件
  95. keepAliveComponents.push(currentComName);
  96. } else if (!to.meta?.keepAlive || to.name == 'Redirect') {
  97. // 不需要缓存的组件
  98. const index = asyncRouteStore.keepAliveComponents.findIndex((name) => name == currentComName);
  99. if (index != -1) {
  100. keepAliveComponents.splice(index, 1);
  101. }
  102. }
  103. asyncRouteStore.setKeepAliveComponents(keepAliveComponents);
  104. const Loading = window['$loading'] || null;
  105. Loading && Loading.finish();
  106. });
  107. router.onError((error) => {
  108. console.log(error, '路由错误');
  109. });
  110. }