Files
front/src/router/guard/permission.ts

82 lines
2.7 KiB
TypeScript
Raw Permalink Normal View History

2026-03-05 23:45:39 +08:00
import NProgress from 'nprogress' // progress bar
2026-03-08 22:41:42 +08:00
import type { Router, RouteLocationRaw } from 'vue-router'
2026-03-05 23:45:39 +08:00
import usePermission from '@/hooks/permission'
import { useAppStore, useUserStore } from '@/store'
import { NOT_FOUND, WHITE_LIST } from '../constants'
import { appRoutes } from '../routes'
2026-03-08 22:41:42 +08:00
// 标记菜单是否正在加载,防止重复加载
let isMenuLoading = false
// 标记菜单是否已加载
let isMenuLoaded = false
2026-03-05 23:45:39 +08:00
export default function setupPermissionGuard(router: Router) {
router.beforeEach(async (to, from, next) => {
const appStore = useAppStore()
const userStore = useUserStore()
const Permission = usePermission()
const permissionsAllow = Permission.accessRouter(to)
2026-03-08 22:41:42 +08:00
2026-03-05 23:45:39 +08:00
if (appStore.menuFromServer) {
// 针对来自服务端的菜单配置进行处理
// Handle routing configuration from the server
2026-03-08 22:41:42 +08:00
// 检查是否在白名单中
if (WHITE_LIST.find((el) => el.name === to.name)) {
next()
NProgress.done()
return
2026-03-05 23:45:39 +08:00
}
2026-03-08 22:41:42 +08:00
console.log('[Permission Guard] Menu not loaded, loading...')
2026-03-05 23:45:39 +08:00
2026-03-08 22:41:42 +08:00
// 检查动态路由是否已加载(使用标志位而非菜单长度,更可靠)
if (!isMenuLoaded && !isMenuLoading) {
console.log('[Permission Guard] Menu not loaded, loading...')
// 设置加载标志
isMenuLoading = true
try {
// 动态路由未加载,先获取菜单配置并注册路由
await appStore.fetchServerMenuConfig()
// 标记加载完成
isMenuLoaded = true
console.log('[Permission Guard] Menu loaded, redirecting to:', to.path)
// 路由注册后需要重新导航到当前路径
next({ path: to.path, query: to.query, params: to.params, replace: true } as RouteLocationRaw)
} catch (error) {
console.error('[Permission Guard] Failed to load menu:', error)
isMenuLoading = false
next(NOT_FOUND)
} finally {
isMenuLoading = false
2026-03-05 23:45:39 +08:00
}
2026-03-08 22:41:42 +08:00
NProgress.done()
return
2026-03-05 23:45:39 +08:00
}
2026-03-08 22:41:42 +08:00
// 如果正在加载菜单,等待加载完成
if (isMenuLoading) {
next({ path: to.path, query: to.query, params: to.params, replace: true } as RouteLocationRaw)
NProgress.done()
return
}
// 动态路由已加载,直接放行
// 因为路由已经通过 addRoute 注册Vue Router 会自动匹配
if (permissionsAllow) {
2026-03-05 23:45:39 +08:00
next()
2026-03-08 22:41:42 +08:00
} else {
next(NOT_FOUND)
}
2026-03-05 23:45:39 +08:00
} else {
// eslint-disable-next-line no-lonely-if
if (permissionsAllow) next()
else {
const destination = Permission.findFirstPermissionRoute(appRoutes, userStore.role) || NOT_FOUND
next(destination)
}
}
NProgress.done()
})
}