import { createRouter, createWebHashHistory } from 'vue-router' import AdminLayout from '../layout/AdminLayout.vue' import { useUserStore } from '../stores/user' // 预加载常用组件 const Dashboard = () => import(/* webpackChunkName: "dashboard" */ '../views/dashboard/index.vue') const WechatConfig = () => import(/* webpackChunkName: "wechat" */ '../views/wechat/config/index.vue') const WechatTemplates = () => import(/* webpackChunkName: "wechat" */ '../views/wechat/templates/index.vue') const WechatLogs = () => import(/* webpackChunkName: "wechat" */ '../views/wechat/logs/index.vue') // 权限管理相关组件 const UserManagement = () => import(/* webpackChunkName: "auth" */ '../views/auth/users/index.vue') const RoleManagement = () => import(/* webpackChunkName: "auth" */ '../views/auth/roles/index.vue') const PermissionManagement = () => import(/* webpackChunkName: "auth" */ '../views/auth/permissions/index.vue') // 预加载这些组件 Promise.all([ Dashboard(), WechatConfig(), WechatTemplates(), WechatLogs(), UserManagement(), RoleManagement() ]) const router = createRouter({ history: createWebHashHistory(), routes: [ { path: '/', redirect: to => { // 如果已登录,重定向到后台首页,否则重定向到登录页 const userStore = useUserStore() return userStore.isLoggedIn ? '/dashboard' : '/login' } }, { path: '/login', name: 'Login', component: () => import(/* webpackChunkName: "login" */ '../views/login/index.vue'), meta: { title: '登录' } }, { path: '/screen', name: 'Screen', component: () => import('@/views/dashboard/screen/index.vue'), meta: { title: '数据大屏', requiresAuth: true } }, { path: '/', component: AdminLayout, redirect: '/dashboard', meta: { requiresAuth: true }, children: [ { path: 'dashboard', name: 'Dashboard', component: Dashboard, meta: { title: '控制台', icon: 'HomeFilled', keepAlive: true } }, { path: 'system/profile', name: 'UserProfile', component: () => import('../views/system/profile/index.vue'), meta: { title: '个人信息', hideInMenu: true } }, // 微信管理 { path: 'wechat', name: 'Wechat', meta: { title: '消息推送管理', icon: 'ChatDotRound' }, redirect: '/wechat/config', children: [ { path: 'config', name: 'WechatConfig', component: WechatConfig, meta: { title: '公众号配置', keepAlive: true } }, { path: 'templates', name: 'WechatTemplates', component: WechatTemplates, meta: { title: '消息模板', keepAlive: true } }, { path: 'logs', name: 'WechatLogs', component: WechatLogs, meta: { title: '发送记录', keepAlive: true } } ] }, // 权限管理 { path: 'auth', name: 'Auth', meta: { title: '权限管理', icon: 'Lock' }, redirect: '/auth/users', children: [ { path: 'users', name: 'UserManagement', component: UserManagement, meta: { title: '用户管理', keepAlive: true } }, { path: 'roles', name: 'RoleManagement', component: RoleManagement, meta: { title: '角色管理', keepAlive: true } }, { path: 'permissions', name: 'PermissionManagement', component: PermissionManagement, meta: { title: '权限管理', keepAlive: true } } ] }, // 系统管理(移除用户相关管理) { path: 'system', name: 'System', meta: { title: '系统管理', icon: 'Setting' }, redirect: '/system/settings', children: [ { path: 'settings', name: 'SystemSettings', component: () => import('../views/system/settings/index.vue'), meta: { title: '系统设置' } }, { path: 'logs', name: 'SystemLogs', component: () => import('../views/system/logs/index.vue'), meta: { title: '系统日志' } }, { path: 'data', name: 'DataManagement', component: () => import('@/views/system/data/index.vue'), meta: { title: '数据管理' } }, { path: 'carousel', name: 'SystemCarousel', component: () => import('../views/system/carousel/index.vue'), meta: { title: '轮播图管理' } } ] }, // 监测管理 { path: 'monitor', name: 'Monitor', meta: { title: '监测管理', icon: 'DataAnalysis' }, redirect: '/monitor/species', children: [ { path: 'species', name: 'SpeciesMonitor', component: () => import('../views/monitor/species/index.vue'), meta: { title: '物种监测' } }, { path: 'environment', name: 'EnvironmentMonitor', component: () => import('../views/monitor/environment/index.vue'), meta: { title: '环境监测' } }, { path: 'observations', name: 'ObservationsMonitor', component: () => import('../views/monitor/observations/index.vue'), meta: { title: '观测管理' } } ] }, // 巡护管理 { path: 'patrol', name: 'Patrol', meta: { title: '巡护管理', icon: 'Location' }, redirect: '/patrol/plans', children: [ { path: 'plans', name: 'PatrolPlans', component: () => import('../views/patrol/plans/index.vue'), meta: { title: '巡护计划' } }, { path: 'tasks', name: 'PatrolTasks', component: () => import('../views/patrol/tasks/index.vue'), meta: { title: '巡护任务' } }, { path: 'records', name: 'PatrolRecords', component: () => import('../views/patrol/records/index.vue'), meta: { title: '巡护记录' } }, { path: 'events', name: 'PatrolEvents', component: () => import('../views/patrol/events/index.vue'), meta: { title: '安防事件' } } ] }, // AI巡护 { path: 'AIPatrol', name: 'AIPatrol', meta: { title: 'AI巡护', icon: 'Monitor' }, redirect: '/AIPatrol/camera', children: [ { path: 'camera', name: 'CameraManagement', component: () => import(/* webpackChunkName: "aipatrol" */ '../views/AIPatrol/Camera/index.vue'), meta: { title: '摄像头管理', keepAlive: true } }, { path: 'sensor', name: 'SensorManagement', component: () => import(/* webpackChunkName: "aipatrol" */ '../views/AIPatrol/sensor/index.vue'), meta: { title: '传感器管理', keepAlive: true } }, { path: 'drone', name: 'DroneManagement', component: () => import(/* webpackChunkName: "aipatrol" */ '../views/AIPatrol/drone/index.vue'), meta: { title: '无人机管理', keepAlive: true } }, { path: 'videos', name: 'VideoList', component: () => import(/* webpackChunkName: "aipatrol" */ '../views/AIPatrol/videos/index.vue'), meta: { title: '视频列表', keepAlive: true } } ] }, // 报告管理 { path: 'report', name: 'Report', meta: { title: '报告管理', icon: 'Document' }, redirect: '/report/daily', children: [ { path: 'daily', name: 'DailyReports', component: () => import('../views/report/daily/index.vue'), meta: { title: '报告管理' } }, { path: 'reportTemplates', name: 'ReportTemplates', component: () => import('../views/report/reportTemplates/index.vue'), meta: { title: '报告模板' } }, { path: 'analysis', name: 'AnalysisReports', component: () => import('../views/report/analysis/index.vue'), meta: { title: '分析报告' } } ] }, // 活动管理 { path: 'activity', name: 'Activity', meta: { title: '活动管理', icon: 'Collection' }, redirect: '/activity/study', children: [ { path: 'study', name: 'StudyManagement', component: () => import('../views/activity/study/index.vue'), meta: { title: '研学管理' } }, { path: 'knowledge', name: 'KnowledgeManagement', component: () => import('../views/activity/knowledge/index.vue'), meta: { title: '知识库管理' } } ] }, // 课程管理 { path: 'course', name: 'Course', meta: { title: '课程管理', icon: 'DataLine' }, redirect: '/course/index', children: [ { path: 'index', name: 'CourseManagement', component: () => import('../views/course/index.vue'), meta: { title: '课程管理' } }, { path: 'application', name: 'CourseApplication', component: () => import('../views/course/Application.vue'), meta: { title: '报名管理' } } ] }, // 用户反馈 { path: 'feedback', name: 'Feedback', meta: { title: '用户反馈', icon: 'ChatLineRound' }, redirect: '/feedback/suggestions', children: [ { path: 'suggestions', name: 'FeedbackSuggestions', component: () => import('../views/feedback/suggestions/index.vue'), meta: { title: '意见反馈' } }, { path: 'satisfaction', name: 'FeedbackSatisfaction', component: () => import('../views/feedback/satisfaction/index.vue'), meta: { title: '满意度调查' } } ] }, // 关于我们 { path: 'about', name: 'About', meta: { title: '关于我们', icon: 'InfoFilled' }, redirect: '/about/projects', children: [ { path: 'projects', name: 'Projects', component: () => import('@/views/about/projects/index.vue'), meta: { title: '简介' } }, { path: 'needToKnow', name: 'NeedToKnow', component: () => import('@/views/about/needToKnow/index.vue'), meta: { title: '游园需知' } } ] } ] } ] }) // 路由守卫 router.beforeEach((to, from, next) => { // 预加载相关联的组件 if (to.name === 'WechatConfig') { WechatTemplates() WechatLogs() } else if (to.name === 'CameraManagement') { import('../views/AIPatrol/sensor/index.vue') import('../views/AIPatrol/drone/index.vue') } const userStore = useUserStore() // 如果访问登录页且已登录,重定向到首页 if (to.path === '/login' && userStore.isLoggedIn) { next('/') return } // 如果需要认证但未登录,重定向到登录页 if (to.matched.some(record => record.meta.requiresAuth) && !userStore.isLoggedIn) { next({ path: '/login', query: { redirect: to.fullPath } }) } else { next() } }) export default router