更换router4过期方法next

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