router.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. import { RouteRecordRaw, createRouter, createWebHashHistory } from "vue-router";
  2. import { UserStatus, userStatus } from "./store/user";
  3. import { watchEffect } from "vue";
  4. const history = createWebHashHistory();
  5. const routes: RouteRecordRaw[] = [
  6. {
  7. path: "/down-vision",
  8. name: "down-vision",
  9. component: () => import("@/view/down-vision.vue"),
  10. },
  11. {
  12. path: "/login",
  13. name: "login",
  14. meta: { title: "登录" },
  15. component: () => import("@/view/login.vue"),
  16. },
  17. {
  18. path: "/tree",
  19. name: "query-tree",
  20. meta: { title: "登录" },
  21. component: () => import("@/view/step-tree/example/example.vue"),
  22. },
  23. {
  24. path: "/",
  25. name: "main-layout",
  26. component: () => import("@/view/layout/nav.vue"),
  27. children: [
  28. {
  29. path: "relics",
  30. name: "relics",
  31. meta: { title: "文物普查" },
  32. component: () => import("@/view/relics.vue"),
  33. },
  34. {
  35. path: "relics/:relicsId",
  36. children: [
  37. {
  38. path: "",
  39. name: "map",
  40. meta: { title: "文物", navClass: "map" },
  41. component: () => import("@/view/map/map.vue"),
  42. },
  43. {
  44. path: "pano/:pid",
  45. name: "pano",
  46. meta: { title: "点位", navClass: "pano" },
  47. component: () => import("@/view/pano/pano.vue"),
  48. },
  49. ],
  50. },
  51. {
  52. path: "scene",
  53. name: "scene",
  54. meta: { title: "场景管理" },
  55. component: () => import("@/view/scene.vue"),
  56. },
  57. {
  58. path: "device",
  59. name: "device",
  60. meta: { title: "设备管理" },
  61. component: () => import("@/view/device.vue"),
  62. },
  63. ],
  64. },
  65. {
  66. path: "/query",
  67. name: "query-main-layout",
  68. component: () => import("@/view/layout/nav.vue"),
  69. children: [
  70. {
  71. path: "relics/:relicsId",
  72. children: [
  73. {
  74. path: "",
  75. name: "query-map",
  76. meta: { title: "文物", navClass: "map" },
  77. component: () => import("@/view/map/map.vue"),
  78. },
  79. {
  80. path: "pano/:pid",
  81. name: "query-pano",
  82. meta: { title: "点位", navClass: "pano" },
  83. component: () => import("@/view/pano/pano.vue"),
  84. },
  85. ],
  86. },
  87. ],
  88. },
  89. ];
  90. export const findRoute = (
  91. routeName: string,
  92. fullPath = false,
  93. routeAll = routes
  94. ): RouteRecordRaw | null => {
  95. for (const route of routeAll) {
  96. if (route.name === routeName) {
  97. return route;
  98. } else if (route.children) {
  99. const childRoute = findRoute(routeName, fullPath, route.children);
  100. if (childRoute) {
  101. return fullPath ? { ...route, children: [childRoute] } : childRoute;
  102. }
  103. }
  104. }
  105. return null;
  106. };
  107. export const router = createRouter({ history, routes });
  108. export const setDocTitle = (title: string) => {
  109. document.title = title + "-不可移动文物管理平台";
  110. };
  111. watchEffect(() => {
  112. const routeName = router.currentRoute.value.name?.toString();
  113. if (routeName === "login" && userStatus.value === UserStatus.LOGINED) {
  114. router.replace({ name: "scene" });
  115. } else if (
  116. routeName &&
  117. routeName !== "login" &&
  118. !routeName.includes("query") &&
  119. userStatus.value === UserStatus.NOT_LOGIN
  120. ) {
  121. router.replace({ name: "login" });
  122. }
  123. });
  124. router.beforeEach((to, _, next) => {
  125. if (!to.name || to.name === "main-layout") {
  126. if (userStatus.value !== UserStatus.NOT_LOGIN) {
  127. router.replace({ name: "scene" });
  128. } else {
  129. router.replace({ name: "login" });
  130. }
  131. return;
  132. }
  133. if (to.meta?.title) {
  134. setDocTitle(to.meta.title as string);
  135. }
  136. next();
  137. });