Index.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. import { createRouter, createWebHashHistory, type RouteLocationNormalized, type RouteRecordRaw } from "vue-router"
  2. import appStore from "@/stores"
  3. import configs from "@/core/config/Index"
  4. import { staticRotuer } from "./statictRouter"
  5. export const to404: RouteRecordRaw = {
  6. path: "/:pathMatch(.*)*",
  7. name: "to404",
  8. redirect: "/404",
  9. }
  10. const routes: Array<RouteRecordRaw> = [
  11. {
  12. path: "/",
  13. redirect: "/DataBoard/index",
  14. name: configs.MAIN_ROUTER_NAME,
  15. component: () => import("@/layouts/main-layout/MainLayout.vue"),
  16. meta: {
  17. middleware: "auth",
  18. },
  19. children: [
  20. // {
  21. // path: "/dashboard",
  22. // name: "dashboard",
  23. // component: () => import("@/views/dashboard/Index.vue"),
  24. // meta: {
  25. // pageTitle: "Dashboard",
  26. // breadcrumbs: ["Dashboards"],
  27. // },
  28. // },
  29. ],
  30. },
  31. {
  32. path: "/",
  33. component: () => import("@/layouts/AuthLayout.vue"),
  34. children: [
  35. {
  36. path: "/sign-in",
  37. name: configs.SIGN_ROUTE_NAME,
  38. component: () => import("@/views/account/SignIn.vue"),
  39. meta: {
  40. pageTitle: "登录",
  41. },
  42. },
  43. ],
  44. },
  45. {
  46. path: "/",
  47. component: () => import("@/layouts/SystemLayout.vue"),
  48. children: [
  49. {
  50. // the 404 route, when none of the above matches
  51. path: "/404",
  52. name: "404",
  53. component: () => import("@/views/error/404.vue"),
  54. meta: {
  55. pageTitle: "Error 404",
  56. },
  57. },
  58. {
  59. path: "/500",
  60. name: "500",
  61. component: () => import("@/views/error/500.vue"),
  62. meta: {
  63. pageTitle: "Error 500",
  64. },
  65. },
  66. ],
  67. },
  68. {
  69. path: "/DataCenter",
  70. name: "DataCenter",
  71. meta: {
  72. pageTitle: "数据大屏",
  73. },
  74. component: () => import("@/views/dashboard/DataCenter.vue"),
  75. },
  76. {
  77. path: "/icon",
  78. name: "icon",
  79. meta: {
  80. pageTitle: "图标",
  81. },
  82. component: () => import("@/layouts/Icon.vue"),
  83. },
  84. ...staticRotuer,
  85. ]
  86. const router = createRouter({
  87. history: createWebHashHistory(),
  88. routes,
  89. })
  90. const hasNecessaryRoute = (to: RouteLocationNormalized) => {
  91. if (router.hasRoute(to.name ?? "")) {
  92. return true
  93. }
  94. const list = router.getRoutes()
  95. for (let i = 0; i < list.length; i++) {
  96. const r = list[i]
  97. if (r.path === to.path || (r.name && r.name === to.name)) {
  98. return true
  99. }
  100. }
  101. return false
  102. }
  103. let isReload = true
  104. router.beforeEach((to, from, next) => {
  105. const configStore = appStore.bodyConfigStore
  106. //解决切换路由tooltip不消失
  107. document.querySelectorAll(".tooltip").forEach((v) => {
  108. v.remove()
  109. })
  110. // current page view title
  111. document.title = `${to.meta.pageTitle ?? ""} - ${import.meta.env.VITE_APP_NAME}`
  112. if (to.name != configs.SIGN_ROUTE_NAME && (to.meta.auth ?? true)) {
  113. const authStore = appStore.authStore
  114. // 在每次页面更改之前验证身份验证令牌
  115. authStore.verifyAuth()
  116. if (!authStore.isAuthenticated && !hasNecessaryRoute(to)) {
  117. console.log("需要登录")
  118. next({ name: configs.SIGN_ROUTE_NAME, params: { redirectUrl: to.fullPath } })
  119. } else {
  120. //刷新页面后重新加载路由菜单
  121. if (isReload) {
  122. isReload = false
  123. appStore.menuStore.loadMenus()
  124. next(to)
  125. } else {
  126. // 将配置重置为初始状态
  127. configStore.resetLayoutConfig()
  128. //在页面访问之前检查页面是否需要身份验证
  129. next()
  130. }
  131. }
  132. } else {
  133. next()
  134. }
  135. // Scroll page to top on every route change
  136. window.scrollTo({
  137. top: 0,
  138. left: 0,
  139. behavior: "smooth",
  140. })
  141. })
  142. export default router