router.ts 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. import { RouteRecordRaw, createRouter, createWebHashHistory } from "vue-router";
  2. import { UserStatus, logintAuth, userStatus, isSuper } from "./store/user";
  3. import { watch, watchEffect } from "vue";
  4. export const COORD_NAME = "map-coord";
  5. export const POYS_NAME = "map-poy";
  6. export const QUERY_COORD_NAME = "query-map-coord";
  7. export const QUERY_POYS_NAME = "query-map-poy";
  8. const history = createWebHashHistory();
  9. const routes: RouteRecordRaw[] = [
  10. {
  11. path: "/no-persession",
  12. name: "no-persession",
  13. meta: { title: "无权限" },
  14. component: () => import("@/view/no-persession.vue"),
  15. },
  16. {
  17. path: "/down-vision",
  18. name: "down-vision",
  19. meta: { title: "" },
  20. component: () => import("@/view/down-vision.vue"),
  21. },
  22. {
  23. path: "/login",
  24. name: "login",
  25. meta: { title: "登录" },
  26. component: () => import("@/view/login.vue"),
  27. },
  28. {
  29. path: "/tree2",
  30. name: "query-tree-2",
  31. meta: { title: "登录" },
  32. component: () => import("@/view/step-tree-v2/example/example.vue"),
  33. },
  34. {
  35. path: "/tree",
  36. name: "query-tree",
  37. meta: { title: "登录" },
  38. component: () => import("@/view/step-tree/example/example.vue"),
  39. },
  40. {
  41. path: "/",
  42. name: "main-layout",
  43. component: () => import("@/view/layout/nav.vue"),
  44. children: [
  45. {
  46. path: "relics",
  47. name: "relics",
  48. meta: { title: "文物普查" },
  49. component: () => import("@/view/relics.vue"),
  50. },
  51. {
  52. path: "relics/:relicsId",
  53. children: [
  54. // {
  55. // path: "",
  56. // name: "map",
  57. // meta: { title: "文物", navClass: "map" },
  58. // component: () => import("@/view/map/map-board.vue"),
  59. // },
  60. {
  61. path: "map",
  62. name: "map",
  63. meta: { title: "文物", navClass: "map" },
  64. component: () => import("@/view/map/layout.vue"),
  65. children: [
  66. {
  67. path: "coord",
  68. name: COORD_NAME,
  69. meta: { title: "文物", navClass: "map" },
  70. component: () => import("@/view/map/coord.vue"),
  71. },
  72. {
  73. path: "polygons",
  74. name: POYS_NAME,
  75. meta: { title: "文物", navClass: "map" },
  76. component: () => import("@/view/map/polygons.vue"),
  77. },
  78. ],
  79. },
  80. {
  81. path: "pano/:pid",
  82. name: "pano",
  83. meta: { title: "点位", navClass: "pano" },
  84. component: () => import("@/view/pano/pano.vue"),
  85. },
  86. ],
  87. },
  88. {
  89. path: "scene",
  90. name: "scene",
  91. meta: { title: "场景管理" },
  92. component: () => import("@/view/scene.vue"),
  93. },
  94. {
  95. path: "device",
  96. name: "device",
  97. meta: { title: "设备管理" },
  98. component: () => import("@/view/device.vue"),
  99. },
  100. {
  101. path: "organization",
  102. name: "organization",
  103. meta: { title: "单位管理" },
  104. component: () => import("@/view/organization.vue"),
  105. },
  106. {
  107. path: "users",
  108. name: "users",
  109. meta: { title: "用户管理" },
  110. component: () => import("@/view/users.vue"),
  111. },
  112. ],
  113. },
  114. {
  115. path: "/query",
  116. name: "query-main-layout",
  117. component: () => import("@/view/layout/nav.vue"),
  118. children: [
  119. {
  120. path: "relics/:relicsId",
  121. children: [
  122. {
  123. path: "",
  124. name: "query-map",
  125. meta: { title: "文物", navClass: "map" },
  126. component: () => import("@/view/map/layout.vue"),
  127. children: [
  128. {
  129. path: "query-coord",
  130. name: QUERY_COORD_NAME,
  131. meta: { title: "文物", navClass: "map" },
  132. component: () => import("@/view/map/coord.vue"),
  133. },
  134. {
  135. path: "query-polygons",
  136. name: QUERY_POYS_NAME,
  137. meta: { title: "文物", navClass: "map" },
  138. component: () => import("@/view/map/polygons.vue"),
  139. },
  140. ],
  141. },
  142. {
  143. path: "pano/:pid",
  144. name: "query-pano",
  145. meta: { title: "点位", navClass: "pano" },
  146. component: () => import("@/view/pano/pano.vue"),
  147. },
  148. ],
  149. },
  150. ],
  151. },
  152. { path: '/:pathMatch(.*)*', component: import("@/view/layout/nav.vue") },
  153. ];
  154. export const findRoute = (
  155. routeName: string,
  156. fullPath = false,
  157. routeAll = routes
  158. ): RouteRecordRaw | null => {
  159. for (const route of routeAll) {
  160. if (route.name === routeName) {
  161. return route;
  162. } else if (route.children) {
  163. const childRoute = findRoute(routeName, fullPath, route.children);
  164. if (childRoute) {
  165. return fullPath ? { ...route, children: [childRoute] } : childRoute;
  166. }
  167. }
  168. }
  169. return null;
  170. };
  171. export const router = createRouter({ history, routes });
  172. export const setDocTitle = (title: string) => {
  173. document.title = title + "-不可移动文物管理平台";
  174. };
  175. watchEffect(() => {
  176. const routeName = router.currentRoute.value.name?.toString();
  177. if (routeName === "login" && userStatus.value === UserStatus.LOGINED) {
  178. router.replace({ name: "scene" });
  179. } else if (
  180. routeName &&
  181. routeName !== "login" &&
  182. !routeName.includes("query") &&
  183. userStatus.value === UserStatus.NOT_LOGIN
  184. ) {
  185. router.replace({ name: "login" });
  186. }
  187. });
  188. // 非登录页面校验是否等里
  189. watch(
  190. () => [router.currentRoute.value.name?.toString(), userStatus.value] as const,
  191. ([routeName, userStatus]) => {
  192. if (
  193. routeName &&
  194. !routeName.includes("query") &&
  195. userStatus === UserStatus.UNKNOWN
  196. ) {
  197. logintAuth();
  198. }
  199. },
  200. { immediate: true }
  201. );
  202. router.beforeEach((to, _, next) => {
  203. if (!to.name || to.name === "main-layout") {
  204. if (userStatus.value !== UserStatus.NOT_LOGIN) {
  205. router.replace({ name: "scene" });
  206. } else {
  207. router.replace({ name: "login" });
  208. }
  209. return;
  210. }
  211. // organization
  212. if (to.name === "organization") {
  213. console.log('isSuper-organization', isSuper.value)
  214. if (!isSuper.value) {
  215. router.replace({ name: "scene" });
  216. return
  217. }
  218. }
  219. if (to.name === "map") {
  220. router.replace({ name: COORD_NAME, params: to.params });
  221. } else if (to.name === "query-map") {
  222. router.replace({ name: QUERY_COORD_NAME, params: to.params });
  223. }
  224. if (to.meta?.title) {
  225. setDocTitle(to.meta.title as string);
  226. }
  227. next();
  228. });