更换router4过期方法next

This commit is contained in:
RuoYi
2026-04-16 15:50:53 +08:00
parent f7d23e886e
commit 3f4e93bd61
+28 -29
View File
@@ -18,57 +18,56 @@ const isWhiteList = (path: string): boolean => {
return whiteList.some((pattern: string) => isPathMatch(pattern, path)) return whiteList.some((pattern: string) => isPathMatch(pattern, path))
} }
router.beforeEach((to, from, next) => { router.beforeEach(async (to, from) => {
NProgress.start() NProgress.start()
if (getToken()) { if (getToken()) {
to.meta.title && useSettingsStore().setTitle(to.meta.title as string) to.meta.title && useSettingsStore().setTitle(to.meta.title as string)
const isLock = useLockStore().isLock const isLock = useLockStore().isLock
/* has token*/
if (to.path === '/login') { if (to.path === '/login') {
next({ path: '/' })
NProgress.done() NProgress.done()
} else if (isWhiteList(to.path)) { return { path: '/' }
next() }
} else if (isLock && to.path !== '/lock') { if (isWhiteList(to.path)) {
next({ path: '/lock' }) return true
}
if (isLock && to.path !== '/lock') {
NProgress.done() NProgress.done()
} else if (!isLock && to.path === '/lock') { return { path: '/lock' }
next({ path: '/' }) }
if (!isLock && to.path === '/lock') {
NProgress.done() NProgress.done()
} else { return { path: '/' }
}
if (useUserStore().roles.length === 0) { if (useUserStore().roles.length === 0) {
isRelogin.show = true isRelogin.show = true
// 判断当前用户是否已拉取完user_info信息 try {
useUserStore().getInfo().then(() => { // 拉取user_info信息
await useUserStore().getInfo()
isRelogin.show = false isRelogin.show = false
usePermissionStore().generateRoutes().then((accessRoutes: any[]) => { // 根据roles权限生成可访问的路由
// 根据roles权限生成可访问的路由表 const accessRoutes = await usePermissionStore().generateRoutes()
accessRoutes.forEach((route: any) => { accessRoutes.forEach((route: any) => {
if (!isHttp(route.path)) { if (!isHttp(route.path)) {
router.addRoute(route) // 动态添加可访问路由表 router.addRoute(route)
} }
}) })
next({ ...to, replace: true }) // hack方法 确保addRoutes已完成 // 重新导航到目标路由,确保动态路由已注册
}) return { ...to, replace: true }
}).catch((err: any) => { } catch (err) {
useUserStore().logOut().then(() => { await useUserStore().logOut()
ElMessage.error(err as string) ElMessage.error(err)
next({ path: '/' }) return { path: '/' }
})
})
} else {
next()
} }
} }
return true
} else { } else {
// 没有token // 没有token
if (isWhiteList(to.path)) { if (isWhiteList(to.path)) {
// 在免登录白名单,直接进入 // 在免登录白名单,直接进入
next() return true
} else {
next(`/login?redirect=${to.fullPath}`) // 否则全部重定向到登录页
NProgress.done()
} }
NProgress.done()
return `/login?redirect=${to.fullPath}` // 否则全部重定向到登录页
} }
}) })