2020-12-05 09:30:51 +08:00
|
|
|
import { RouteLocationNormalized, 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-12-04 21:52:29 +08:00
|
|
|
const body = document.body;
|
|
|
|
|
|
|
|
|
|
const isHash = (href: string) => {
|
|
|
|
|
return /^#/.test(href);
|
|
|
|
|
};
|
|
|
|
|
|
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>;
|
2020-10-13 01:40:18 +08:00
|
|
|
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
|
|
|
|
2020-10-11 11:11:05 +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 {
|
2020-10-13 01:40:18 +08:00
|
|
|
if (closeMessageOnSwitch) {
|
|
|
|
|
Modal.destroyAll();
|
|
|
|
|
notification.destroy();
|
|
|
|
|
}
|
2020-09-28 20:19:10 +08:00
|
|
|
// Switching the route will delete the previous request
|
2020-10-13 01:40:18 +08:00
|
|
|
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-05 09:30:51 +08:00
|
|
|
router.afterEach((to) => {
|
2020-12-04 21:52:29 +08:00
|
|
|
// scroll top
|
|
|
|
|
isHash((to as RouteLocationNormalized & { href: string })?.href) && body.scrollTo(0, 0);
|
|
|
|
|
|
2020-12-04 21:25:33 +08:00
|
|
|
loadedPageMap.set(to.path, true);
|
2020-12-04 21:52:29 +08:00
|
|
|
|
2020-11-26 21:10:21 +08:00
|
|
|
const { t } = useI18n();
|
2020-12-04 21:52:29 +08:00
|
|
|
|
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-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);
|
|
|
|
|
}
|