index.js 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. import {createRouter, createWebHistory} from 'vue-router'
  2. import {CONFIG, refreshRoleRoute} from "@/utils/roleUtils.js";
  3. import {Message} from "@/utils/message/Message.js";
  4. import {refreshAllDictData} from "@/utils/dictUtils.js";
  5. const routes = [
  6. { path: '/', component: () => import('../views/Login.vue') },
  7. { path: '/login', component: () => import('../views/Login.vue') },
  8. {
  9. path: '/login-mobile',
  10. meta: { defaultQuery: { loginPath: '/parent-mobile-course' } },
  11. component: () => import('../views/Login.vue')
  12. },
  13. // 免登录
  14. { path: '/quick-login', component: () => import('../views/QuickLogin.vue') },
  15. { path: '/promotion-login', component: () => import('../views/PromotionLogin.vue') },
  16. // //【AI实验课】登录
  17. // { path: '/ai-login', component: () => import('../views/AiCourseLogin.vue') },
  18. // //【blockly编程课】免租户登录
  19. // { path: '/blockly-login', meta: {TENANT: '内部测试租户'}, component: () => import('../views/BlocklyLogin.vue') },
  20. // 网页版登录注册页
  21. {
  22. path: '/register-login',
  23. component: () => import('../views/RegisterLogin.vue')
  24. },
  25. // 编程课注册页
  26. {
  27. path: '/reg',
  28. component: () => import('../views/BlocklyRegister.vue')
  29. },
  30. // 管理界面
  31. {
  32. path: '/management-interface',
  33. component: () => import('../views/ManagementInterface.vue')
  34. },
  35. // 【通识课】首页
  36. {
  37. path: '/home',
  38. component: () => import('../components/HomePage.vue')
  39. },
  40. // 智能课
  41. {
  42. path: '/ai-general-course',
  43. component: () => import('../views/AIPage/AIGeneralCourse.vue')
  44. },
  45. // AI实验室
  46. {
  47. path: '/ai-laboratory',
  48. component: () => import('../views/AIPage/AILaboratory.vue')
  49. },
  50. // 英文数字人老师
  51. {
  52. path: '/ai-ennumerals',
  53. component: () => import('../views/EnNumerals/index.vue')
  54. },
  55. // 能力测评 问卷列表
  56. {
  57. path: '/evaluation',
  58. component: () => import('../views/evaluation/testList.vue')
  59. },
  60. // 在线测试
  61. {
  62. path: '/testTopic',
  63. component: () => import('../views/evaluation/testTopic.vue')
  64. },
  65. // 个性化学习
  66. {
  67. path: '/personalized',
  68. component: () => import('../views/personalized/Personalized.vue')
  69. },
  70. // 测试提交
  71. {
  72. path: '/testSubmit',
  73. component: () => import('../views/evaluation/testSubmit.vue')
  74. },
  75. // 智能绘画
  76. {
  77. path: '/ai-painting',
  78. component: () => import('../views/AIPage/AIPainting.vue')
  79. },
  80. // 图生图
  81. {
  82. path: '/ai-image',
  83. component: () => import('../views/AIPage/AIImageToImage.vue')
  84. },
  85. // AI头像
  86. {
  87. path: '/ai-avatar',
  88. component: () => import('../views/AIPage/AIAvatar.vue')
  89. },
  90. // 图生视频
  91. {
  92. path: '/ai-video',
  93. component: () => import('../views/AIPage/AIImageToVideo.vue')
  94. },
  95. // 大运河
  96. {
  97. path: '/ai-grandcanal',
  98. component: () => import('../views/AIPage/GrandCanal.vue')
  99. },
  100. // 植物专家
  101. {
  102. path: '/ai-plantexperts',
  103. component: () => import('../views/AIPage/PlantExperts.vue')
  104. },
  105. // 智能问答
  106. {
  107. path: '/ai-questions',
  108. component: () => import('../views/AIPage/AIQuestions.vue')
  109. },
  110. // 通识课
  111. {
  112. path: '/ai-develop',
  113. component: () => import('../views/AIPage/AIDevelop.vue')
  114. },
  115. // 家长课堂-手机端课程列表
  116. {
  117. path: '/parent-mobile-course',
  118. component: () => import('../views/ParentMobile/ParentMobileCourse.vue')
  119. },
  120. // 家长课堂-手机端课程列表详情
  121. {
  122. path: '/parent-mobile-course-detail',
  123. component: () => import('../views/ParentMobile/ParentMobileCourseDetail.vue')
  124. },
  125. // 虚拟实验室
  126. {
  127. path: '/virtual-laboratory',
  128. component: () => import('../views/virtuallaboratory/index.vue')
  129. },
  130. // 智能台灯
  131. { path: '/smartDeskLamp', component: () => import('../views/virtuallaboratory/blockly/SmartDeskLamp.vue') },
  132. // 智能家居
  133. { path: '/smartHome', component: () => import('../views/virtuallaboratory/blockly/SmartHome.vue') },
  134. // AI 古诗
  135. {
  136. path: '/ai-poetry',
  137. component: () => import('../views/AIPage/AIPoetry.vue')
  138. },
  139. // 【blockly编程课】首页
  140. {
  141. path: '/blocklyHome',
  142. component: () => import('../views/programming/ProgrammingGame.vue')
  143. },
  144. // 编程课列表
  145. {
  146. path: '/programmingList',
  147. component: () => import('../views/programming/ProgrammingList.vue')
  148. },
  149. // 编程课列表内容
  150. {
  151. path: '/programmingCourset',
  152. component: () => import('../views/programming/ProgrammingCourset.vue')
  153. },
  154. // 编程课视频
  155. {
  156. path: '/interface',
  157. component: () => import('../views/programming/Interface.vue')
  158. },
  159. // 【AI实验课】首页
  160. // 实验室主题
  161. {
  162. path: '/aiCourseHome',
  163. component: () => import('../views/laboratory/ExperimentalTheme.vue')
  164. },
  165. // 实验室类型
  166. {
  167. path: '/experiment-type',
  168. component: () => import('../views/laboratory/ExperimentType.vue')
  169. },
  170. // 实验课程
  171. {
  172. path: '/experimental-course',
  173. component: () => import('../views/laboratory/ExperimentalCourses.vue')
  174. },
  175. // 实验界面
  176. {
  177. path: '/experimental-interface',
  178. component: () => import('../views/laboratory/ExperimentalInterface.vue')
  179. },
  180. ]
  181. // 定义主页及其子路由映射
  182. const homeRoutes = {
  183. home: '/home',
  184. login: '/login',
  185. role: 'course',
  186. children: [
  187. '/ai-general-course',
  188. '/ai-laboratory',
  189. '/ai-ennumerals',
  190. '/evaluation',
  191. '/testTopic',
  192. '/personalized',
  193. '/testSubmit',
  194. '/ai-painting',
  195. '/ai-image',
  196. '/ai-avatar',
  197. '/ai-video',
  198. '/ai-questions',
  199. '/ai-develop',
  200. '/ai-grandcanal',
  201. '/ai-plantexperts',
  202. '/virtual-laboratory',
  203. '/smartHome',
  204. '/smartDeskLamp',
  205. '/ai-poetry',
  206. '/parent-mobile-course-detail',
  207. '/parent-mobile-course',
  208. ]
  209. }
  210. const blocklyRoutes = {
  211. home: '/blocklyHome',
  212. login: '/blockly-login',
  213. role: 'blockly',
  214. children: [
  215. '/programmingList',
  216. '/programmingCourset',
  217. '/interface'
  218. ]
  219. }
  220. const aiCourseRoutes = {
  221. home: '/aiCourseHome',
  222. login: '/ai-login',
  223. role: 'aiCourse',
  224. children: [
  225. '/experiment-type',
  226. '/experimental-course',
  227. '/experimental-interface'
  228. ]
  229. }
  230. // 管理界面路由配置
  231. const managementRoutes = {
  232. home: '/management-interface',
  233. login: '/login',
  234. role: 'management',
  235. children: [
  236. '/management-interface'
  237. ]
  238. }
  239. // 定义登录页面与主页的映射关系
  240. const loginToHomeMap = {
  241. '/login': managementRoutes.home
  242. }
  243. const router = createRouter({
  244. history: createWebHistory(),
  245. routes
  246. })
  247. // 导航守卫
  248. router.beforeEach(async (to, from, next) => {
  249. // 注册页面始终允许访问,不需要登录状态
  250. if ( ['/register-login',
  251. '/reg',
  252. '/login-mobile',
  253. '/quick-login',
  254. '/promotion-login'].includes(to.path)) {
  255. next()
  256. return
  257. }
  258. // 检查登录状态
  259. const isLoggedIn = localStorage.getItem('isLoggedIn') === 'true'
  260. // 允许未登录用户访问的页面列表
  261. const allowedPages = Object.keys(loginToHomeMap)
  262. // 如果未登录且不是允许访问的页面,重定向到登录页
  263. if (!isLoggedIn && !allowedPages.includes(to.path)) {
  264. next('/login')
  265. return
  266. }
  267. if (to.path === "/"){
  268. await refreshAllDictData()
  269. await refreshRoleRoute()
  270. }
  271. // 从localStorage获取角色路由数据
  272. const roleRouteStr = localStorage.getItem(CONFIG.USER_ROLE_ROUTE_KEY)
  273. const roleRouteSet = roleRouteStr ? JSON.parse(roleRouteStr) : []
  274. // 如果是登录页面,直接放行并存储登录信息
  275. if (Object.keys(loginToHomeMap).includes(to.path)) {
  276. localStorage.setItem('loginPath', to.path)
  277. next()
  278. return
  279. }
  280. // 获取当前登录类型
  281. const loginPath = localStorage.getItem('loginPath')
  282. // 获取当前登录类型对应的主页
  283. let allowedHome = loginToHomeMap[loginPath]
  284. // 检查目标路由是否在允许的路由范围内
  285. let isAllowed = false
  286. // 首先检查用户是否有权限访问对应的主页
  287. const hasHomePermission = roleRouteSet.includes(homeRoutes.role)
  288. const hasBlocklyPermission = roleRouteSet.includes(blocklyRoutes.role)
  289. const hasAiCoursePermission = roleRouteSet.includes(aiCourseRoutes.role)
  290. const hasManagementPermission = true // 管理界面默认允许所有登录用户访问
  291. // 检查目标路由是否在允许的范围内
  292. if ((to.path === managementRoutes.home || managementRoutes.children.includes(to.path)) && hasManagementPermission) {
  293. isAllowed = true
  294. } else if ((to.path === homeRoutes.home || homeRoutes.children.includes(to.path)) && hasHomePermission) {
  295. isAllowed = true
  296. } else if ((to.path === blocklyRoutes.home || blocklyRoutes.children.includes(to.path)) && hasBlocklyPermission) {
  297. isAllowed = true
  298. } else if ((to.path === aiCourseRoutes.home || aiCourseRoutes.children.includes(to.path)) && hasAiCoursePermission) {
  299. isAllowed = true
  300. }
  301. if (isAllowed) {
  302. next()
  303. } else {
  304. // 不允许访问时,重定向到管理界面
  305. if (to.path !== "/")Message().warning('您没有权限访问该页面,已自动切换到管理界面!', true);
  306. next(managementRoutes.home)
  307. }
  308. })
  309. export default router;
  310. export { homeRoutes, blocklyRoutes, aiCourseRoutes, managementRoutes };