import {createRouter, createWebHistory} from 'vue-router' import {CONFIG, refreshRoleRoute} from "@/utils/roleUtils.js"; import {Message} from "@/utils/message/Message.js"; import {refreshAllDictData} from "@/utils/dictUtils.js"; const routes = [ { path: '/', component: () => import('../views/Login.vue') }, { path: '/login', component: () => import('../views/Login.vue') }, { path: '/login-mobile', meta: { defaultQuery: { loginPath: '/ai-general-course' } }, component: () => import('../views/Login.vue') }, // 免登录 { path: '/quick-login', component: () => import('../views/QuickLogin.vue') }, { path: '/promotion-login', component: () => import('../views/PromotionLogin.vue') }, // //【AI实验课】登录 // { path: '/ai-login', component: () => import('../views/AiCourseLogin.vue') }, // //【blockly编程课】免租户登录 // { path: '/blockly-login', meta: {TENANT: '内部测试租户'}, component: () => import('../views/BlocklyLogin.vue') }, // 网页版登录注册页 { path: '/register-login', component: () => import('../views/RegisterLogin.vue') }, // 编程课注册页 { path: '/reg', component: () => import('../views/BlocklyRegister.vue') }, // 管理界面 { path: '/management-interface', component: () => import('../views/ManagementInterface.vue') }, // 【通识课】首页 { path: '/home', component: () => import('../components/HomePage.vue') }, // 智能课 { path: '/ai-general-course', component: () => import('../views/AIPage/AIGeneralCourse.vue') }, // AI实验室 { path: '/ai-laboratory', component: () => import('../views/AIPage/AILaboratory.vue') }, // 英文数字人老师 { path: '/ai-ennumerals', component: () => import('../views/EnNumerals/index.vue') }, // 能力测评 问卷列表 { path: '/evaluation', component: () => import('../views/evaluation/testList.vue') }, // 在线测试 { path: '/testTopic', component: () => import('../views/evaluation/testTopic.vue') }, // 个性化学习 { path: '/personalized', component: () => import('../views/personalized/Personalized.vue') }, // 测试提交 { path: '/testSubmit', component: () => import('../views/evaluation/testSubmit.vue') }, // 智能绘画 { path: '/ai-painting', component: () => import('../views/AIPage/AIPainting.vue') }, // 图生图 { path: '/ai-image', component: () => import('../views/AIPage/AIImageToImage.vue') }, // AI头像 { path: '/ai-avatar', component: () => import('../views/AIPage/AIAvatar.vue') }, // 图生视频 { path: '/ai-video', component: () => import('../views/AIPage/AIImageToVideo.vue') }, // 大运河 { path: '/ai-grandcanal', component: () => import('../views/AIPage/GrandCanal.vue') }, // 植物专家 { path: '/ai-plantexperts', component: () => import('../views/AIPage/PlantExperts.vue') }, // 智能问答 { path: '/ai-questions', component: () => import('../views/AIPage/AIQuestions.vue') }, // 通识课 { path: '/ai-develop', component: () => import('../views/AIPage/AIDevelop.vue') }, // 家长课堂-手机端课程列表 { path: '/parent-mobile-course', component: () => import('../views/ParentMobile/ParentMobileCourse.vue') }, // 家长课堂-手机端课程列表详情 { path: '/parent-mobile-course-detail', component: () => import('../views/ParentMobile/ParentMobileCourseDetail.vue') }, // 虚拟实验室 { path: '/virtual-laboratory', component: () => import('../views/virtuallaboratory/index.vue') }, // 智能台灯 { path: '/smartDeskLamp', component: () => import('../views/virtuallaboratory/blockly/SmartDeskLamp.vue') }, // 智能家居 { path: '/smartHome', component: () => import('../views/virtuallaboratory/blockly/SmartHome.vue') }, // AI 古诗 { path: '/ai-poetry', component: () => import('../views/AIPage/AIPoetry.vue') }, // 【blockly编程课】首页 { path: '/blocklyHome', component: () => import('../views/programming/ProgrammingGame.vue') }, // 编程课列表 { path: '/programmingList', component: () => import('../views/programming/ProgrammingList.vue') }, // 编程课列表内容 { path: '/programmingCourset', component: () => import('../views/programming/ProgrammingCourset.vue') }, // 编程课视频 { path: '/interface', component: () => import('../views/programming/Interface.vue') }, // 【AI实验课】首页 // 实验室主题 { path: '/aiCourseHome', component: () => import('../views/laboratory/ExperimentalTheme.vue') }, // 实验室类型 { path: '/experiment-type', component: () => import('../views/laboratory/ExperimentType.vue') }, // 实验课程 { path: '/experimental-course', component: () => import('../views/laboratory/ExperimentalCourses.vue') }, // 实验界面 { path: '/experimental-interface', component: () => import('../views/laboratory/ExperimentalInterface.vue') }, ] // 定义主页及其子路由映射 const homeRoutes = { home: '/home', login: '/login', role: 'course', children: [ '/ai-general-course', '/ai-laboratory', '/ai-ennumerals', '/evaluation', '/testTopic', '/personalized', '/testSubmit', '/ai-painting', '/ai-image', '/ai-avatar', '/ai-video', '/ai-questions', '/ai-develop', '/ai-grandcanal', '/ai-plantexperts', '/virtual-laboratory', '/smartHome', '/smartDeskLamp', '/ai-poetry', '/parent-mobile-course-detail', '/parent-mobile-course', ] } const blocklyRoutes = { home: '/blocklyHome', login: '/blockly-login', role: 'blockly', children: [ '/programmingList', '/programmingCourset', '/interface' ] } const aiCourseRoutes = { home: '/aiCourseHome', login: '/ai-login', role: 'aiCourse', children: [ '/experiment-type', '/experimental-course', '/experimental-interface' ] } // 管理界面路由配置 const managementRoutes = { home: '/management-interface', login: '/login', role: 'management', children: [ '/management-interface' ] } // 定义登录页面与主页的映射关系 const loginToHomeMap = { '/login': managementRoutes.home } const router = createRouter({ history: createWebHistory(), routes }) // 导航守卫 router.beforeEach(async (to, from, next) => { // 注册页面始终允许访问,不需要登录状态 if ( ['/register-login', '/reg', '/login-mobile', '/quick-login', '/promotion-login'].includes(to.path)) { next() return } // 检查登录状态 const isLoggedIn = localStorage.getItem('isLoggedIn') === 'true' // 允许未登录用户访问的页面列表 const allowedPages = Object.keys(loginToHomeMap) // 如果未登录且不是允许访问的页面,重定向到登录页 if (!isLoggedIn && !allowedPages.includes(to.path)) { next('/login') return } if (to.path === "/"){ await refreshAllDictData() await refreshRoleRoute() } // 从localStorage获取角色路由数据 const roleRouteStr = localStorage.getItem(CONFIG.USER_ROLE_ROUTE_KEY) const roleRouteSet = roleRouteStr ? JSON.parse(roleRouteStr) : [] // 如果是登录页面,直接放行并存储登录信息 if (Object.keys(loginToHomeMap).includes(to.path)) { localStorage.setItem('loginPath', to.path) next() return } // 获取当前登录类型 const loginPath = localStorage.getItem('loginPath') // 获取当前登录类型对应的主页 let allowedHome = loginToHomeMap[loginPath] // 检查目标路由是否在允许的路由范围内 let isAllowed = false // 首先检查用户是否有权限访问对应的主页 const hasHomePermission = roleRouteSet.includes(homeRoutes.role) const hasBlocklyPermission = roleRouteSet.includes(blocklyRoutes.role) const hasAiCoursePermission = roleRouteSet.includes(aiCourseRoutes.role) const hasManagementPermission = true // 管理界面默认允许所有登录用户访问 // 检查目标路由是否在允许的范围内 if ((to.path === managementRoutes.home || managementRoutes.children.includes(to.path)) && hasManagementPermission) { isAllowed = true } else if ((to.path === homeRoutes.home || homeRoutes.children.includes(to.path)) && hasHomePermission) { isAllowed = true } else if ((to.path === blocklyRoutes.home || blocklyRoutes.children.includes(to.path)) && hasBlocklyPermission) { isAllowed = true } else if ((to.path === aiCourseRoutes.home || aiCourseRoutes.children.includes(to.path)) && hasAiCoursePermission) { isAllowed = true } if (isAllowed) { next() } else { // 不允许访问时,重定向到管理界面 if (to.path !== "/")Message().warning('您没有权限访问该页面,已自动切换到管理界面!', true); next(managementRoutes.home) } }) export default router; export { homeRoutes, blocklyRoutes, aiCourseRoutes, managementRoutes };