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 years ago
5
import { getParams } from '/@/utils/helper/routeHelper';
vben
authored
5 years ago
6
7
import { useTransitionSetting } from '/@/hooks/setting/useTransitionSetting';
import { unref } from 'vue';
8
vben
authored
5 years ago
9
const { getOpenPageLoading, getEnableTransition } = useTransitionSetting();
10
11
12
export function createPageLoadingGuard(router: Router) {
let isFirstLoad = true;
router.beforeEach(async (to) => {
vben
authored
5 years ago
13
const { openKeepAlive, multiTabsSetting: { show } = {} } = appStore.getProjectConfig;
14
15
16
if (!userStore.getTokenState) {
return true;
}
vben
authored
5 years ago
17
if (!unref(getEnableTransition) && unref(getOpenPageLoading)) {
18
19
20
appStore.commitPageLoadingState(true);
return true;
}
vben
authored
5 years ago
21
22
23
if (show && openKeepAlive && !isFirstLoad) {
const tabList = tabStore.getTabsState;
vben
authored
5 years ago
24
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
5 years ago
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
5 years ago
36
(!unref(getEnableTransition) && unref(getOpenPageLoading)) ||
vben
authored
5 years ago
37
38
39
40
isFirstLoad ||
to.meta.afterCloseLoading ||
realToPath === realFormPath
) {
41
42
43
setTimeout(() => {
appStore.commitPageLoadingState(false);
}, 110);
44
45
isFirstLoad = false;
}
46
47
48
49
return true;
});
}