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