router.ts 3.0 KB

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