Commit 6e26edb240fb5c0e91c1528bdf297e4a616ab94e

Authored by 杨鸣坤
1 parent d8a556a9

fix: 切换至hash路由并修复corpCode获取与API代理

- 路由改用createWebHashHistory,适配/iot/基础路径
- 修复hash模式下URL参数无法获取的问题,corpCode改用computed从route.query读取
- withCorpCode统一调用getApiUrl补全接口地址
- 更新vite代理配置及base路径
  1 +# 生产环境API基础地址
  2 +VITE_API_BASE=http://182.127.106.19:81/iot-scheduler
@@ -37,11 +37,9 @@ const route = useRoute() @@ -37,11 +37,9 @@ const route = useRoute()
37 const router = useRouter() 37 const router = useRouter()
38 const currentRoute = computed(() => route.path) 38 const currentRoute = computed(() => route.path)
39 39
40 -// 从URL获取corpCode,全局共享  
41 -function getCorpCode() {  
42 - return new URLSearchParams(window.location.search).get('corpCode') || ''  
43 -}  
44 -const corpCode = ref(getCorpCode()) 40 +// 从URL获取corpCode,全局共享(hash模式下参数在#后面,需用route.query)
  41 +// 使用computed确保路由切换时corpCode始终同步
  42 +const corpCode = computed(() => route.query.corpCode || '')
45 provide('corpCode', corpCode) 43 provide('corpCode', corpCode)
46 44
47 // 给URL拼接corpCode 45 // 给URL拼接corpCode
@@ -110,10 +110,13 @@ const dtuSn = computed(() => props.device?._raw?.dtuSn || props.device?.name || @@ -110,10 +110,13 @@ const dtuSn = computed(() => props.device?._raw?.dtuSn || props.device?.name ||
110 110
111 // 从全局获取corpCode 111 // 从全局获取corpCode
112 const corpCode = inject('corpCode') 112 const corpCode = inject('corpCode')
  113 +import { getApiUrl } from '../config/api.js'
  114 +
113 function withCorpCode(url) { 115 function withCorpCode(url) {
114 - if (!corpCode.value) return url  
115 - const sep = url.includes('?') ? '&' : '?'  
116 - return `${url}${sep}corpCode=${encodeURIComponent(corpCode.value)}` 116 + const fullUrl = getApiUrl(url)
  117 + if (!corpCode.value) return fullUrl
  118 + const sep = fullUrl.includes('?') ? '&' : '?'
  119 + return `${fullUrl}${sep}corpCode=${encodeURIComponent(corpCode.value)}`
117 } 120 }
118 121
119 // API 数据 122 // API 数据
@@ -124,10 +124,13 @@ const durationFilter = ref(null) @@ -124,10 +124,13 @@ const durationFilter = ref(null)
124 124
125 // 从全局获取corpCode 125 // 从全局获取corpCode
126 const corpCode = inject('corpCode') 126 const corpCode = inject('corpCode')
  127 +import { getApiUrl } from '../config/api.js'
  128 +
127 function withCorpCode(url) { 129 function withCorpCode(url) {
128 - if (!corpCode.value) return url  
129 - const sep = url.includes('?') ? '&' : '?'  
130 - return `${url}${sep}corpCode=${encodeURIComponent(corpCode.value)}` 130 + const fullUrl = getApiUrl(url)
  131 + if (!corpCode.value) return fullUrl
  132 + const sep = fullUrl.includes('?') ? '&' : '?'
  133 + return `${fullUrl}${sep}corpCode=${encodeURIComponent(corpCode.value)}`
131 } 134 }
132 135
133 // 状态映射 136 // 状态映射
@@ -95,10 +95,13 @@ const dateRange = ref(null) @@ -95,10 +95,13 @@ const dateRange = ref(null)
95 95
96 // 从全局获取corpCode 96 // 从全局获取corpCode
97 const corpCode = inject('corpCode') 97 const corpCode = inject('corpCode')
  98 +import { getApiUrl } from '../config/api.js'
  99 +
98 function withCorpCode(url) { 100 function withCorpCode(url) {
99 - if (!corpCode.value) return url  
100 - const sep = url.includes('?') ? '&' : '?'  
101 - return `${url}${sep}corpCode=${encodeURIComponent(corpCode.value)}` 101 + const fullUrl = getApiUrl(url)
  102 + if (!corpCode.value) return fullUrl
  103 + const sep = fullUrl.includes('?') ? '&' : '?'
  104 + return `${fullUrl}${sep}corpCode=${encodeURIComponent(corpCode.value)}`
102 } 105 }
103 106
104 // 禁用未来日期 107 // 禁用未来日期
@@ -143,10 +143,13 @@ const oeeData = ref(null) @@ -143,10 +143,13 @@ const oeeData = ref(null)
143 143
144 // 从全局获取corpCode 144 // 从全局获取corpCode
145 const corpCode = inject('corpCode') 145 const corpCode = inject('corpCode')
  146 +import { getApiUrl } from '../config/api.js'
  147 +
146 function withCorpCode(url) { 148 function withCorpCode(url) {
147 - if (!corpCode.value) return url  
148 - const sep = url.includes('?') ? '&' : '?'  
149 - return `${url}${sep}corpCode=${encodeURIComponent(corpCode.value)}` 149 + const fullUrl = getApiUrl(url)
  150 + if (!corpCode.value) return fullUrl
  151 + const sep = fullUrl.includes('?') ? '&' : '?'
  152 + return `${fullUrl}${sep}corpCode=${encodeURIComponent(corpCode.value)}`
150 } 153 }
151 154
152 // 布局常量 155 // 布局常量
  1 +// API基础地址
  2 +const API_BASE = import.meta.env.VITE_API_BASE || ''
  3 +
  4 +// 生产环境后端路径不带 /api/ 前缀,这里做兼容处理
  5 +export function getApiUrl(url) {
  6 + const cleanUrl = url.replace(/^\/api\//, '/')
  7 + return API_BASE + cleanUrl
  8 +}
  9 +
  10 +export default API_BASE
1 -import { createRouter, createWebHistory } from 'vue-router' 1 +import { createRouter, createWebHashHistory } from 'vue-router' // 改用 Hash 模式
2 2
3 const routes = [ 3 const routes = [
4 { 4 {
@@ -20,8 +20,8 @@ const routes = [ @@ -20,8 +20,8 @@ const routes = [
20 ] 20 ]
21 21
22 const router = createRouter({ 22 const router = createRouter({
23 - history: createWebHistory(), 23 + history: createWebHashHistory('/iot/'), // 使用 Hash 模式
24 routes 24 routes
25 }) 25 })
26 26
27 -export default router 27 +export default router
@@ -319,10 +319,14 @@ const runStatusFilter = ref('') // runStatus 筛选: ''=全部, '0'=离线, '1'= @@ -319,10 +319,14 @@ const runStatusFilter = ref('') // runStatus 筛选: ''=全部, '0'=离线, '1'=
319 // 从全局获取corpCode(由App.vue提供) 319 // 从全局获取corpCode(由App.vue提供)
320 const corpCode = inject('corpCode') 320 const corpCode = inject('corpCode')
321 321
  322 +import { getApiUrl } from '../config/api.js'
  323 +
322 // 给URL拼接corpCode 324 // 给URL拼接corpCode
323 function withCorpCode(url) { 325 function withCorpCode(url) {
324 - const sep = url.includes('?') ? '&' : '?'  
325 - return `${url}${sep}corpCode=${encodeURIComponent(corpCode.value)}` 326 + const fullUrl = getApiUrl(url)
  327 + if (!corpCode.value) return fullUrl
  328 + const sep = fullUrl.includes('?') ? '&' : '?'
  329 + return `${fullUrl}${sep}corpCode=${encodeURIComponent(corpCode.value)}`
326 } 330 }
327 331
328 // 各状态数量(接口返回后更新) 332 // 各状态数量(接口返回后更新)
@@ -388,10 +388,13 @@ const lampStateFilter = ref('') // 灯状态筛选: ''=全部, '1'=绿, '2'=红, @@ -388,10 +388,13 @@ const lampStateFilter = ref('') // 灯状态筛选: ''=全部, '1'=绿, '2'=红,
388 388
389 // 从全局获取corpCode 389 // 从全局获取corpCode
390 const corpCode = inject('corpCode') 390 const corpCode = inject('corpCode')
  391 +import { getApiUrl } from '../config/api.js'
  392 +
391 function withCorpCode(url) { 393 function withCorpCode(url) {
392 - if (!corpCode.value) return url  
393 - const sep = url.includes('?') ? '&' : '?'  
394 - return `${url}${sep}corpCode=${encodeURIComponent(corpCode.value)}` 394 + const fullUrl = getApiUrl(url)
  395 + if (!corpCode.value) return fullUrl
  396 + const sep = fullUrl.includes('?') ? '&' : '?'
  397 + return `${fullUrl}${sep}corpCode=${encodeURIComponent(corpCode.value)}`
395 } 398 }
396 399
397 // 各颜色数量(接口返回后更新,初始默认值) 400 // 各颜色数量(接口返回后更新,初始默认值)
@@ -2,20 +2,19 @@ import { fileURLToPath, URL } from 'node:url' @@ -2,20 +2,19 @@ import { fileURLToPath, URL } from 'node:url'
2 2
3 import { defineConfig } from 'vite' 3 import { defineConfig } from 'vite'
4 import vue from '@vitejs/plugin-vue' 4 import vue from '@vitejs/plugin-vue'
5 -import vueDevTools from 'vite-plugin-vue-devtools'  
6 5
7 // https://vite.dev/config/ 6 // https://vite.dev/config/
8 export default defineConfig({ 7 export default defineConfig({
  8 + base: '/iot/',
9 plugins: [ 9 plugins: [
10 vue(), 10 vue(),
11 - vueDevTools(),  
12 ], 11 ],
13 server: { 12 server: {
14 proxy: { 13 proxy: {
15 '/api': { 14 '/api': {
16 - target: 'http://127.0.0.1:8080', 15 + target: 'http://182.127.106.19:81',
17 changeOrigin: true, 16 changeOrigin: true,
18 - rewrite: (path) => path.replace(/^\/api/, ''), 17 + rewrite: (path) => path.replace(/^\/api/, '/iot-scheduler'),
19 }, 18 },
20 }, 19 },
21 }, 20 },