pageLoadingGuard.ts 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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. import { useTransitionSetting } from '/@/hooks/setting/useTransitionSetting';
  7. import { unref } from 'vue';
  8. const { getOpenPageLoading, getEnableTransition } = useTransitionSetting();
  9. export function createPageLoadingGuard(router: Router) {
  10. let isFirstLoad = true;
  11. router.beforeEach(async (to) => {
  12. const { openKeepAlive, multiTabsSetting: { show } = {} } = appStore.getProjectConfig;
  13. if (!userStore.getTokenState) {
  14. return true;
  15. }
  16. if (!unref(getEnableTransition) && unref(getOpenPageLoading)) {
  17. appStore.commitPageLoadingState(true);
  18. return true;
  19. }
  20. if (show && openKeepAlive && !isFirstLoad) {
  21. const tabList = tabStore.getTabsState;
  22. const isOpen = tabList.some((tab) => tab.path === to.path);
  23. appStore.setPageLoadingAction(!isOpen);
  24. } else {
  25. appStore.setPageLoadingAction(true);
  26. }
  27. return true;
  28. });
  29. router.afterEach(async (to, from) => {
  30. const realToPath = to.path.replace(getParams(to), '');
  31. const realFormPath = from.path.replace(getParams(from), '');
  32. if (
  33. (!unref(getEnableTransition) && unref(getOpenPageLoading)) ||
  34. isFirstLoad ||
  35. to.meta.afterCloseLoading ||
  36. realToPath === realFormPath
  37. ) {
  38. setTimeout(() => {
  39. appStore.commitPageLoadingState(false);
  40. }, 110);
  41. isFirstLoad = false;
  42. }
  43. return true;
  44. });
  45. }