import { createRouter, createWebHistory } from 'vue-router' import type { RouteRecordRaw } from 'vue-router' import { useUserStore } from '@/stores/user' const routes: RouteRecordRaw[] = [ { path: '/login', name: 'Login', component: () => import('@/views/Login.vue'), meta: { title: '登录' } }, { path: '/', component: () => import('@/views/Layout.vue'), redirect: '/dashboard', children: [ { path: 'dashboard', name: 'Dashboard', component: () => import('@/views/Dashboard.vue'), meta: { title: '首页' } }, { path: 'customer', name: 'Customer', component: () => import('@/views/Customer.vue'), meta: { title: '客户管理' } }, { path: 'report', name: 'Report', component: () => import('@/views/Report.vue'), meta: { title: '报备管理' } }, { path: 'dealer', name: 'Dealer', component: () => import('@/views/Dealer.vue'), meta: { title: '经销商管理', requiresAdmin: true } } ] } ] const router = createRouter({ history: createWebHistory(), routes }) // 路由守卫 router.beforeEach((to, from, next) => { const userStore = useUserStore() // 设置页面标题 document.title = `${to.meta.title || ''} - 经销商管理系统` // 如果访问登录页,已登录则跳转到首页 if (to.path === '/login') { if (userStore.isLoggedIn) { next('/') } else { next() } return } // 其他页面需要登录 if (!userStore.isLoggedIn) { next('/login') return } // 管理员权限校验 if (to.meta.requiresAdmin && !userStore.isAdmin) { next('/') return } next() }) export default router