2026-04-13 17:32:58 +08:00
|
|
|
import { createRouter, createWebHashHistory, createWebHistory } from 'vue-router'
|
|
|
|
|
import { useUserStore } from '@/stores/modules/user'
|
|
|
|
|
import { useAuthStore } from '@/stores/modules/auth'
|
|
|
|
|
import { useHomeStore } from '@/stores/modules/home'
|
|
|
|
|
import { LOGIN_URL, ROUTER_WHITE_LIST } from '@/config'
|
|
|
|
|
import { initDynamicRouter } from '@/routers/modules/dynamicRouter'
|
|
|
|
|
import { staticRouter } from '@/routers/modules/staticRouter'
|
|
|
|
|
import NProgress from '@/config/nprogress'
|
|
|
|
|
import { createHomeMenuMap, getHomeValidPaths, HOME_EXCLUDED_PATHS } from '@/utils/home'
|
|
|
|
|
|
|
|
|
|
const WHITE_LIST_SET = new Set(ROUTER_WHITE_LIST)
|
|
|
|
|
const mode = import.meta.env.VITE_ROUTER_MODE
|
|
|
|
|
|
|
|
|
|
const routerMode = {
|
|
|
|
|
hash: () => createWebHashHistory(),
|
|
|
|
|
history: () => createWebHistory()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const router = createRouter({
|
|
|
|
|
history: routerMode[mode]?.() || createWebHashHistory(),
|
|
|
|
|
routes: [...staticRouter],
|
|
|
|
|
strict: false,
|
|
|
|
|
scrollBehavior: () => ({ left: 0, top: 0 })
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
const syncHomeStateWithMenus = () => {
|
|
|
|
|
const authStore = useAuthStore()
|
|
|
|
|
const homeStore = useHomeStore()
|
|
|
|
|
homeStore.syncWithMenus(getHomeValidPaths(authStore.flatMenuListGet))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
router.beforeEach(async (to, from, next) => {
|
|
|
|
|
const userStore = useUserStore()
|
|
|
|
|
const authStore = useAuthStore()
|
|
|
|
|
|
|
|
|
|
NProgress.start()
|
|
|
|
|
|
|
|
|
|
const title = import.meta.env.VITE_GLOB_APP_TITLE
|
|
|
|
|
document.title = to.meta.title ? `${to.meta.title} - ${title}` : title
|
|
|
|
|
|
|
|
|
|
if (to.path.toLocaleLowerCase() === LOGIN_URL) {
|
|
|
|
|
if (userStore.accessToken) {
|
|
|
|
|
return next('/')
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
resetRouter()
|
|
|
|
|
return next()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (WHITE_LIST_SET.has(to.path)) return next()
|
|
|
|
|
|
|
|
|
|
if (!userStore.accessToken) return next({ path: LOGIN_URL, replace: true })
|
|
|
|
|
|
|
|
|
|
if (!authStore.authMenuListGet.length) {
|
|
|
|
|
try {
|
|
|
|
|
await initDynamicRouter()
|
|
|
|
|
syncHomeStateWithMenus()
|
|
|
|
|
return next({ ...to, replace: true })
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error('Dynamic router initialization failed', error)
|
|
|
|
|
await userStore.logout()
|
|
|
|
|
return next({ path: LOGIN_URL, replace: true })
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
syncHomeStateWithMenus()
|
|
|
|
|
await authStore.setRouteName(to.name as string)
|
|
|
|
|
|
|
|
|
|
if (!authStore.activateInfoLoadedGet) {
|
|
|
|
|
await authStore.setActivateInfo()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
next()
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
export const resetRouter = () => {
|
|
|
|
|
const authStore = useAuthStore()
|
|
|
|
|
authStore.flatMenuListGet.forEach(route => {
|
|
|
|
|
const { name } = route
|
|
|
|
|
if (name && router.hasRoute(name)) router.removeRoute(name)
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
router.onError(error => {
|
|
|
|
|
NProgress.done()
|
2026-04-16 08:11:38 +08:00
|
|
|
console.error('[router] route error', {
|
|
|
|
|
message: error.message,
|
|
|
|
|
currentPath: router.currentRoute.value.fullPath
|
|
|
|
|
})
|
2026-04-13 17:32:58 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
router.afterEach(to => {
|
|
|
|
|
NProgress.done()
|
|
|
|
|
|
|
|
|
|
if (HOME_EXCLUDED_PATHS.has(to.path)) return
|
|
|
|
|
|
|
|
|
|
const authStore = useAuthStore()
|
|
|
|
|
const homeStore = useHomeStore()
|
|
|
|
|
const menuMap = createHomeMenuMap(authStore.flatMenuListGet)
|
|
|
|
|
|
|
|
|
|
if (!menuMap[to.path]) return
|
|
|
|
|
|
|
|
|
|
homeStore.addRecentVisited(to.path)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
export default router
|