|
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
|
|
vben
authored
|
9
|
import { useGlobSetting, useProjectSetting } from '/@/hooks/setting';
|
vben
authored
|
10
|
|
vben
authored
|
11
|
import { getIsOpenTab, getRoute } from '/@/router/helper/routeHelper';
|
vben
authored
|
12
|
import { setTitle } from '/@/utils/browser';
|
vben
authored
|
13
|
import { AxiosCanceler } from '/@/utils/http/axios/axiosCancel';
|
|
14
|
|
vben
authored
|
15
|
import { tabStore } from '/@/store/modules/tab';
|
vben
authored
|
16
|
import { useI18n } from '/@/hooks/web/useI18n';
|
vben
authored
|
17
|
import { REDIRECT_NAME } from '/@/router/constant';
|
vben
authored
|
18
|
|
vben
authored
|
19
|
const { closeMessageOnSwitch, removeAllHttpPending } = useProjectSetting();
|
vben
authored
|
20
|
const globSetting = useGlobSetting();
|
vben
authored
|
21
|
|
|
22
|
export function createGuard(router: Router) {
|
vben
authored
|
23
|
let axiosCanceler: Nullable<AxiosCanceler>;
|
nebv
authored
|
24
25
26
|
if (removeAllHttpPending) {
axiosCanceler = new AxiosCanceler();
}
|
vben
authored
|
27
28
|
createPageLoadingGuard(router);
|
vben
authored
|
29
|
router.beforeEach(async (to) => {
|
vben
authored
|
30
|
// Determine whether the tab has been opened
|
vben
authored
|
31
|
const isOpen = getIsOpenTab(to.fullPath);
|
vben
authored
|
32
|
to.meta.inTab = isOpen;
|
vben
authored
|
33
34
|
// Notify routing changes
|
vben
authored
|
35
|
tabStore.commitLastChangeRouteState(getRoute(to));
|
vben
authored
|
36
|
|
|
37
|
try {
|
nebv
authored
|
38
39
40
41
|
if (closeMessageOnSwitch) {
Modal.destroyAll();
notification.destroy();
}
|
|
42
|
// Switching the route will delete the previous request
|
nebv
authored
|
43
|
removeAllHttpPending && axiosCanceler!.removeAllPending();
|
|
44
45
46
|
} catch (error) {
console.warn('basic guard error:' + error);
}
|
vben
authored
|
47
|
return true;
|
|
48
|
});
|
vben
authored
|
49
50
|
router.afterEach((to) => {
|
vben
authored
|
51
|
const { t } = useI18n();
|
vben
authored
|
52
|
// change html title
|
vben
authored
|
53
|
to.name !== REDIRECT_NAME && setTitle(t(to.meta.title), globSetting.title);
|
vben
authored
|
54
|
});
|
vben
authored
|
55
|
createProgressGuard(router);
|
|
56
57
|
createPermissionGuard(router);
}
|