import { createRouter, createWebHistory } from 'vue-router' import { useUserStore } from '@/stores/user' const routes = [ { path: '/login', name: 'Login', component: () => import('@/views/Login.vue'), meta: { title: '登录' } }, { path: '/', component: () => import('@/layout/MainLayout.vue'), redirect: '/dashboard', children: [ { path: 'dashboard', name: 'Dashboard', component: () => import('@/views/Dashboard.vue'), meta: { title: '首页' } }, { path: 'system/user', name: 'User', component: () => import('@/views/system/User.vue'), meta: { title: '用户管理' } }, { path: 'system/department', name: 'Department', component: () => import('@/views/system/Department.vue'), meta: { title: '部门管理' } }, { path: 'data/excel', name: 'Excel', component: () => import('@/views/data/Excel.vue'), meta: { title: 'Excel上传' } }, { path: 'data/unit-task', name: 'UnitTask', component: () => import('@/views/data/UnitTask.vue'), meta: { title: '单位上传任务' } }, { path: 'statistics/unit', name: 'UnitStatistics', component: () => import('@/views/statistics/UnitStatistics.vue'), meta: { title: '各单位数据统计' } } ] } ] const router = createRouter({ history: createWebHistory(), routes }) router.beforeEach((to, from, next) => { document.title = to.meta.title || '数据管理系统' const userStore = useUserStore() if (to.path !== '/login' && !userStore.token) { next('/login') } else { next() } }) export default router