225 lines
8.3 KiB
TypeScript
225 lines
8.3 KiB
TypeScript
import { createRouter, createWebHistory } from 'vue-router'
|
|
|
|
const router = createRouter({
|
|
history: createWebHistory(import.meta.env.BASE_URL),
|
|
routes: [
|
|
{
|
|
path: '/',
|
|
name: 'home',
|
|
component: () => import('../views/HomeView.vue'),
|
|
meta: { requiresAuth: true, isAdmin: false }
|
|
},
|
|
{
|
|
path: '/auth',
|
|
name: 'auth',
|
|
component: () => import('../views/AuthView.vue'),
|
|
meta: { requiresAuth: false, hideNav: true, isAdmin: false }
|
|
},
|
|
{
|
|
path: '/products',
|
|
name: 'products',
|
|
component: () => import('../views/ProductListView.vue'),
|
|
meta: { requiresAuth: true, isAdmin: false }
|
|
},
|
|
{
|
|
path: '/product/:id',
|
|
name: 'product-detail',
|
|
component: () => import('../views/ProductDetailView.vue'),
|
|
meta: { requiresAuth: true, hideNav: true, isAdmin: false }
|
|
},
|
|
{
|
|
path: '/cart',
|
|
name: 'cart',
|
|
component: () => import('../views/CartView.vue'),
|
|
meta: { requiresAuth: true, isAdmin: false }
|
|
},
|
|
{
|
|
path: '/profile',
|
|
name: 'profile',
|
|
component: () => import('../views/ProfileView.vue'),
|
|
meta: { requiresAuth: true, isAdmin: false }
|
|
},
|
|
{
|
|
path: '/shop/:id',
|
|
name: 'shop-detail',
|
|
component: () => import('../views/ShopDetailView.vue'),
|
|
meta: { requiresAuth: true, isAdmin: false }
|
|
},
|
|
{
|
|
path: '/wishlist',
|
|
name: 'wishlist',
|
|
component: () => import('../views/WishlistView.vue'),
|
|
meta: { requiresAuth: true, isAdmin: false }
|
|
},
|
|
{
|
|
path: '/settings',
|
|
name: 'settings',
|
|
component: () => import('../views/SettingsView.vue'),
|
|
meta: { requiresAuth: true, hideNav: true, isAdmin: false }
|
|
},
|
|
{
|
|
path: '/admin/login',
|
|
name: 'admin-login',
|
|
component: () => import('../views/admin/AdminLoginView.vue'),
|
|
meta: { requiresAuth: false, isAdmin: true, hideNav: true }
|
|
},
|
|
{
|
|
path: '/admin',
|
|
component: () => import('../views/admin/AdminLayout.vue'),
|
|
meta: { requiresAuth: true, isAdmin: true, hideNav: true },
|
|
children: [
|
|
{
|
|
path: '',
|
|
name: 'admin',
|
|
component: () => import('../views/admin/HomeView.vue'),
|
|
meta: { requiresAuth: true, isAdmin: true, hideNav: true }
|
|
},
|
|
{
|
|
path: 'dashboard',
|
|
name: 'admin-dashboard',
|
|
component: () => import('../views/admin/DashboardView.vue'),
|
|
meta: { requiresAuth: true, isAdmin: true, hideNav: true, permissionCode: 'dashboard:view' }
|
|
},
|
|
{
|
|
path: 'users',
|
|
name: 'admin-users',
|
|
component: () => import('../views/admin/UsersView.vue'),
|
|
meta: { requiresAuth: true, isAdmin: true, hideNav: true, permissionCode: 'user:view' }
|
|
},
|
|
{
|
|
path: 'products',
|
|
name: 'admin-products',
|
|
component: () => import('../views/admin/ProductsView.vue'),
|
|
meta: { requiresAuth: true, isAdmin: true, hideNav: true, permissionCode: 'product:view' }
|
|
},
|
|
{
|
|
path: 'shops',
|
|
name: 'admin-shops',
|
|
component: () => import('../views/admin/ShopsView.vue'),
|
|
meta: { requiresAuth: true, isAdmin: true, hideNav: true, permissionCode: 'shop:view' }
|
|
},
|
|
{
|
|
path: 'permissions',
|
|
name: 'admin-permissions',
|
|
component: () => import('../views/admin/PermissionsView.vue'),
|
|
meta: { requiresAuth: true, isAdmin: true, hideNav: true, permissionCode: 'permission:view' }
|
|
}
|
|
]
|
|
}
|
|
]
|
|
})
|
|
|
|
router.beforeEach((to, _from, next) => {
|
|
const isAdminRoute = to.meta.isAdmin as boolean
|
|
const requiresAuth = to.meta.requiresAuth as boolean
|
|
|
|
if (isAdminRoute) {
|
|
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 {
|
|
const token = localStorage.getItem('accessToken') || sessionStorage.getItem('accessToken')
|
|
|
|
if (requiresAuth && !token) {
|
|
next('/auth')
|
|
} else if (to.path === '/auth' && token) {
|
|
next('/')
|
|
} else if (to.path === '/' && !token) {
|
|
next('/auth')
|
|
} else {
|
|
next()
|
|
}
|
|
}
|
|
})
|
|
|
|
export default router |