index.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /* eslint-disable @typescript-eslint/restrict-template-expressions */
  2. /* eslint-disable @typescript-eslint/no-base-to-string */
  3. import { createRouter, createWebHistory, type RouteRecordRaw } from "vue-router"
  4. import appStore from "@s"
  5. import configs from "@@/config"
  6. import JwtService from "@@/services/JwtService"
  7. import { isReLogin } from "@@/services/RequestService"
  8. import message from "@@/utils/message"
  9. import { staticRouter } from "./_staticRouter"
  10. const whiteList = ["/login", "/register", "/icon", "/r"]
  11. export const to404: RouteRecordRaw = {
  12. path: "/:pathMatch(.*)*",
  13. name: "to404",
  14. redirect: "/404"
  15. }
  16. export const constantRoutes: RouteRecordRaw[] = [
  17. {
  18. path: "/",
  19. redirect: "/home",
  20. name: configs.MAIN_ROUTER_NAME,
  21. component: () => import("@/layouts/dynamic/Layout.vue"),
  22. children: [
  23. {
  24. path: "/home",
  25. name: "home",
  26. component: () => import("@/views/home.vue"),
  27. meta: {
  28. title: "首页",
  29. affix: true
  30. }
  31. },
  32. {
  33. path: "/profile",
  34. name: "profile",
  35. component: () => import("@/views/account/profile/index.vue"),
  36. meta: {
  37. title: "个人中心",
  38. noCache: false
  39. }
  40. }
  41. ]
  42. },
  43. ...staticRouter,
  44. {
  45. path: "/",
  46. children: [
  47. {
  48. path: "/login",
  49. name: "login",
  50. component: () => import("@/views/account/login.vue"),
  51. meta: {
  52. title: "登录"
  53. }
  54. }
  55. ]
  56. },
  57. {
  58. path: "/social-callback",
  59. component: () => import("@/views/account/social/callback.vue"),
  60. meta: {
  61. title: "登录"
  62. }
  63. },
  64. {
  65. path: "/",
  66. component: () => import("@/layouts/system/Index.vue"),
  67. children: [
  68. {
  69. // the 404 route, when none of the above matches
  70. path: "/404",
  71. name: "404",
  72. component: () => import("@/views/error/404.vue"),
  73. meta: {
  74. title: "404"
  75. }
  76. },
  77. {
  78. path: "/500",
  79. name: "500",
  80. component: () => import("@/views/error/500.vue"),
  81. meta: {
  82. title: "500"
  83. }
  84. }
  85. ]
  86. }
  87. ]
  88. const router = createRouter({
  89. //出现刷新页面404的问题 请在nginx配置root 下一行添加 try_files $uri $uri/ /index.html;
  90. history: createWebHistory(import.meta.env.VITE_APP_CONTEXT_PATH),
  91. routes: constantRoutes,
  92. scrollBehavior(to, _from, savedPosition) {
  93. if (savedPosition) {
  94. return savedPosition
  95. } else if (to.hash) {
  96. return {
  97. el: to.hash,
  98. top: 80,
  99. behavior: "smooth"
  100. }
  101. } else {
  102. return { top: 0, left: 0, behavior: "smooth" }
  103. }
  104. }
  105. })
  106. function doBefore(to: any) {
  107. //console.log("Router_Name", to.name)
  108. // 解决切换路由tooltip不消失
  109. document.querySelectorAll(".tooltip").forEach((v) => {
  110. v.remove()
  111. })
  112. }
  113. router.beforeEach((to, _from, next) => {
  114. doBefore(to)
  115. NProgressStart()
  116. const authStore = appStore.authStore
  117. if (JwtService.getToken()) {
  118. document.title = to.meta.title
  119. ? `${to.meta.title} - ${import.meta.env.VITE_APP_NAME}`
  120. : import.meta.env.VITE_APP_NAME
  121. appStore.bodyConfigStore.resetLayoutConfig()
  122. if (to.path === "/login") {
  123. next({ path: "/" })
  124. NProgressClose()
  125. } else {
  126. appStore.dictStore.initDict()
  127. if (!authStore.user?.userName) {
  128. isReLogin.show = true
  129. authStore
  130. .getInfo()
  131. .then(() => {
  132. isReLogin.show = false
  133. if (to.path.search("/redirect-tenant") == 0) {
  134. next({ ...to, replace: true })
  135. } else {
  136. appStore.menuStore.generateRoutes().then(() => {
  137. next({ ...to, replace: true })
  138. })
  139. }
  140. })
  141. .catch((err) => {
  142. isReLogin.show = false
  143. authStore.logout().then(() => {
  144. message.msgError(err)
  145. next({ path: import.meta.env.VITE_APP_CONTEXT_PATH + "home" })
  146. })
  147. })
  148. appStore.appConfigStore.loadConfig()
  149. } else {
  150. next()
  151. }
  152. }
  153. } else {
  154. // 没有token
  155. if (whiteList.includes(to.path)) {
  156. // 在免登录白名单,直接进入
  157. next()
  158. } else {
  159. next(`/login?redirect=${to.fullPath}`) // 否则全部重定向到登录页
  160. NProgressClose()
  161. }
  162. }
  163. // Scroll page to top on every route change
  164. // window.scrollTo({
  165. // top: 0,
  166. // left: 0,
  167. // behavior: "smooth",
  168. // })
  169. })
  170. router.afterEach((_to, _from) => {
  171. // console.log("AFTER", _to.path, _from.path)
  172. NProgressClose()
  173. })
  174. export default router