import { createRouter, createWebHistory } from 'vue-router' import { useUserStore } from '@/stores/user' const routes = [ { path: '/login', name: 'Login', component: () => import('@/views/login/index.vue'), meta: { title: '登录' } }, { path: '/register', name: 'Register', component: () => import('@/views/register/index.vue'), meta: { title: '注册' } }, { path: '/', component: () => import('@/layout/index.vue'), redirect: '/home', children: [ { path: 'home', name: 'Home', component: () => import('@/views/home/index.vue'), meta: { title: '首页', requiresAuth: true } }, { path: 'detail', name: 'Detail', component: () => import('@/views/detail/index.vue'), meta: { title: '设备详情', requiresAuth: true } }, { path: 'drone-detail', name: 'DroneDetail', component: () => import('@/views/detail/drone.vue'), meta: { title: '无人机详情', requiresAuth: true } }, { path: 'dataset', name: 'Dataset', component: () => import('@/views/dataset/index.vue'), meta: { title: '数据集', requiresAuth: true } }, { path: 'face-recognition', name: 'FaceRecognition', component: () => import('@/views/face-recognition/index.vue'), meta: { title: '人脸识别', requiresAuth: true } }, { path: 'personnel', name: 'Personnel', component: () => import('@/views/personnel/index.vue'), meta: { title: '人员管理', requiresAuth: true } } ] }, { path: '/:pathMatch(.*)*', redirect: '/login' } ] const router = createRouter({ history: createWebHistory(), routes }) router.beforeEach((to, from, next) => { document.title = to.meta.title ? `${to.meta.title} - 视觉管理平台` : '视觉管理平台' const userStore = useUserStore() const token = userStore.token if (to.meta.requiresAuth) { if (!token) { next({ name: 'Login', query: { redirect: to.fullPath } }) } else { next() } } else { if (token && (to.name === 'Login' || to.name === 'Register')) { next({ name: 'Home' }) } else { next() } } }) export default router