index.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. import { createRouter, createWebHashHistory } from 'vue-router'
  2. import Index from '../views/Index.vue'
  3. import Chat from '../views/Chat.vue'
  4. import AIWriting from '../views/AIWriting.vue'
  5. import Admin from '../views/Admin.vue'
  6. import ExamWorkshop from '../views/ExamWorkshop.vue'
  7. import HazardDetection from '../views/HazardDetection.vue'
  8. import PolicyDocument from '../views/PolicyDocument.vue'
  9. import SafetyHazard from '../views/SafetyHazard.vue'
  10. import TemplateEditor from '../views/TemplateEditor.vue'
  11. import MobileIndex from '../views/mobile/m-Index.vue'
  12. import MobileChat from '../views/mobile/m-Chat.vue'
  13. import MobileSafetyHazard from '../views/mobile/m-SafetyHazard.vue'
  14. import MobileExamWorkshop from '../views/mobile/m-ExamWorkshop.vue'
  15. import MobileHazardDetection from '../views/mobile/m-HazardDetection.vue'
  16. import MobileAIWriting from '../views/mobile/m-AIWriting.vue'
  17. import MobilePolicyDocument from '../views/mobile/m-PolicyDocument.vue'
  18. import NotFound from '../views/NotFound.vue'
  19. import { isMobile } from '../utils/mobileDetector.js'
  20. // ===== 注释掉:登录页已废弃,改用票据认证 =====
  21. // import Login from '../views/Login.vue'
  22. // import { isLoggedIn } from '../utils/auth.js'
  23. const routes = [
  24. {
  25. path: '/',
  26. name: 'Index',
  27. component: Index,
  28. beforeEnter: (to, from, next) => {
  29. // 如果是移动设备,重定向到移动端首页
  30. if (isMobile()) {
  31. console.log('📱 检测到移动设备,重定向到 /mobile')
  32. console.log('📍 原始 URL:', window.location.href)
  33. console.log('📍 原始查询参数:', to.query)
  34. // 重定向时保留所有查询参数(包括票据)
  35. // 使用 replace 而不是 push,避免在历史记录中留下 /
  36. next({
  37. path: '/mobile',
  38. query: to.query, // 保留查询参数
  39. replace: true
  40. });
  41. } else {
  42. next();
  43. }
  44. }
  45. },
  46. {
  47. path: '/mobile',
  48. name: 'MobileIndex',
  49. component: MobileIndex
  50. },
  51. {
  52. path: '/mobile/chat',
  53. name: 'MobileChat',
  54. component: MobileChat
  55. },
  56. {
  57. path: '/mobile/safety-hazard',
  58. name: 'MobileSafetyHazard',
  59. component: MobileSafetyHazard
  60. },
  61. {
  62. path: '/mobile/exam-workshop',
  63. name: 'MobileExamWorkshop',
  64. component: MobileExamWorkshop
  65. },
  66. {
  67. path: '/mobile/hazard-detection',
  68. name: 'MobileHazardDetection',
  69. component: MobileHazardDetection
  70. },
  71. {
  72. path: '/mobile/ai-writing',
  73. name: 'MobileAIWriting',
  74. component: MobileAIWriting
  75. },
  76. {
  77. path: '/mobile/policy-document',
  78. name: 'MobilePolicyDocument',
  79. component: MobilePolicyDocument
  80. },
  81. {
  82. path: '/chat',
  83. name: 'Chat',
  84. component: Chat
  85. },
  86. {
  87. path: '/ai-writing',
  88. name: 'AIWriting',
  89. component: AIWriting
  90. },
  91. {
  92. path: '/admin',
  93. name: 'Admin',
  94. component: Admin
  95. },
  96. {
  97. path: '/exam-workshop',
  98. name: 'ExamWorkshop',
  99. component: ExamWorkshop
  100. },
  101. {
  102. path: '/hazard-detection',
  103. name: 'HazardDetection',
  104. component: HazardDetection
  105. },
  106. {
  107. path: '/policy-document',
  108. name: 'PolicyDocument',
  109. component: PolicyDocument
  110. },
  111. {
  112. path: '/safety-hazard',
  113. name: 'SafetyHazard',
  114. component: SafetyHazard
  115. },
  116. {
  117. path: '/template-editor',
  118. name: 'TemplateEditor',
  119. component: TemplateEditor
  120. },
  121. // ===== 注释掉:登录路由已废弃,改用票据认证 =====
  122. // {
  123. // path: '/login',
  124. // name: 'Login',
  125. // component: Login
  126. // }
  127. // ===== 新增:404错误页面 =====
  128. {
  129. path: '/404',
  130. name: 'NotFound',
  131. component: NotFound
  132. },
  133. // 捕获所有未定义的路由
  134. {
  135. path: '/:pathMatch(.*)*',
  136. redirect: '/404'
  137. }
  138. ]
  139. const router = createRouter({
  140. history: createWebHashHistory(),
  141. routes
  142. })
  143. // 全局前置守卫 - 系统停机维护拦截
  144. const isMaintenance = true; // 是否开启停机维护模式
  145. router.beforeEach((to, from, next) => {
  146. if (isMaintenance) {
  147. // 允许访问的维护页面(即显示了公告的主界面)
  148. const allowedPaths = ['/', '/mobile'];
  149. if (!allowedPaths.includes(to.path)) {
  150. console.log('🚧 系统维护中,拦截访问:', to.path);
  151. // 根据设备类型重定向到对应的主界面(带上原有参数防丢失)
  152. if (isMobile()) {
  153. return next({ path: '/mobile', query: to.query, replace: true });
  154. } else {
  155. return next({ path: '/', query: to.query, replace: true });
  156. }
  157. }
  158. }
  159. next();
  160. })
  161. // ===== 注释掉:登录认证守卫已废弃,改用票据认证 =====
  162. // 全局前置守卫 - 登录认证
  163. // router.beforeEach((to, from, next) => {
  164. // // 如果是访问登录页,直接放行
  165. // if (to.path === '/login') {
  166. // next()
  167. // return
  168. // }
  169. //
  170. // // 检查是否已登录
  171. // if (!isLoggedIn()) {
  172. // // 未登录,重定向到登录页,并保存原始访问路径
  173. // console.log('🔒 未登录,重定向到登录页')
  174. // next({
  175. // path: '/login',
  176. // query: { redirect: to.fullPath }
  177. // })
  178. // } else {
  179. // // 已登录,放行
  180. // next()
  181. // }
  182. // })
  183. export default router