Blame view

src/router/guard/pageLoadingGuard.ts 926 Bytes
陈文彬 authored
1
import type { Router } from 'vue-router';
Vben authored
2
3
import { useAppStoreWidthOut } from '/@/store/modules/app';
import { useUserStoreWidthOut } from '/@/store/modules/user';
vben authored
4
5
import { useTransitionSetting } from '/@/hooks/setting/useTransitionSetting';
import { unref } from 'vue';
陈文彬 authored
6
7

export function createPageLoadingGuard(router: Router) {
Vben authored
8
9
10
  const userStore = useUserStoreWidthOut();
  const appStore = useAppStoreWidthOut();
  const { getOpenPageLoading } = useTransitionSetting();
陈文彬 authored
11
  router.beforeEach(async (to) => {
Vben authored
12
    if (!userStore.getToken) {
陈文彬 authored
13
14
      return true;
    }
vben authored
15
    if (to.meta.loaded) {
陈文彬 authored
16
17
      return true;
    }
vben authored
18
vben authored
19
    if (unref(getOpenPageLoading)) {
陈文彬 authored
20
      appStore.setPageLoadingAction(true);
vben authored
21
      return true;
陈文彬 authored
22
    }
vben authored
23
陈文彬 authored
24
25
    return true;
  });
vben authored
26
27
  router.afterEach(async () => {
    if (unref(getOpenPageLoading)) {
28
      setTimeout(() => {
Vben authored
29
        appStore.setPageLoading(false);
30
      }, 220);
陈文彬 authored
31
32
33
34
    }
    return true;
  });
}