|
@@ -30,20 +30,14 @@ const pendingLoading = ref(false)
|
|
|
* 设置路由全局前置守卫
|
|
* 设置路由全局前置守卫
|
|
|
*/
|
|
*/
|
|
|
export function setupBeforeEachGuard(router: Router): void {
|
|
export function setupBeforeEachGuard(router: Router): void {
|
|
|
- router.beforeEach(
|
|
|
|
|
- async (
|
|
|
|
|
- to: RouteLocationNormalized,
|
|
|
|
|
- from: RouteLocationNormalized,
|
|
|
|
|
- next: NavigationGuardNext
|
|
|
|
|
- ) => {
|
|
|
|
|
- try {
|
|
|
|
|
- await handleRouteGuard(to, from, next, router)
|
|
|
|
|
- } catch (error) {
|
|
|
|
|
- console.error('路由守卫处理失败:', error)
|
|
|
|
|
- next('/exception/500')
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ router.beforeEach(async (to: RouteLocationNormalized, from: RouteLocationNormalized, next: NavigationGuardNext) => {
|
|
|
|
|
+ try {
|
|
|
|
|
+ await handleRouteGuard(to, from, next, router)
|
|
|
|
|
+ } catch (error) {
|
|
|
|
|
+ console.error('路由守卫处理失败:', error)
|
|
|
|
|
+ next('/exception/500')
|
|
|
}
|
|
}
|
|
|
- )
|
|
|
|
|
|
|
+ })
|
|
|
|
|
|
|
|
// 设置后置守卫以关闭 loading 和进度条
|
|
// 设置后置守卫以关闭 loading 和进度条
|
|
|
setupAfterEachGuard(router)
|
|
setupAfterEachGuard(router)
|
|
@@ -73,12 +67,7 @@ function setupAfterEachGuard(router: Router): void {
|
|
|
/**
|
|
/**
|
|
|
* 处理路由守卫逻辑
|
|
* 处理路由守卫逻辑
|
|
|
*/
|
|
*/
|
|
|
-async function handleRouteGuard(
|
|
|
|
|
- to: RouteLocationNormalized,
|
|
|
|
|
- from: RouteLocationNormalized,
|
|
|
|
|
- next: NavigationGuardNext,
|
|
|
|
|
- router: Router
|
|
|
|
|
-): Promise<void> {
|
|
|
|
|
|
|
+async function handleRouteGuard(to: RouteLocationNormalized, from: RouteLocationNormalized, next: NavigationGuardNext, router: Router): Promise<void> {
|
|
|
const settingStore = useSettingStore()
|
|
const settingStore = useSettingStore()
|
|
|
const userStore = useUserStore()
|
|
const userStore = useUserStore()
|
|
|
|
|
|
|
@@ -127,11 +116,7 @@ async function handleRouteGuard(
|
|
|
/**
|
|
/**
|
|
|
* 处理登录状态
|
|
* 处理登录状态
|
|
|
*/
|
|
*/
|
|
|
-async function handleLoginStatus(
|
|
|
|
|
- to: RouteLocationNormalized,
|
|
|
|
|
- userStore: ReturnType<typeof useUserStore>,
|
|
|
|
|
- next: NavigationGuardNext
|
|
|
|
|
-): Promise<boolean> {
|
|
|
|
|
|
|
+async function handleLoginStatus(to: RouteLocationNormalized, userStore: ReturnType<typeof useUserStore>, next: NavigationGuardNext): Promise<boolean> {
|
|
|
if (!userStore.isLogin && to.path !== RoutesAlias.Login && !to.meta.noLogin) {
|
|
if (!userStore.isLogin && to.path !== RoutesAlias.Login && !to.meta.noLogin) {
|
|
|
userStore.logOut()
|
|
userStore.logOut()
|
|
|
next(RoutesAlias.Login)
|
|
next(RoutesAlias.Login)
|
|
@@ -143,12 +128,7 @@ async function handleLoginStatus(
|
|
|
/**
|
|
/**
|
|
|
* 处理动态路由注册
|
|
* 处理动态路由注册
|
|
|
*/
|
|
*/
|
|
|
-async function handleDynamicRoutes(
|
|
|
|
|
- to: RouteLocationNormalized,
|
|
|
|
|
- from: RouteLocationNormalized,
|
|
|
|
|
- next: NavigationGuardNext,
|
|
|
|
|
- router: Router
|
|
|
|
|
-): Promise<void> {
|
|
|
|
|
|
|
+async function handleDynamicRoutes(to: RouteLocationNormalized, from: RouteLocationNormalized, next: NavigationGuardNext, router: Router): Promise<void> {
|
|
|
try {
|
|
try {
|
|
|
// 显示 loading 并标记 pending
|
|
// 显示 loading 并标记 pending
|
|
|
pendingLoading.value = true
|
|
pendingLoading.value = true
|
|
@@ -177,12 +157,7 @@ async function handleDynamicRoutes(
|
|
|
return
|
|
return
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- next({
|
|
|
|
|
- path: to.path,
|
|
|
|
|
- query: to.query,
|
|
|
|
|
- hash: to.hash,
|
|
|
|
|
- replace: true
|
|
|
|
|
- })
|
|
|
|
|
|
|
+ next({ path: to.path, query: to.query, hash: to.hash, replace: true })
|
|
|
} catch (error) {
|
|
} catch (error) {
|
|
|
console.error('动态路由注册失败:', error)
|
|
console.error('动态路由注册失败:', error)
|
|
|
next('/exception/500')
|
|
next('/exception/500')
|
|
@@ -251,15 +226,10 @@ function filterEmptyMenus(menuList: AppRouteRecord[]): AppRouteRecord[] {
|
|
|
})
|
|
})
|
|
|
.filter((item) => {
|
|
.filter((item) => {
|
|
|
// 过滤掉布局组件且没有子菜单的项
|
|
// 过滤掉布局组件且没有子菜单的项
|
|
|
- const isEmptyLayoutMenu =
|
|
|
|
|
- item.component === RoutesAlias.Layout && (!item.children || item.children.length === 0)
|
|
|
|
|
|
|
+ const isEmptyLayoutMenu = item.component === RoutesAlias.Layout && (!item.children || item.children.length === 0)
|
|
|
|
|
|
|
|
// 过滤掉组件为空字符串且没有子菜单的项,但保留有外链的菜单项
|
|
// 过滤掉组件为空字符串且没有子菜单的项,但保留有外链的菜单项
|
|
|
- const isEmptyComponentMenu =
|
|
|
|
|
- item.component === '' &&
|
|
|
|
|
- (!item.children || item.children.length === 0) &&
|
|
|
|
|
- item.meta.isIframe !== true &&
|
|
|
|
|
- !item.meta.link
|
|
|
|
|
|
|
+ const isEmptyComponentMenu = item.component === '' && (!item.children || item.children.length === 0) && item.meta.isIframe !== true && !item.meta.link
|
|
|
|
|
|
|
|
return !(isEmptyLayoutMenu || isEmptyComponentMenu)
|
|
return !(isEmptyLayoutMenu || isEmptyComponentMenu)
|
|
|
})
|
|
})
|