import { createRouter, createWebHistory } from 'vue-router' import { useAuthStore } from '@/stores/auth' const routes = [ { path: '/login', name: 'Login', component: () => import('@/views/login/index.vue'), meta: { title: '登录', public: true } }, { path: '/error', name: 'Error', component: () => import('@/views/error/index.vue'), meta: { title: '出错了', public: true } }, { path: '/', component: () => import('@/components/Layout.vue'), redirect: '/dashboard', children: [ { path: 'dashboard', name: 'Dashboard', component: () => import('@/views/dashboard/index.vue'), meta: { title: '仪表盘', icon: 'Odometer' } }, { path: 'tenants', name: 'Tenants', component: () => import('@/views/tenants/index.vue'), meta: { title: '租户管理', icon: 'OfficeBuilding' } }, { path: 'tenants/:id', name: 'TenantDetail', component: () => import('@/views/tenants/detail.vue'), meta: { title: '租户详情', hidden: true } }, { path: 'apps', name: 'Apps', component: () => import('@/views/apps/index.vue'), meta: { title: '应用管理', icon: 'Grid' } }, { path: 'tenant-wechat-apps', name: 'TenantWechatApps', component: () => import('@/views/tenant-wechat-apps/index.vue'), meta: { title: '企微应用', icon: 'ChatDotRound' } }, { path: 'app-config', name: 'AppConfig', component: () => import('@/views/app-config/index.vue'), meta: { title: '租户应用配置', icon: 'Setting' } }, { path: 'stats', name: 'Stats', component: () => import('@/views/stats/index.vue'), meta: { title: '统计分析', icon: 'TrendCharts' } }, { path: 'logs', name: 'Logs', component: () => import('@/views/logs/index.vue'), meta: { title: '日志查看', icon: 'Document' } }, { path: 'users', name: 'Users', component: () => import('@/views/users/index.vue'), meta: { title: '用户管理', icon: 'User', role: 'admin' } } ] } ] const router = createRouter({ history: createWebHistory(), routes }) // 路由守卫 router.beforeEach((to, from, next) => { // 设置页面标题 document.title = to.meta.title ? `${to.meta.title} - 平台管理` : '平台管理' // 检查登录状态 const authStore = useAuthStore() if (to.meta.public) { next() } else if (!authStore.isLoggedIn) { next('/login') } else if (to.meta.role && authStore.user?.role !== to.meta.role) { next('/dashboard') } else { next() } }) export default router