index.ts 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. import { createRouter, createWebHistory } from "vue-router";
  2. import LayOut from "@/layout/index.vue";
  3. import { sessionStorageTool } from "@/utils/storage";
  4. import { SESSION_STORAGE_KEYS } from "@/constants/storage";
  5. const router = createRouter({
  6. routes: [
  7. {
  8. path: "/",
  9. component: LayOut,
  10. children: [
  11. {
  12. path: "exam",
  13. name: "exam",
  14. component: () => import("@/pages/exam-manage/index.vue"),
  15. },
  16. {
  17. path: "subjects",
  18. name: "subjects",
  19. component: () => import("@/pages/subjects-manage/index.vue"),
  20. },
  21. {
  22. path: "user",
  23. name: "user",
  24. component: () => import("@/pages/user-manage/index.vue"),
  25. },
  26. {
  27. path: "school",
  28. name: "school",
  29. component: () => import("@/pages/school-manage/index.vue"),
  30. },
  31. ],
  32. },
  33. {
  34. path: "/",
  35. name: "login",
  36. component: () => import("@/pages/login/index.vue"),
  37. },
  38. ],
  39. history: createWebHistory(),
  40. });
  41. router.beforeEach((to, from, next) => {
  42. if (to.name === "login") {
  43. sessionStorageTool.remove(SESSION_STORAGE_KEYS.LOGIN_RESULT);
  44. return next();
  45. }
  46. if (!sessionStorageTool.get(SESSION_STORAGE_KEYS.LOGIN_RESULT)) {
  47. return next({ name: "login" });
  48. }
  49. next();
  50. });
  51. export default router;