西藏巴青项目

index.js 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. import { createRouter, createWebHistory } from "vue-router"
  2. import ScreenLayout from "../layout/ScreenLayout.vue"
  3. import { getToken } from "../utils/auth"
  4. import { ensureScreenAutoLogin, isScreenAutoLoginEnabled } from "../utils/screenAutoLogin"
  5. /**
  6. * 大屏路由表(路径与后端 /bigScreen/* 业务对应,便于联调)
  7. * meta.titleZh / titleBo:浏览器标题与页面双语标题参考
  8. */
  9. const routes = [
  10. {
  11. path: "/login",
  12. name: "Login",
  13. component: () => import("../views/login/index.vue"),
  14. meta: {
  15. titleZh: "登录",
  16. titleBo: "ཐོ་ཞུགས།",
  17. public: true
  18. }
  19. },
  20. {
  21. path: "/",
  22. component: ScreenLayout,
  23. redirect: "/home",
  24. meta: { requiresAuth: true },
  25. children: [
  26. {
  27. path: "home",
  28. name: "ScreenHome",
  29. component: () => import("../views/home/index.vue"),
  30. meta: {
  31. titleZh: "数字畜牧一张图",
  32. titleBo: "གཙོ་ངོས།"
  33. }
  34. },
  35. {
  36. path: "livestock-resources",
  37. name: "LivestockResources",
  38. component: () => import("../views/livestockResources/index.vue"),
  39. meta: {
  40. titleZh: "畜牧资源",
  41. titleBo: "སྐྱེས་ཁམས་ཐོན་ཁུངས།"
  42. }
  43. },
  44. {
  45. path: "trade-sales",
  46. name: "TradeSales",
  47. component: () => import("../views/tradeSales/index.vue"),
  48. meta: {
  49. titleZh: "交易销售",
  50. titleBo: "འབྲེལ་གཏོང་ཚོང་འབུལ།"
  51. }
  52. },
  53. {
  54. path: "common-prosperity",
  55. name: "CommonProsperity",
  56. component: () => import("../views/commonProsperity/index.vue"),
  57. meta: {
  58. titleZh: "共同富裕统计",
  59. titleBo: "སྤྱི་འགྲོས་ཕྱུག་པོའི་བསྡུས་རྩིས།"
  60. }
  61. },
  62. {
  63. path: "epidemic-risk",
  64. name: "EpidemicRisk",
  65. component: () => import("../views/epidemicRisk/index.vue"),
  66. meta: {
  67. titleZh: "疫病风险统计",
  68. titleBo: "ནད་རིམས་ཉེན་ཁའི་བསྡུས་རྩིས།"
  69. }
  70. },
  71. {
  72. path: ":pathMatch(.*)*",
  73. name: "NotFound",
  74. component: () => import("../views/error/404.vue"),
  75. meta: {
  76. titleZh: "页面不存在",
  77. titleBo: "ཤོག་ངོས་མི་འདུག"
  78. }
  79. }
  80. ]
  81. }
  82. ]
  83. const router = createRouter({
  84. // 与 ruoyi-ui router.base 一致;生产 import.meta.env.BASE_URL 为 /screen/
  85. history: createWebHistory(import.meta.env.BASE_URL),
  86. routes
  87. })
  88. /** 未登录跳转登录页;已登录访问 /login 则进首页;启用免密时先尝试自动登录 */
  89. router.beforeEach(async (to, _from, next) => {
  90. const token = getToken()
  91. if (to.path === "/login") {
  92. if (token) {
  93. next({ path: "/home" })
  94. return
  95. }
  96. if (isScreenAutoLoginEnabled()) {
  97. const ok = await ensureScreenAutoLogin()
  98. if (ok) {
  99. const redirect =
  100. typeof to.query.redirect === "string" ? to.query.redirect : "/home"
  101. next({ path: redirect })
  102. return
  103. }
  104. }
  105. next()
  106. return
  107. }
  108. const needAuth = to.matched.some((r) => r.meta.requiresAuth)
  109. if (needAuth && !getToken()) {
  110. if (isScreenAutoLoginEnabled()) {
  111. const ok = await ensureScreenAutoLogin()
  112. if (ok) {
  113. next()
  114. return
  115. }
  116. }
  117. next({ path: "/login", query: { redirect: to.fullPath } })
  118. return
  119. }
  120. next()
  121. })
  122. /** 根据路由 meta 更新浏览器标签标题 */
  123. router.afterEach((to) => {
  124. const zh = to.meta?.titleZh
  125. const appTitle = import.meta.env.VITE_APP_TITLE || "ruoyi-screen"
  126. document.title = zh ? `${zh} - ${appTitle}` : appTitle
  127. })
  128. export default router