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