|
|
@ -96,38 +96,38 @@ function getCurrentUser() { |
|
|
|
return token |
|
|
|
} |
|
|
|
// 路由守卫,检查本地 Token 是否存在
|
|
|
|
// router.beforeEach((to, from, next) => {
|
|
|
|
// console.log(from)
|
|
|
|
// const token = sessionStorage.getItem('token')
|
|
|
|
// if (!token && to.path !== '/login') {
|
|
|
|
// next('/login') // 没有 token,重定向到登录页
|
|
|
|
// } else if (token && to.path === '/login') {
|
|
|
|
// next('/index') // 已有 token 且请求登录页,则跳转到主页
|
|
|
|
// } else {
|
|
|
|
// next() // 通过
|
|
|
|
// }
|
|
|
|
// })
|
|
|
|
router.beforeEach((to, from, next) => { |
|
|
|
console.log(from) |
|
|
|
const token = sessionStorage.getItem('token') |
|
|
|
if (!token && to.path !== '/login') { |
|
|
|
next('/login') // 没有 token,重定向到登录页
|
|
|
|
} else if (token && to.path === '/login') { |
|
|
|
next('/index') // 已有 token 且请求登录页,则跳转到主页
|
|
|
|
} else { |
|
|
|
next() // 通过
|
|
|
|
} |
|
|
|
}) |
|
|
|
// 在 router/index.ts 中添加全局路由守卫
|
|
|
|
// router.beforeEach((to, from, next) => {
|
|
|
|
// console.log(from)
|
|
|
|
// const user = getCurrentUser() // 获取当前用户
|
|
|
|
// console.log(user)
|
|
|
|
// // 判断目标路由是否需要权限检查
|
|
|
|
// if (to.matched.some((record) => record.meta.requiresAuth)) {
|
|
|
|
// // 如果没有登录,重定向到登录页面
|
|
|
|
// if (!user) {
|
|
|
|
// return next('/login') // 保存当前访问的路径,用于登录后重定向
|
|
|
|
// }
|
|
|
|
// // 判断是否需要 Admin 权限
|
|
|
|
// if (
|
|
|
|
// to.matched.some((record) => record.meta.requiresAdmin) &&
|
|
|
|
// user.usrRole !== 'Admin'
|
|
|
|
// ) {
|
|
|
|
// return next({ path: '/notFound' }) // 权限不足,重定向到首页或者其他页面
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// // 如果没有问题,继续导航
|
|
|
|
// next()
|
|
|
|
// })
|
|
|
|
router.beforeEach((to, from, next) => { |
|
|
|
console.log(from) |
|
|
|
const user = getCurrentUser() // 获取当前用户
|
|
|
|
console.log(user) |
|
|
|
// 判断目标路由是否需要权限检查
|
|
|
|
if (to.matched.some((record) => record.meta.requiresAuth)) { |
|
|
|
// 如果没有登录,重定向到登录页面
|
|
|
|
if (!user) { |
|
|
|
return next('/login') // 保存当前访问的路径,用于登录后重定向
|
|
|
|
} |
|
|
|
// 判断是否需要 Admin 权限
|
|
|
|
if ( |
|
|
|
to.matched.some((record) => record.meta.requiresAdmin) && |
|
|
|
user.usrRole !== 'Admin' |
|
|
|
) { |
|
|
|
return next({ path: '/notFound' }) // 权限不足,重定向到首页或者其他页面
|
|
|
|
} |
|
|
|
} |
|
|
|
// 如果没有问题,继续导航
|
|
|
|
next() |
|
|
|
}) |
|
|
|
|
|
|
|
export default router |