```
feat(auth): 添加用户权限获取接口并完善JWT令牌角色信息 - 在JWT令牌中添加用户角色信息 - 新增get_my_permissions接口用于获取当前用户权限码列表 - 重构认证回调逻辑,增加错误日志记录 - 更新用户信息获取接口使用Authorization头验证 ```
This commit is contained in:
@@ -29,11 +29,6 @@ const routes = [
|
||||
name: 'DeviceList',
|
||||
component: () => import('../views/DeviceList.vue')
|
||||
},
|
||||
{
|
||||
path: '/import',
|
||||
name: 'ImportData',
|
||||
component: () => import('../views/ImportData.vue')
|
||||
},
|
||||
{
|
||||
path: '/charts',
|
||||
name: 'Charts',
|
||||
@@ -43,7 +38,36 @@ const routes = [
|
||||
path: '/olt',
|
||||
name: 'OltManage',
|
||||
component: () => import('../views/OltManage.vue')
|
||||
}
|
||||
},
|
||||
{
|
||||
path: '/inventory',
|
||||
name: 'Inventory',
|
||||
component: () => import('../views/Inventory.vue')
|
||||
},
|
||||
{
|
||||
path: '/users',
|
||||
name: 'UserManage',
|
||||
component: () => import('../views/UserManage.vue'),
|
||||
meta: { requiresRole: 'admin' }
|
||||
},
|
||||
{
|
||||
path: '/roles',
|
||||
name: 'RolePermissions',
|
||||
component: () => import('../views/RolePermissions.vue'),
|
||||
meta: { requiresRole: 'admin' }
|
||||
},
|
||||
{
|
||||
path: '/settings',
|
||||
name: 'Settings',
|
||||
component: () => import('../views/Settings.vue'),
|
||||
meta: { requiresRole: 'admin' }
|
||||
},
|
||||
{
|
||||
path: '/audit',
|
||||
name: 'AuditLog',
|
||||
component: () => import('../views/AuditLog.vue'),
|
||||
meta: { requiresRole: 'admin' }
|
||||
},
|
||||
]
|
||||
}
|
||||
]
|
||||
@@ -53,15 +77,33 @@ const router = createRouter({
|
||||
routes
|
||||
})
|
||||
|
||||
router.beforeEach((to, from, next) => {
|
||||
router.beforeEach(async (to, from, next) => {
|
||||
const authStore = useAuthStore()
|
||||
|
||||
if (to.meta.requiresAuth && !authStore.token) {
|
||||
next('/login')
|
||||
} else if (to.path === '/login' && authStore.token) {
|
||||
next('/dashboard')
|
||||
} else {
|
||||
next()
|
||||
return next('/login')
|
||||
}
|
||||
if (to.path === '/login' && authStore.token) {
|
||||
return next('/dashboard')
|
||||
}
|
||||
|
||||
// 需要角色检查时,确保 user 信息已加载
|
||||
if (to.meta.requiresRole && authStore.token) {
|
||||
if (!authStore.user) {
|
||||
await authStore.fetchProfile()
|
||||
}
|
||||
if (authStore.role !== to.meta.requiresRole) {
|
||||
return next('/dashboard')
|
||||
}
|
||||
}
|
||||
|
||||
// 加载权限列表(仅首次)
|
||||
if (authStore.token && authStore.permissions.length === 0) {
|
||||
await authStore.fetchPermissions()
|
||||
}
|
||||
|
||||
next()
|
||||
})
|
||||
|
||||
export default router
|
||||
|
||||
|
||||
Reference in New Issue
Block a user