You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

81 lines
2.0 KiB

import { createRouter, createWebHistory } from 'vue-router'
import { getToken, getUserInfo } from '@/utils/auth'
const routes = [
{
path: '/login',
name: 'Login',
component: () => import('@/views/Login.vue'),
meta: { requiresAuth: false }
},
{
path: '/register',
name: 'Register',
component: () => import('@/views/Register.vue'),
meta: { requiresAuth: false }
},
{
path: '/',
component: () => import('@/layouts/MainLayout.vue'),
redirect: '/certificates',
meta: { requiresAuth: true },
children: [
{
path: 'certificates',
name: 'Certificates',
component: () => import('@/views/Certificates.vue')
},
{
path: 'certificate/register',
name: 'RegisterCertificate',
component: () => import('@/views/RegisterCertificate.vue')
},
{
path: 'certificate/verify',
name: 'VerifyCertificate',
component: () => import('@/views/VerifyCertificate.vue')
},
{
path: 'admin/requests',
name: 'AdminRequests',
component: () => import('@/views/admin/Requests.vue'),
meta: { requiresAdmin: true }
},
{
path: 'admin/certificates',
name: 'AdminCertificates',
component: () => import('@/views/admin/Certificates.vue'),
meta: { requiresAdmin: true }
}
]
}
]
const router = createRouter({
history: createWebHistory(),
routes
})
router.beforeEach((to, from, next) => {
const token = getToken()
const userInfo = getUserInfo()
if (to.meta.requiresAuth && !token) {
next('/login')
} else if (!to.meta.requiresAuth && token && (to.path === '/login' || to.path === '/register')) {
next('/')
} else if (to.meta.requiresAdmin) {
// 检查管理员权限
if (!userInfo || userInfo.authority !== 1) {
next('/certificates') // 非管理员跳转到证书列表
} else {
next()
}
} else {
next()
}
})
export default router