vben
2020-11-12 3f78b5aa0cd3e7a6f17d58512ca93ee2905d5e2f
提交 | 用户 | age
2f6253 1 import type { Router } from 'vue-router';
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
67d0ff 9 import { useSetting } from '/@/hooks/core/useSetting';
e23336 10
31e271 11 import { getIsOpenTab, setCurrentTo } from '/@/utils/helper/routeHelper';
b35009 12 import { setTitle } from '/@/utils/browser';
e23336 13 import { AxiosCanceler } from '/@/utils/http/axios/axiosCancel';
2f6253 14
4baf90 15 import { tabStore } from '/@/store/modules/tab';
V 16
b35009 17 const { projectSetting, globSetting } = useSetting();
2f6253 18 export function createGuard(router: Router) {
e83cb0 19   const { openNProgress, closeMessageOnSwitch, removeAllHttpPending } = projectSetting;
N 20   let axiosCanceler: AxiosCanceler | null;
21   if (removeAllHttpPending) {
22     axiosCanceler = new AxiosCanceler();
23   }
3f78b5 24
V 25   createPageLoadingGuard(router);
e9536b 26   router.beforeEach(async (to) => {
4baf90 27     // Determine whether the tab has been opened
31e271 28     const isOpen = getIsOpenTab(to.fullPath);
e9536b 29     to.meta.inTab = isOpen;
4baf90 30
V 31     // Notify routing changes
32     const { fullPath, path, query, params, name, meta } = to;
33     tabStore.commitLastChangeRouteState({
34       fullPath,
35       path,
36       query,
37       params,
38       name,
39       meta,
40     } as any);
41
2f6253 42     try {
e83cb0 43       if (closeMessageOnSwitch) {
N 44         Modal.destroyAll();
45         notification.destroy();
46       }
2f6253 47       // TODO Some special interfaces require long connections
48       // Switching the route will delete the previous request
e83cb0 49       removeAllHttpPending && axiosCanceler!.removeAllPending();
2f6253 50     } catch (error) {
51       console.warn('basic guard error:' + error);
52     }
31e271 53     setCurrentTo(to);
V 54     return true;
2f6253 55   });
b35009 56
V 57   router.afterEach((to) => {
58     // change html title
e23336 59     setTitle(to.meta.title, globSetting.title);
b35009 60   });
V 61
e83cb0 62   openNProgress && createProgressGuard(router);
2f6253 63   createPermissionGuard(router);
64 }