index.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. import { getToken } from "@/auth/auth";
  2. import { isNil } from "lodash-es";
  3. import Vue from "vue";
  4. import VueRouter from "vue-router";
  5. import Home from "../views/Home/Home.vue";
  6. import Layout from "@/views/Layout/Layout.vue";
  7. import invigilation from "./invigilation";
  8. // ignore NavigationDuplicated. https://github.com/vuejs/vue-router/issues/2881
  9. const originalPush = VueRouter.prototype.push;
  10. VueRouter.prototype.push = function push(location, onResolve, onReject) {
  11. if (onResolve || onReject)
  12. return originalPush.call(this, location, onResolve, onReject);
  13. try {
  14. return originalPush.call(this, location).catch((err) => err);
  15. } catch (error) {
  16. console.log(error);
  17. }
  18. };
  19. // end ignore
  20. Vue.use(VueRouter);
  21. // function propsValidator(route, component) {
  22. // const props = { ...route.params };
  23. // Object.entries(props).map(([key, prop]) => {
  24. // console.log(prop, key);
  25. // if (!(prop instanceof component.props[key].type)) {
  26. // props[key] = component.props[key].type(prop);
  27. // }
  28. // });
  29. // return props;
  30. // }
  31. const routes = [
  32. {
  33. path: "/home",
  34. component: Layout,
  35. children: [
  36. {
  37. path: "",
  38. name: "Home",
  39. component: Home,
  40. },
  41. ...invigilation,
  42. ],
  43. },
  44. {
  45. path: "/system",
  46. name: "System",
  47. component: Layout,
  48. children: [
  49. {
  50. path: "user",
  51. name: "UserManagement",
  52. component: () =>
  53. import(
  54. /* webpackChunkName: "system" */ "../features/system/UserManagement/UserManagement.vue"
  55. ),
  56. },
  57. {
  58. path: "org",
  59. name: "OrgManagement",
  60. component: () =>
  61. import(
  62. /* webpackChunkName: "system" */ "../features/system/OrgManagement/OrgManagement.vue"
  63. ),
  64. },
  65. ],
  66. },
  67. {
  68. path: "/exam",
  69. name: "Exam",
  70. component: Layout,
  71. children: [
  72. {
  73. path: "list",
  74. name: "ExamManagement",
  75. component: () =>
  76. import(
  77. /* webpackChunkName: "exam" */ "../features/examwork/ExamManagement/ExamManagement.vue"
  78. ),
  79. },
  80. {
  81. path: "edit/:id?",
  82. name: "ExamEdit",
  83. component: () =>
  84. import(
  85. /* webpackChunkName: "exam" */ "../features/examwork/ExamManagement/ExamEdit.vue"
  86. ),
  87. meta: {
  88. relate: "ExamManagement",
  89. },
  90. },
  91. {
  92. path: ":examId/activity",
  93. name: "ActivityManagement",
  94. component: () =>
  95. import(
  96. /* webpackChunkName: "exam" */ "../features/examwork/ActivityManagement/ActivityManagement.vue"
  97. ),
  98. },
  99. {
  100. path: ":examId/activity/new",
  101. name: "ActivityEdit",
  102. component: () =>
  103. import(
  104. /* webpackChunkName: "exam" */ "../features/examwork/ActivityManagement/ActivityEdit.vue"
  105. ),
  106. meta: {
  107. relate: "ActivityManagement",
  108. },
  109. },
  110. {
  111. path: "examstudent",
  112. name: "ExamStudentManagement",
  113. component: () =>
  114. import(
  115. /* webpackChunkName: "exam" */ "../features/examwork/ExamStudentManagement/ExamStudentManagement.vue"
  116. ),
  117. },
  118. {
  119. path: "examstudent/import",
  120. name: "ExamStudentImport",
  121. component: () =>
  122. import(
  123. /* webpackChunkName: "exam" */ "../features/examwork/ExamStudentImport/ExamStudentImport.vue"
  124. ),
  125. },
  126. {
  127. path: "course",
  128. name: "CourseManagement",
  129. component: () =>
  130. import(
  131. /* webpackChunkName: "exam" */ "../features/examwork/CourseManagement/CourseManagement.vue"
  132. ),
  133. },
  134. {
  135. path: "student",
  136. name: "StudentManagement",
  137. component: () =>
  138. import(
  139. /* webpackChunkName: "exam" */ "../features/examwork/StudentManagement/StudentManagement.vue"
  140. ),
  141. },
  142. {
  143. path: "invigilate",
  144. name: "InvigilateManagement",
  145. component: () =>
  146. import(
  147. /* webpackChunkName: "exam" */ "../features/examwork/InvigilateManagement/InvigilateManagement.vue"
  148. ),
  149. },
  150. {
  151. path: "task",
  152. name: "ImportExportTask",
  153. component: () =>
  154. import(
  155. /* webpackChunkName: "exam" */ "../features/examwork/ImportExportTask/ImportExportTask.vue"
  156. ),
  157. },
  158. ],
  159. },
  160. {
  161. path: "/login",
  162. name: "Login",
  163. component: () =>
  164. import(/* webpackChunkName: "Login" */ "../features/Login/Login.vue"),
  165. },
  166. {
  167. path: "/*",
  168. name: "404",
  169. component: () =>
  170. import(/* webpackChunkName: "default" */ "../views/404.vue"),
  171. },
  172. ];
  173. const router = new VueRouter({
  174. mode: "history",
  175. base: process.env.BASE_URL,
  176. routes,
  177. });
  178. // FIXME: router.route 添加 auth,代表是否需要认证。
  179. // FIXME: roles. 根据roles来授权。
  180. // FIXME: 在Login页面,验证通过后,返回redirectTo 页面。
  181. router.beforeEach((to, from, next) => {
  182. if (to.path) {
  183. window._hmt.push(["_trackPageview", to.fullPath]);
  184. }
  185. const token = getToken();
  186. if (isNil(token) && to.path.includes("/login") === false) {
  187. router.push("/login?redirectTo=" + encodeURI(to.fullPath));
  188. next(false);
  189. } else {
  190. next();
  191. }
  192. });
  193. export default router;