index.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. import {createRouter, createWebHashHistory} 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. {
  7. path: '/',
  8. redirect: () => {
  9. // 判断域名,返回对应路由
  10. if (window.location.hostname === 'blockly.learn-ai.com.cn') {
  11. return '/blockly-login';
  12. } else {
  13. return '/login';
  14. }
  15. }
  16. },
  17. { path: '/login', component: () => import('../views/Login.vue') },
  18. // 免登录
  19. { path: '/quick-login', component: () => import('../views/QuickLogin.vue') },
  20. { path: '/promotion-login', component: () => import('../views/PromotionLogin.vue') },
  21. //【AI实验课】登录
  22. { path: '/ai-login', component: () => import('../views/AiCourseLogin.vue') },
  23. //【blockly编程课】免租户登录
  24. { path: '/blockly-login', meta: {TENANT: '内部测试租户'}, component: () => import('../views/BlocklyLogin.vue') },
  25. // 网页版登录注册页
  26. {
  27. path: '/register-login',
  28. component: () => import('../views/RegisterLogin.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: '/virtual-laboratory',
  118. component: () => import('../views/virtuallaboratory/index.vue')
  119. },
  120. // AI 古诗
  121. {
  122. path: '/ai-poetry',
  123. component: () => import('../views/AIPage/AIPoetry.vue')
  124. },
  125. // 【blockly编程课】首页
  126. {
  127. path: '/blocklyHome',
  128. component: () => import('../views/programming/ProgrammingGame.vue')
  129. },
  130. // Blockly
  131. { path: '/blockly', component: () => import('../views/blockly/Blockly.vue') },
  132. // Blockly2
  133. { path: '/blockly2', component: () => import('../views/blockly/Blockly2.vue') },
  134. { path: '/mapGame', component: () => import('../views/blockly/MapGame.vue') },
  135. // 编程游戏列表
  136. { path: '/programming02', component: () => import('../views/gamepage/GameIndex.vue') },
  137. // 编程课列表
  138. {
  139. path: '/programmingList',
  140. component: () => import('../views/programming/ProgrammingList.vue')
  141. },
  142. // 编程课列表内容
  143. {
  144. path: '/programmingCourset',
  145. component: () => import('../views/programming/ProgrammingCourset.vue')
  146. },
  147. // 编程课视频
  148. {
  149. path: '/interface',
  150. component: () => import('../views/programming/Interface.vue')
  151. },
  152. // 【AI实验课】首页
  153. // 实验室主题
  154. {
  155. path: '/aiCourseHome',
  156. component: () => import('../views/laboratory/ExperimentalTheme.vue')
  157. },
  158. // 实验室类型
  159. {
  160. path: '/experiment-type',
  161. component: () => import('../views/laboratory/ExperimentType.vue')
  162. },
  163. // 实验课程
  164. {
  165. path: '/experimental-course',
  166. component: () => import('../views/laboratory/ExperimentalCourses.vue')
  167. },
  168. // 实验界面
  169. {
  170. path: '/experimental-interface',
  171. component: () => import('../views/laboratory/ExperimentalInterface.vue')
  172. },
  173. ]
  174. // 定义主页及其子路由映射
  175. const homeRoutes = {
  176. home: '/home',
  177. login: '/login',
  178. role: 'course',
  179. children: [
  180. '/ai-general-course',
  181. '/ai-laboratory',
  182. '/ai-ennumerals',
  183. '/evaluation',
  184. '/testTopic',
  185. '/personalized',
  186. '/testSubmit',
  187. '/ai-painting',
  188. '/ai-image',
  189. '/ai-avatar',
  190. '/ai-video',
  191. '/ai-questions',
  192. '/ai-develop',
  193. '/ai-grandcanal',
  194. '/ai-plantexperts',
  195. '/virtual-laboratory',
  196. '/ai-poetry',
  197. '/management-interface',
  198. ]
  199. }
  200. const blocklyRoutes = {
  201. home: '/blocklyHome',
  202. login: '/blockly-login',
  203. role: 'blockly',
  204. children: [
  205. '/blockly',
  206. '/blockly2',
  207. '/mapGame',
  208. '/programming02',
  209. '/programmingList',
  210. '/programmingCourset',
  211. '/interface'
  212. ]
  213. }
  214. const aiCourseRoutes = {
  215. home: '/aiCourseHome',
  216. login: '/ai-login',
  217. role: 'aiCourse',
  218. children: [
  219. '/experiment-type',
  220. '/experimental-course',
  221. '/experimental-interface'
  222. ]
  223. }
  224. // 定义登录页面与主页的映射关系
  225. const loginToHomeMap = {
  226. '/login': homeRoutes.home,
  227. '/quick-login': homeRoutes.home,
  228. '/promotion-login': homeRoutes.home,
  229. '/blockly-login': blocklyRoutes.home,
  230. '/ai-login': aiCourseRoutes.home,
  231. '/register-login': homeRoutes.home
  232. }
  233. const router = createRouter({
  234. history: createWebHashHistory(),
  235. routes
  236. })
  237. // 导航守卫
  238. router.beforeEach(async (to, from, next) => {
  239. if (to.path === "/"){
  240. await refreshAllDictData()
  241. await refreshRoleRoute()
  242. }
  243. // 从localStorage获取角色路由数据
  244. const roleRouteStr = localStorage.getItem(CONFIG.USER_ROLE_ROUTE_KEY)
  245. const roleRouteSet = roleRouteStr ? JSON.parse(roleRouteStr) : []
  246. // 如果是登录页面,直接放行并存储登录信息
  247. if (Object.keys(loginToHomeMap).includes(to.path)) {
  248. localStorage.setItem('loginPath', to.path)
  249. next()
  250. return
  251. }
  252. // 获取当前登录类型
  253. const loginPath = localStorage.getItem('loginPath')
  254. // 获取当前登录类型对应的主页
  255. let allowedHome = loginToHomeMap[loginPath]
  256. // 检查目标路由是否在允许的路由范围内
  257. let isAllowed = false
  258. // 首先检查用户是否有权限访问对应的主页
  259. const hasHomePermission = roleRouteSet.includes(homeRoutes.role)
  260. const hasBlocklyPermission = roleRouteSet.includes(blocklyRoutes.role)
  261. const hasAiCoursePermission = roleRouteSet.includes(aiCourseRoutes.role)
  262. // 如果用户没有当前登录类型对应的权限,切换到用户有权限的主页
  263. if (!((allowedHome === homeRoutes.home && hasHomePermission) ||
  264. (allowedHome === blocklyRoutes.home && hasBlocklyPermission) ||
  265. (allowedHome === aiCourseRoutes.home && hasAiCoursePermission))) {
  266. // 找到用户有权限的主页
  267. if (hasAiCoursePermission) {
  268. allowedHome = aiCourseRoutes.home
  269. } else if (hasBlocklyPermission) {
  270. allowedHome = blocklyRoutes.home
  271. } else if (hasHomePermission) {
  272. allowedHome = homeRoutes.home
  273. } else {
  274. // 如果没有任何权限,可以考虑重定向到登录页面或其他公共页面
  275. next(loginPath)
  276. return
  277. }
  278. }
  279. // 检查目标路由是否在允许的范围内
  280. if ((to.path === homeRoutes.home || homeRoutes.children.includes(to.path)) && hasHomePermission) {
  281. isAllowed = true
  282. } else if ((to.path === blocklyRoutes.home || blocklyRoutes.children.includes(to.path)) && hasBlocklyPermission) {
  283. isAllowed = true
  284. } else if ((to.path === aiCourseRoutes.home || aiCourseRoutes.children.includes(to.path)) && hasAiCoursePermission) {
  285. isAllowed = true
  286. }
  287. if (isAllowed) {
  288. next()
  289. } else {
  290. // 不允许访问时,重定向到用户有权限的主页
  291. if (to.path !== "/")Message().warning('您没有权限访问该页面,已自动切换到您有权限的主页!', true);
  292. next(allowedHome)
  293. }
  294. })
  295. export default router;
  296. export { homeRoutes, blocklyRoutes, aiCourseRoutes };