index.ts 4.6 KB

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