Blame view

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