vue-vben-admin/src/router/guard/index.ts

61 lines
1.9 KiB
TypeScript
Raw Normal View History

2020-12-04 21:25:33 +08:00
import { isNavigationFailure, Router } from 'vue-router';
2020-09-28 20:19:10 +08:00
import { Modal, notification } from 'ant-design-vue';
2020-10-30 21:32:05 +08:00
2020-09-28 20:19:10 +08:00
import { createProgressGuard } from './progressGuard';
import { createPermissionGuard } from './permissionGuard';
import { createPageLoadingGuard } from './pageLoadingGuard';
2020-10-30 21:32:05 +08:00
2020-11-23 23:24:13 +08:00
import { useGlobSetting, useProjectSetting } from '/@/hooks/setting';
2020-10-30 21:32:05 +08:00
2020-12-04 21:25:33 +08:00
import { getRoute } from '/@/router/helper/routeHelper';
2020-10-27 22:48:08 +08:00
import { setTitle } from '/@/utils/browser';
2020-10-30 21:32:05 +08:00
import { AxiosCanceler } from '/@/utils/http/axios/axiosCancel';
2020-09-28 20:19:10 +08:00
2020-11-10 21:58:19 +08:00
import { tabStore } from '/@/store/modules/tab';
2020-11-26 21:10:21 +08:00
import { useI18n } from '/@/hooks/web/useI18n';
2020-12-03 21:49:32 +08:00
import { REDIRECT_NAME } from '/@/router/constant';
2020-11-10 21:58:19 +08:00
2020-11-25 21:26:10 +08:00
const { closeMessageOnSwitch, removeAllHttpPending } = useProjectSetting();
2020-11-23 00:35:15 +08:00
const globSetting = useGlobSetting();
2020-12-03 21:49:32 +08:00
2020-09-28 20:19:10 +08:00
export function createGuard(router: Router) {
2020-12-03 21:49:32 +08:00
let axiosCanceler: Nullable<AxiosCanceler>;
if (removeAllHttpPending) {
axiosCanceler = new AxiosCanceler();
}
2020-12-04 21:25:33 +08:00
const loadedPageMap = new Map<string, boolean>();
2020-11-12 23:58:44 +08:00
router.beforeEach(async (to) => {
2020-12-04 21:25:33 +08:00
to.meta.loaded = !!loadedPageMap.get(to.path);
2020-11-10 21:58:19 +08:00
// Notify routing changes
2020-12-03 21:49:32 +08:00
tabStore.commitLastChangeRouteState(getRoute(to));
2020-11-10 21:58:19 +08:00
2020-09-28 20:19:10 +08:00
try {
if (closeMessageOnSwitch) {
Modal.destroyAll();
notification.destroy();
}
2020-09-28 20:19:10 +08:00
// Switching the route will delete the previous request
removeAllHttpPending && axiosCanceler!.removeAllPending();
2020-09-28 20:19:10 +08:00
} catch (error) {
console.warn('basic guard error:' + error);
}
2020-10-14 21:08:07 +08:00
return true;
2020-09-28 20:19:10 +08:00
});
2020-10-27 22:48:08 +08:00
2020-12-04 21:25:33 +08:00
router.afterEach((to, from, failure) => {
loadedPageMap.set(to.path, true);
2020-11-26 21:10:21 +08:00
const { t } = useI18n();
2020-10-27 22:48:08 +08:00
// change html title
2020-12-03 21:49:32 +08:00
to.name !== REDIRECT_NAME && setTitle(t(to.meta.title), globSetting.title);
2020-12-04 21:25:33 +08:00
if (isNavigationFailure(failure)) {
console.error('router navigation failed:', failure);
}
2020-10-27 22:48:08 +08:00
});
2020-12-04 21:25:33 +08:00
createPageLoadingGuard(router);
2020-11-25 00:43:33 +08:00
createProgressGuard(router);
2020-09-28 20:19:10 +08:00
createPermissionGuard(router);
}