1
0

pageLoadingGuard.ts 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import type { Router } from 'vue-router';
  2. import { tabStore } from '/@/store/modules/tab';
  3. import { appStore } from '/@/store/modules/app';
  4. import { userStore } from '/@/store/modules/user';
  5. import { getParams } from '/@/utils/helper/routeHelper';
  6. export function createPageLoadingGuard(router: Router) {
  7. let isFirstLoad = true;
  8. router.beforeEach(async (to) => {
  9. const {
  10. openKeepAlive,
  11. openRouterTransition,
  12. openPageLoading,
  13. multiTabsSetting: { show } = {},
  14. } = appStore.getProjectConfig;
  15. if (!userStore.getTokenState) {
  16. return true;
  17. }
  18. if (!openRouterTransition && openPageLoading) {
  19. appStore.commitPageLoadingState(true);
  20. return true;
  21. }
  22. if (show && openKeepAlive && !isFirstLoad) {
  23. const tabList = tabStore.getTabsState;
  24. const isOpen = tabList.some((tab) => tab.path === to.path);
  25. appStore.setPageLoadingAction(!isOpen);
  26. } else {
  27. appStore.setPageLoadingAction(true);
  28. }
  29. return true;
  30. });
  31. router.afterEach(async (to, from) => {
  32. const { openRouterTransition, openPageLoading } = appStore.getProjectConfig;
  33. const realToPath = to.path.replace(getParams(to), '');
  34. const realFormPath = from.path.replace(getParams(from), '');
  35. if (
  36. (!openRouterTransition && openPageLoading) ||
  37. isFirstLoad ||
  38. to.meta.afterCloseLoading ||
  39. realToPath === realFormPath
  40. ) {
  41. setTimeout(() => {
  42. appStore.commitPageLoadingState(false);
  43. }, 110);
  44. isFirstLoad = false;
  45. }
  46. return true;
  47. });
  48. }