初始化
This commit is contained in:
103
frontend/src/routers/index.ts
Normal file
103
frontend/src/routers/index.ts
Normal file
@@ -0,0 +1,103 @@
|
||||
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()
|
||||
console.warn('Route error', error.message)
|
||||
})
|
||||
|
||||
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
|
||||
Reference in New Issue
Block a user