|
|
@ -1,64 +1,20 @@ |
|
|
import { RouteLocationNormalized, Router } from 'vue-router'; |
|
|
import { Router } from 'vue-router'; |
|
|
|
|
|
|
|
|
import { Modal, notification } from 'ant-design-vue'; |
|
|
|
|
|
|
|
|
|
|
|
import { createProgressGuard } from './progressGuard'; |
|
|
import { createProgressGuard } from './progressGuard'; |
|
|
import { createPermissionGuard } from './permissionGuard'; |
|
|
import { createPermissionGuard } from './permissionGuard'; |
|
|
import { createPageLoadingGuard } from './pageLoadingGuard'; |
|
|
import { createPageLoadingGuard } from './pageLoadingGuard'; |
|
|
|
|
|
import { createTitleGuard } from './titleGuard'; |
|
|
import { useGlobSetting, useProjectSetting } from '/@/hooks/setting'; |
|
|
import { createMessageGuard } from './messageGuard'; |
|
|
|
|
|
import { createScrollGuard } from './scrollGuard'; |
|
|
import { setTitle } from '/@/utils/browser'; |
|
|
import { createHttpGuard } from './httpGuard'; |
|
|
import { AxiosCanceler } from '/@/utils/http/axios/axiosCancel'; |
|
|
import { createPageGuard } from './pageGuard'; |
|
|
|
|
|
|
|
|
import { useI18n } from '/@/hooks/web/useI18n'; |
|
|
|
|
|
import { REDIRECT_NAME } from '/@/router/constant'; |
|
|
|
|
|
import { setLastChangeTab } from '/@/logics/mitt/tabChange'; |
|
|
|
|
|
|
|
|
|
|
|
const { closeMessageOnSwitch, removeAllHttpPending } = useProjectSetting(); |
|
|
|
|
|
const globSetting = useGlobSetting(); |
|
|
|
|
|
|
|
|
|
|
|
const body = document.body; |
|
|
|
|
|
|
|
|
|
|
|
const isHash = (href: string) => { |
|
|
|
|
|
return /^#/.test(href); |
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
export function createGuard(router: Router) { |
|
|
export function createGuard(router: Router) { |
|
|
let axiosCanceler: Nullable<AxiosCanceler>; |
|
|
createPageGuard(router); |
|
|
if (removeAllHttpPending) { |
|
|
createHttpGuard(router); |
|
|
axiosCanceler = new AxiosCanceler(); |
|
|
createScrollGuard(router); |
|
|
} |
|
|
createMessageGuard(router); |
|
|
const loadedPageMap = new Map<string, boolean>(); |
|
|
createTitleGuard(router); |
|
|
|
|
|
|
|
|
router.beforeEach(async (to) => { |
|
|
|
|
|
to.meta.loaded = !!loadedPageMap.get(to.path); |
|
|
|
|
|
// Notify routing changes
|
|
|
|
|
|
setLastChangeTab(to); |
|
|
|
|
|
try { |
|
|
|
|
|
if (closeMessageOnSwitch) { |
|
|
|
|
|
Modal.destroyAll(); |
|
|
|
|
|
notification.destroy(); |
|
|
|
|
|
} |
|
|
|
|
|
// Switching the route will delete the previous request
|
|
|
|
|
|
removeAllHttpPending && axiosCanceler!.removeAllPending(); |
|
|
|
|
|
} catch (error) { |
|
|
|
|
|
console.warn('basic guard error:' + error); |
|
|
|
|
|
} |
|
|
|
|
|
return true; |
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
router.afterEach((to) => { |
|
|
|
|
|
// scroll top
|
|
|
|
|
|
isHash((to as RouteLocationNormalized & { href: string })?.href) && body.scrollTo(0, 0); |
|
|
|
|
|
|
|
|
|
|
|
loadedPageMap.set(to.path, true); |
|
|
|
|
|
|
|
|
|
|
|
const { t } = useI18n(); |
|
|
|
|
|
|
|
|
|
|
|
// change html title
|
|
|
|
|
|
to.name !== REDIRECT_NAME && setTitle(t(to.meta.title), globSetting.title); |
|
|
|
|
|
}); |
|
|
|
|
|
createPageLoadingGuard(router); |
|
|
createPageLoadingGuard(router); |
|
|
createProgressGuard(router); |
|
|
createProgressGuard(router); |
|
|
createPermissionGuard(router); |
|
|
createPermissionGuard(router); |
|
|
|