完善权限的管理和校验

This commit is contained in:
xiaowang
2026-02-26 17:54:18 +08:00
parent 9cc74c26e6
commit 8f81cfa471
14 changed files with 309 additions and 63 deletions
+95 -10
View File
@@ -71,7 +71,8 @@ const router = createRouter({
{
path: '',
name: 'admin',
redirect: '/admin/dashboard'
component: () => import('../views/admin/HomeView.vue'),
meta: { requiresAuth: true, isAdmin: true, hideNav: true }
},
{
path: 'dashboard',
@@ -113,16 +114,100 @@ router.beforeEach((to, _from, next) => {
const requiresAuth = to.meta.requiresAuth as boolean
if (isAdminRoute) {
const adminToken = localStorage.getItem('adminAccessToken') || sessionStorage.getItem('adminAccessToken')
if (requiresAuth && !adminToken) {
next('/admin/login')
} else if (to.path === '/admin/login' && adminToken) {
next('/admin/dashboard')
const adminToken = localStorage.getItem('adminAccessToken') || sessionStorage.getItem('adminAccessToken')
console.log('Admin route check:', { to: to.path, adminToken: !!adminToken, requiresAuth })
if (requiresAuth && !adminToken) {
console.log('No token, redirecting to login')
next('/admin/login')
} else if (to.path === '/admin/login' && adminToken) {
console.log('Token exists, redirecting from login to dashboard')
next('/admin/dashboard')
} else {
// 检查用户权限
const permissionCode = to.meta.permissionCode as string
if (permissionCode) {
// 获取用户信息
try {
const adminUserInfoStr = localStorage.getItem('adminUserInfo')
console.log('Admin user info:', adminUserInfoStr)
if (adminUserInfoStr) {
try {
const adminUserInfo = JSON.parse(adminUserInfoStr)
console.log('Parsed admin user info:', adminUserInfo)
// 管理员用户(type === 4)拥有所有权限
if (adminUserInfo.type === 4) {
console.log('Admin user, allowing access')
next()
}
// 店长用户(type === 2)拥有所有权限
else if (adminUserInfo.type === 2) {
console.log('Shop manager user, allowing access')
next()
}
// 美甲师用户(type === 3)需要检查权限
else if (adminUserInfo.type === 3) {
console.log('美甲师 user, checking permissions')
// 获取用户权限
const userPermissionsStr = localStorage.getItem('userPermissions')
console.log('User permissions:', userPermissionsStr)
if (userPermissionsStr) {
const userPermissions = JSON.parse(userPermissionsStr)
// 检查用户是否拥有该权限
if (userPermissions.includes(permissionCode)) {
console.log('User has required permission:', permissionCode)
next()
} else {
console.log('User missing required permission:', permissionCode)
// 权限不足,检查是否是首页
if (to.path === '/admin') {
// 如果是首页,允许访问(作为默认页面)
console.log('Allowing access to home as default page')
next()
} else {
// 对于其他页面,重定向到首页
next('/admin')
}
}
} else {
console.log('No user permissions found')
// 没有权限信息,允许访问首页临时,直到权限加载完成
if (to.path === '/admin') {
console.log('Allowing access to home to load permissions')
next()
} else {
// 对于其他页面,重定向到首页
next('/admin')
}
}
} else {
console.log('Unknown user type, redirecting to login')
// 未知用户类型,重定向到登录页
next('/admin/login')
}
} catch (parseError) {
console.error('Failed to parse user info:', parseError)
// 解析失败,允许访问所有页面临时,直到权限加载完成
console.log('Allowing temporary access due to parse error')
next()
}
} else {
console.log('No user info found, redirecting to login')
// 没有用户信息,重定向到登录页
next('/admin/login')
}
} catch (error) {
console.error('解析用户信息失败:', error)
// 解析失败,重定向到登录页
next('/admin/login')
}
} else {
// 没有权限要求,直接通过
next()
}
}
} else {
next()
}
} else {
const token = localStorage.getItem('accessToken') || sessionStorage.getItem('accessToken')
if (requiresAuth && !token) {