mirror of
https://gitcode.com/yangzongzhuan/RuoYi-Vue3.git
synced 2026-05-22 19:08:37 +00:00
更换router4过期方法next
This commit is contained in:
+34
-35
@@ -18,57 +18,56 @@ const isWhiteList = (path) => {
|
|||||||
return whiteList.some(pattern => isPathMatch(pattern, path))
|
return whiteList.some(pattern => 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)
|
to.meta.title && useSettingsStore().setTitle(to.meta.title)
|
||||||
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) {
|
}
|
||||||
isRelogin.show = true
|
if (useUserStore().roles.length === 0) {
|
||||||
// 判断当前用户是否已拉取完user_info信息
|
isRelogin.show = true
|
||||||
useUserStore().getInfo().then(() => {
|
try {
|
||||||
isRelogin.show = false
|
// 拉取user_info信息
|
||||||
usePermissionStore().generateRoutes().then(accessRoutes => {
|
await useUserStore().getInfo()
|
||||||
// 根据roles权限生成可访问的路由表
|
isRelogin.show = false
|
||||||
accessRoutes.forEach(route => {
|
// 根据roles权限生成可访问的路由
|
||||||
if (!isHttp(route.path)) {
|
const accessRoutes = await usePermissionStore().generateRoutes()
|
||||||
router.addRoute(route) // 动态添加可访问路由表
|
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: '/' })
|
|
||||||
})
|
|
||||||
})
|
})
|
||||||
} else {
|
// 重新导航到目标路由,确保动态路由已注册
|
||||||
next()
|
return { ...to, replace: true }
|
||||||
|
} catch (err) {
|
||||||
|
await useUserStore().logOut()
|
||||||
|
ElMessage.error(err)
|
||||||
|
return { path: '/' }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
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}` // 否则全部重定向到登录页
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user