Skip to content

Commit

Permalink
perf: 移除路由守卫中的 next 参数
Browse files Browse the repository at this point in the history
  • Loading branch information
pany-ang committed Dec 3, 2024
1 parent 2685efa commit ab306c4
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions src/router/guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,21 @@ const { setTitle } = useTitle()

export function registerNavigationGuard(router: Router) {
// 全局前置守卫
router.beforeEach(async (to, _from, next) => {
router.beforeEach(async (to, _from) => {
NProgress.start()
const userStore = useUserStore()
const permissionStore = usePermissionStore()
// 如果没有登陆
if (!getToken()) {
// 如果在免登录的白名单中,则直接进入
if (isWhiteList(to)) return next()
if (isWhiteList(to)) return true
// 其他没有访问权限的页面将被重定向到登录页面
return next("/login")
return "/login"
}
// 如果已经登录,并准备进入 Login 页面,则重定向到主页
if (to.path === "/login") return next({ path: "/" })
if (to.path === "/login") return "/"
// 如果用户已经获得其权限角色
if (userStore.roles.length !== 0) return next()
if (userStore.roles.length !== 0) return true
// 否则要重新获取权限角色
try {
await userStore.getInfo()
Expand All @@ -38,12 +38,12 @@ export function registerNavigationGuard(router: Router) {
// 将 "有访问权限的动态路由" 添加到 Router 中
permissionStore.addRoutes.forEach(route => router.addRoute(route))
// 设置 replace: true, 因此导航将不会留下历史记录
next({ ...to, replace: true })
return { ...to, replace: true }
} catch (error) {
// 过程中发生任何错误,都直接重置 Token,并重定向到登录页面
userStore.resetToken()
ElMessage.error((error as Error).message || "路由守卫发生错误")
next("/login")
return "/login"
}
})

Expand Down

0 comments on commit ab306c4

Please sign in to comment.