index.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. import type { Router, RouteLocationNormalized } from 'vue-router';
  2. import { useAppStoreWithOut } from '/@/store/modules/app';
  3. import { useUserStoreWithOut } from '/@/store/modules/user';
  4. import { useVentStore } from '/@/store/modules/vent';
  5. import { useTransitionSetting } from '/@/hooks/setting/useTransitionSetting';
  6. import { AxiosCanceler } from '/@/utils/http/axios/axiosCancel';
  7. import { Modal, notification } from 'ant-design-vue';
  8. import { warn } from '/@/utils/log';
  9. import { unref } from 'vue';
  10. import { setRouteChange } from '/@/logics/mitt/routeChange';
  11. import { createPermissionGuard } from './permissionGuard';
  12. import { createStateGuard } from './stateGuard';
  13. import nProgress from 'nprogress';
  14. import projectSetting from '/@/settings/projectSetting';
  15. import { createParamMenuGuard } from './paramMenuGuard';
  16. import { RootRoute } from '/@/router/routes';
  17. import { useGlobSetting } from '/@/hooks/setting';
  18. import { PageEnum } from '/@/enums/pageEnum';
  19. import { unmountMicroApps } from '/@/qiankun';
  20. // Don't change the order of creation
  21. export function setupRouterGuard(router: Router) {
  22. createPageGuard(router);
  23. createPageLoadingGuard(router);
  24. createHttpGuard(router);
  25. createScrollGuard(router);
  26. createMessageGuard(router);
  27. createProgressGuard(router);
  28. createPermissionGuard(router);
  29. createParamMenuGuard(router); // must after createPermissionGuard (menu has been built.)
  30. createStateGuard(router);
  31. }
  32. const glob = useGlobSetting();
  33. RootRoute.redirect = glob.homePath || PageEnum.BASE_HOME;
  34. /**
  35. * Hooks for handling page state
  36. */
  37. function createPageGuard(router: Router) {
  38. const loadedPageMap = new Map<string, boolean>();
  39. router.beforeEach(async (to) => {
  40. // if (!to.path.startsWith('/micro-vent-3dModal')) {
  41. // unmountMicroApps(['/micro-vent-3dModal']);
  42. // }
  43. // if (!to.path.startsWith('/micro-need-air')) {
  44. // unmountMicroApps(['/micro-need-air']);
  45. // }
  46. // The page has already been loaded, it will be faster to open it again, you don’t need to do loading and other processing
  47. to.meta.loaded = !!loadedPageMap.get(to.path);
  48. // Notify routing changes
  49. setRouteChange(to);
  50. return true;
  51. });
  52. router.afterEach((to) => {
  53. loadedPageMap.set(to.path, true);
  54. });
  55. }
  56. // Used to handle page loading status
  57. function createPageLoadingGuard(router: Router) {
  58. const ventStore = useVentStore();
  59. const userStore = useUserStoreWithOut();
  60. const appStore = useAppStoreWithOut();
  61. const { getOpenPageLoading } = useTransitionSetting();
  62. router.beforeEach(async (to, from) => {
  63. if (!userStore.getToken) {
  64. return true;
  65. }
  66. if (to.meta.loaded) {
  67. return true;
  68. }
  69. if (!ventStore.allTableHeaderColumns) await ventStore.setAllTableHeaderColumns();
  70. if (unref(getOpenPageLoading)) {
  71. appStore.setPageLoadingAction(true);
  72. return true;
  73. }
  74. return true;
  75. });
  76. router.afterEach(async (to, from) => {
  77. if (unref(getOpenPageLoading)) {
  78. // TODO Looking for a better way
  79. // The timer simulates the loading time to prevent flashing too fast,
  80. setTimeout(() => {
  81. appStore.setPageLoading(false);
  82. }, 220);
  83. }
  84. return true;
  85. });
  86. }
  87. /**
  88. * The interface used to close the current page to complete the request when the route is switched
  89. * @param router
  90. */
  91. function createHttpGuard(router: Router) {
  92. const { removeAllHttpPending } = projectSetting;
  93. let axiosCanceler: Nullable<AxiosCanceler>;
  94. if (removeAllHttpPending) {
  95. axiosCanceler = new AxiosCanceler();
  96. }
  97. router.beforeEach(async () => {
  98. // Switching the route will delete the previous request
  99. axiosCanceler?.removeAllPending();
  100. return true;
  101. });
  102. }
  103. // Routing switch back to the top
  104. function createScrollGuard(router: Router) {
  105. const isHash = (href: string) => {
  106. return /^#/.test(href);
  107. };
  108. const body = document.body;
  109. router.afterEach(async (to) => {
  110. // scroll top
  111. isHash((to as RouteLocationNormalized & { href: string })?.href) && body.scrollTo(0, 0);
  112. return true;
  113. });
  114. }
  115. /**
  116. * Used to close the message instance when the route is switched
  117. * @param router
  118. */
  119. export function createMessageGuard(router: Router) {
  120. const { closeMessageOnSwitch } = projectSetting;
  121. router.beforeEach(async () => {
  122. try {
  123. if (closeMessageOnSwitch) {
  124. Modal.destroyAll();
  125. notification.destroy();
  126. }
  127. } catch (error) {
  128. warn('message guard error:' + error);
  129. }
  130. return true;
  131. });
  132. }
  133. export function createProgressGuard(router: Router) {
  134. const { getOpenNProgress } = useTransitionSetting();
  135. router.beforeEach(async (to) => {
  136. if (to.meta.loaded) {
  137. return true;
  138. }
  139. unref(getOpenNProgress) && nProgress.start();
  140. return true;
  141. });
  142. router.afterEach(async () => {
  143. unref(getOpenNProgress) && nProgress.done();
  144. return true;
  145. });
  146. }