|
1
2
3
|
import type { Router } from 'vue-router';
import { Modal, notification } from 'ant-design-vue';
|
vben
authored
|
4
|
|
|
5
6
7
|
import { createProgressGuard } from './progressGuard';
import { createPermissionGuard } from './permissionGuard';
import { createPageLoadingGuard } from './pageLoadingGuard';
|
vben
authored
|
8
|
|
|
9
|
import { useSetting } from '/@/hooks/core/useSetting';
|
vben
authored
|
10
|
|
vben
authored
|
11
|
import { getIsOpenTab, setCurrentTo } from '/@/utils/helper/routeHelper';
|
vben
authored
|
12
|
import { setTitle } from '/@/utils/browser';
|
vben
authored
|
13
|
import { AxiosCanceler } from '/@/utils/http/axios/axiosCancel';
|
|
14
|
|
vben
authored
|
15
|
const { projectSetting, globSetting } = useSetting();
|
|
16
|
export function createGuard(router: Router) {
|
nebv
authored
|
17
18
19
20
21
|
const { openNProgress, closeMessageOnSwitch, removeAllHttpPending } = projectSetting;
let axiosCanceler: AxiosCanceler | null;
if (removeAllHttpPending) {
axiosCanceler = new AxiosCanceler();
}
|
vben
authored
|
22
|
router.beforeEach(async (to) => {
|
vben
authored
|
23
|
const isOpen = getIsOpenTab(to.fullPath);
|
vben
authored
|
24
|
to.meta.inTab = isOpen;
|
|
25
|
try {
|
nebv
authored
|
26
27
28
29
|
if (closeMessageOnSwitch) {
Modal.destroyAll();
notification.destroy();
}
|
|
30
31
|
// TODO Some special interfaces require long connections
// Switching the route will delete the previous request
|
nebv
authored
|
32
|
removeAllHttpPending && axiosCanceler!.removeAllPending();
|
|
33
34
35
|
} catch (error) {
console.warn('basic guard error:' + error);
}
|
vben
authored
|
36
37
|
setCurrentTo(to);
return true;
|
|
38
|
});
|
vben
authored
|
39
40
41
|
router.afterEach((to) => {
// change html title
|
vben
authored
|
42
|
setTitle(to.meta.title, globSetting.title);
|
vben
authored
|
43
44
|
});
|
nebv
authored
|
45
|
openNProgress && createProgressGuard(router);
|
|
46
47
48
|
createPermissionGuard(router);
createPageLoadingGuard(router);
}
|