From ecf0e4add9afadef866e938861c98d2c45c1a814 Mon Sep 17 00:00:00 2001 From: zhangmj <1373657463@qq.com> Date: Mon, 5 Jan 2026 16:11:23 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B7=AF=E7=94=B1=E6=B3=A8=E9=87=8A=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=E5=8F=96=E6=B6=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/router/index.js | 74 ++++++++++++++++++++++----------------------- 1 file changed, 37 insertions(+), 37 deletions(-) diff --git a/src/router/index.js b/src/router/index.js index 43080cd..30be581 100644 --- a/src/router/index.js +++ b/src/router/index.js @@ -39,43 +39,43 @@ const router = createRouter({ }) router.beforeEach((to, from, next) => { - // const token = cache.local.get('token') - // if (token) { - // if (whiteList.indexOf(to.path) !== -1) { - // next() - // } else { - // if (useUserStore().roles.length === 0) { - // // 判断当前用户是否已拉取完user_info信息 - // useUserStore().getInfo().then(() => { - // usePermissionStore().generateRoutes().then(accessRoutes => { - // accessRoutes.forEach(route => { - // router.addRoute(route) // 动态添加可访问路由表 - // }) - - // next({ ...to, replace: true }) // hack方法 确保addRoutes已完成 - // }) - // }).catch(err => { - // // 登出接口防抖操作 - // debounce(() => { - // useUserStore().logout().then(() => { - // ElMessage.error(err) - // next({ path: '/login' }) - // }) - // }, 1000)() - // }) - // } else { - // next() - // } - // } - // } else { - // // 没有token - // if (whiteList.indexOf(to.path) !== -1) { - // // 在免登录白名单,直接进入 + const token = cache.local.get('token') + if (token) { + if (whiteList.indexOf(to.path) !== -1) { next() - // } else { - // // next(`/login?redirect=${to.fullPath}`) // 否则全部重定向到登录页 - // next('/login') // 否则全部重定向到登录页 - // } - // } + } else { + if (useUserStore().roles.length === 0) { + // 判断当前用户是否已拉取完user_info信息 + useUserStore().getInfo().then(() => { + usePermissionStore().generateRoutes().then(accessRoutes => { + accessRoutes.forEach(route => { + router.addRoute(route) // 动态添加可访问路由表 + }) + + next({ ...to, replace: true }) // hack方法 确保addRoutes已完成 + }) + }).catch(err => { + // 登出接口防抖操作 + debounce(() => { + useUserStore().logout().then(() => { + ElMessage.error(err) + next({ path: '/login' }) + }) + }, 1000)() + }) + } else { + next() + } + } + } else { + // 没有token + if (whiteList.indexOf(to.path) !== -1) { + // 在免登录白名单,直接进入 + next() + } else { + // next(`/login?redirect=${to.fullPath}`) // 否则全部重定向到登录页 + next('/login') // 否则全部重定向到登录页 + } + } }) export default router