index.ts
1.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import { RouteLocationNormalized, Router } from 'vue-router';
import { Modal, notification } from 'ant-design-vue';
import { createProgressGuard } from './progressGuard';
import { createPermissionGuard } from './permissionGuard';
import { createPageLoadingGuard } from './pageLoadingGuard';
import { useGlobSetting, useProjectSetting } from '/@/hooks/setting';
import { setTitle } from '/@/utils/browser';
import { AxiosCanceler } from '/@/utils/http/axios/axiosCancel';
import { useI18n } from '/@/hooks/web/useI18n';
import { REDIRECT_NAME } from '/@/router/constant';
import { setLastChangeTab } from '/@/logics/mitt/tabChange';
const { closeMessageOnSwitch, removeAllHttpPending } = useProjectSetting();
const globSetting = useGlobSetting();
const body = document.body;
const isHash = (href: string) => {
return /^#/.test(href);
};
export function createGuard(router: Router) {
let axiosCanceler: Nullable<AxiosCanceler>;
if (removeAllHttpPending) {
axiosCanceler = new AxiosCanceler();
}
const loadedPageMap = new Map<string, boolean>();
router.beforeEach(async (to) => {
to.meta.loaded = !!loadedPageMap.get(to.path);
// Notify routing changes
setLastChangeTab(to);
try {
if (closeMessageOnSwitch) {
Modal.destroyAll();
notification.destroy();
}
// Switching the route will delete the previous request
removeAllHttpPending && axiosCanceler!.removeAllPending();
} catch (error) {
console.warn('basic guard error:' + error);
}
return true;
});
router.afterEach((to) => {
// scroll top
isHash((to as RouteLocationNormalized & { href: string })?.href) && body.scrollTo(0, 0);
loadedPageMap.set(to.path, true);
const { t } = useI18n();
// change html title
to.name !== REDIRECT_NAME && setTitle(t(to.meta.title), globSetting.title);
});
createPageLoadingGuard(router);
createProgressGuard(router);
createPermissionGuard(router);
}