pageLoadingGuard.ts 926 字节
Newer Older
陈文彬 已提交
1
import type { Router } from 'vue-router';
V
Vben 已提交
2 3
import { useAppStoreWidthOut } from '/@/store/modules/app';
import { useUserStoreWidthOut } from '/@/store/modules/user';
V
vben 已提交
4 5
import { useTransitionSetting } from '/@/hooks/setting/useTransitionSetting';
import { unref } from 'vue';
陈文彬 已提交
6 7

export function createPageLoadingGuard(router: Router) {
V
Vben 已提交
8 9 10
  const userStore = useUserStoreWidthOut();
  const appStore = useAppStoreWidthOut();
  const { getOpenPageLoading } = useTransitionSetting();
陈文彬 已提交
11
  router.beforeEach(async (to) => {
V
Vben 已提交
12
    if (!userStore.getToken) {
陈文彬 已提交
13 14
      return true;
    }
V
vben 已提交
15
    if (to.meta.loaded) {
陈文彬 已提交
16 17
      return true;
    }
V
vben 已提交
18

V
vben 已提交
19
    if (unref(getOpenPageLoading)) {
陈文彬 已提交
20
      appStore.setPageLoadingAction(true);
V
vben 已提交
21
      return true;
陈文彬 已提交
22
    }
V
vben 已提交
23

陈文彬 已提交
24 25
    return true;
  });
V
vben 已提交
26 27
  router.afterEach(async () => {
    if (unref(getOpenPageLoading)) {
陈文彬 已提交
28
      setTimeout(() => {
V
Vben 已提交
29
        appStore.setPageLoading(false);
V
Vben 已提交
30
      }, 220);
陈文彬 已提交
31 32 33 34
    }
    return true;
  });
}