Merge pull request #587 from sxd-mike/main

添加路由守护
This commit is contained in:
hrz
2025-03-30 18:36:06 +08:00
committed by GitHub
+18
View File
@@ -69,4 +69,22 @@ const router = new VueRouter({
routes
})
// 需要登录才能访问的路由
const protectedRoutes = ['home', 'RoleConfig', 'DeviceManagement', 'UserManagement', 'ModelConfig']
// 路由守卫
router.beforeEach((to, from, next) => {
// 检查是否是需要保护的路由
if (protectedRoutes.includes(to.name)) {
// 从localStorage获取token
const token = localStorage.getItem('token')
if (!token) {
// 未登录,跳转到登录页
next({ name: 'login', query: { redirect: to.fullPath } })
return
}
}
next()
})
export default router