index.ts 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. import authApis from '@/apis/auth';
  2. import { createRouter, createWebHistory } from 'vue-router'
  3. import {useStore} from "@/store";
  4. const router = createRouter({
  5. history: createWebHistory(import.meta.env.BASE_URL),
  6. routes: [
  7. // 定义公共页面
  8. {
  9. path: "/404",
  10. component: () => import("@/views/404.vue")
  11. },
  12. {
  13. path: "/:catchAll(.*)",
  14. redirect: "/404"
  15. },
  16. // 登录与注册
  17. {
  18. path: "/",
  19. redirect: () => {
  20. window.location.href = "https://p-nju.seec.seecoder.cn/login?from=https://air-nju.seec.seecoder.cn";
  21. // window.location.href = "http://localhost:8000/login?from=http://localhost:4000";
  22. // {本地门户首页地址}?from={本地eai首页地址}
  23. return "/"; // 占位返回值,实际不会执行
  24. },
  25. },
  26. // {
  27. // path: "/",
  28. // redirect: "/login"
  29. // },
  30. // {
  31. // path: '/login',
  32. // name: 'login',
  33. // component: () => import('../views/LoginPage/LoginPage.vue')
  34. // },
  35. {
  36. path: '/register',
  37. name: 'register',
  38. component: () => import('../views/LoginPage/RegisterPage.vue')
  39. },
  40. {
  41. path: '/passwordChange',
  42. name: 'passwordChange',
  43. component: () => import('../views/LoginPage/PasswordChangePage.vue')
  44. },
  45. // 用户相关界面
  46. {
  47. path: '/home',
  48. name: 'home',
  49. redirect: '/home/courseSquare',
  50. component: () => import('../layout/Layout/Layout.vue'),
  51. children: [
  52. {
  53. path: "/profile",
  54. name: "profile",
  55. component: () => import('@/views/Profile.vue')
  56. },
  57. {
  58. path: 'courseSquare',
  59. name: 'courseSquare',
  60. component: () => import("../views/CourseSquare/index.vue")
  61. },
  62. {
  63. path: 'courseDetail/:courseId',
  64. name: 'courseDetail',
  65. component: () => import("../views/CourseSquare/component/CourseDetails/index.vue")
  66. },
  67. {
  68. path: 'moreCourse',
  69. name: 'moreCourse',
  70. component: () => import("../views/CourseSquare/component/AllCourse/MoreCourse.vue"),
  71. props(route) {
  72. return route.query
  73. },
  74. },
  75. {
  76. path: 'myCourse',
  77. name: 'myCourse',
  78. component: () => import("../views/CoursePage/CoursePage.vue")
  79. },
  80. {
  81. path: 'myHomework',
  82. name: 'myHomework',
  83. component: () => import("@/views/HomeworkPage/index.vue")
  84. },
  85. {
  86. path: 'homeworkDetail/:assignmentId',
  87. name: 'homeworkDetail',
  88. component: () => import("@/views/HomeworkPage/component/HomeworkDetail.vue")
  89. },
  90. {
  91. path: 'assignment/:assignmentId/aispeaking',
  92. name: 'aispeaking',
  93. component: () => import('../views/AiSpeakingPage/SpeakingPage.vue'),
  94. props: (route) => ({
  95. assignmentId: Number(route.query.assignmentId)
  96. })
  97. },
  98. {
  99. path: 'assignment/:assignmentId/airecord',
  100. name: 'airecord',
  101. component: () => import('../views/AiSpeakingPage/SpeakingRecord.vue'),
  102. props: (route) => ({
  103. userId: Number(route.query.userId),
  104. assignmentId: Number(route.query.assignmentId),
  105. assignmentName: route.query.assignmentName || ''
  106. })
  107. },
  108. {
  109. path: 'assignment/:assignmentId/edit',
  110. name: 'edit',
  111. component: () => import('../views/EditPage/EditPage.vue')
  112. },
  113. {
  114. path: 'assignment/:assignmentId/AIEvaluation',
  115. name: 'AIEvaluation',
  116. component: () => import('../views/AIEvaluationPage/AIEvaluationPage.vue')
  117. },
  118. {
  119. path: 'assignment/:assignmentId/correct',
  120. name: 'correct',
  121. component: () => import('../views/CorrectPage/CorrectPage.vue')
  122. },
  123. {
  124. path: 'myCorrect/course',
  125. name: 'myCorrectWithCourse',
  126. component: () => import("@/views/CorrectPage/CoursePage.vue")
  127. },
  128. {
  129. path: 'myCorrect/assignment',
  130. name: 'myCorrectWithAssignment',
  131. component: () => import("@/views/CorrectPage/AssignmentPage.vue")
  132. },
  133. {
  134. path: 'myCorrect/assignment/:assignmentId',
  135. name: 'myCorrectWithAssignmentId',
  136. component: () => import("@/views/CorrectPage/CorrectAssignmentPage.vue")
  137. },
  138. {
  139. path: 'studentDetail',
  140. name: 'studentDetail',
  141. component: () => import("@/views/ClassPage/StudentDetail.vue")
  142. },
  143. {
  144. path: 'courseClass/:courseId',
  145. name: 'courseClass',
  146. component: () => import("@/views/ClassPage/CourseClass.vue")
  147. },
  148. {
  149. path: 'classDetail',
  150. name: 'classDetail',
  151. component: () => import("@/views/ClassPage/ClassDetail.vue"),
  152. children:[
  153. {
  154. path: "",
  155. name:"classDetail",
  156. redirect: "/home/classDetail/student"
  157. },
  158. {
  159. path: 'student',
  160. name: 'classStudent',
  161. component: () => import("@/views/ClassPage/ClassStudent.vue")
  162. },
  163. {
  164. path: 'homework',
  165. name: 'classHomework',
  166. component: () => import("@/views/ClassPage/ClassHomework.vue")
  167. },
  168. ]
  169. },
  170. {
  171. path:"behavior",
  172. name: "behavior",
  173. redirect: 'home/behavior/main',
  174. component: () => import("../views/BehaviorPage/BehaviorBoard.vue"),
  175. children:[
  176. {
  177. path: 'main',
  178. name: 'behaviorHome',
  179. component: () => import("../views/BehaviorPage/subPage/BehaviorHome.vue")
  180. },
  181. {
  182. path: 'total',
  183. name: 'total',
  184. component: () => import("../views/BehaviorPage/subPage/TotalAnalysis.vue")
  185. },
  186. {
  187. path: 'edit',
  188. name: 'editData',
  189. component: () => import("../views/BehaviorPage/subPage/EditAnalysis.vue")
  190. },
  191. {
  192. path: 'pause',
  193. name: 'pause',
  194. component: () => import("../views/BehaviorPage/subPage/PauseAnalysis.vue")
  195. },
  196. {
  197. path: 'process',
  198. name: 'process',
  199. component: () => import("../views/BehaviorPage/subPage/ProcessAnalysis.vue")
  200. },
  201. ]
  202. },
  203. ]
  204. },
  205. // 管理员相关界面
  206. {
  207. path: "/admin",
  208. redirect: '/admin/login',
  209. },
  210. {
  211. path: "/admin/login",
  212. name: 'adminLogin',
  213. component: () => import("../views/AdminPage/LoginPage/AdminLoginPage.vue"),
  214. },
  215. // {
  216. // path: "/admin/login",
  217. // name: 'adminLogin',
  218. // component: () => import("../views/LoginPage/LoginPage.vue"),
  219. // },
  220. {
  221. path: "/manager",
  222. name: "manager",
  223. // redirect: '/manager/course',
  224. component: () => import("../layout/AdminLayout/AdminLayout.vue"),
  225. children: [
  226. {
  227. path: "course",
  228. name: "courseManage",
  229. component: () => import("../views/AdminPage/CourseManage/CourseManage.vue"),
  230. meta: {
  231. roles: ['ADMIN']
  232. }
  233. },
  234. {
  235. path: "user",
  236. name: "userManage",
  237. component: () => import("../views/AdminPage/UserManage/UserManage.vue"),
  238. meta: {
  239. roles: ['ADMIN']
  240. }
  241. },
  242. {
  243. path: "file",
  244. name: "fileManage",
  245. component: () => import("../views/AdminPage/FileManage/FileManage.vue"),
  246. meta: {
  247. roles: ['ADMIN']
  248. }
  249. },
  250. ]
  251. },
  252. ]
  253. })
  254. /*
  255. 路由守卫
  256. */
  257. router.beforeEach(async (to) => {
  258. const store = useStore();
  259. const publicPaths = [ '/register', '/passwordChange', '/admin/login'];
  260. // 1. 检查URL中的token
  261. const urlToken = new URLSearchParams(window.location.search).get('token');
  262. if (urlToken) {
  263. try {
  264. // 等待 token 验证完成
  265. const res = await authApis.authToken(urlToken || store.token);
  266. const data = res.data.data;
  267. // 存储用户状态
  268. store.setUser(data);
  269. store.setToken(data?.token);
  270. localStorage.setItem('Authorization', data?.token);
  271. localStorage.setItem('User', JSON.stringify(data));
  272. // 清除 URL 中的 token
  273. if (urlToken) {
  274. window.history.replaceState({}, '', to.path);
  275. }
  276. // 如果当前路径是公开路径,允许继续;否则跳转到首页
  277. // if (!publicPaths.includes(to.path)) {
  278. // return '/home';
  279. // }
  280. } catch (error) {
  281. console.error('Token验证失败:', error);
  282. // 验证失败,清除无效的 token
  283. store.setUser(null);
  284. store.setToken(null);
  285. localStorage.removeItem('Authorization');
  286. localStorage.removeItem('User');
  287. }
  288. }
  289. if (publicPaths.includes(to.path)) return true;
  290. if (!store.user?.role) {
  291. return '/';
  292. }
  293. if (to.meta?.roles && !(to.meta.roles as any[]).includes(store.user.role)) {
  294. return '/home';
  295. }
  296. return true;
  297. });
  298. export default router