From c08f6f0f5907ce63bc219a83613d571497a1f3cb Mon Sep 17 00:00:00 2001 From: yangsy Date: Tue, 4 Nov 2025 22:12:23 +0800 Subject: [PATCH] chore: proxy config --- vite.config.ts | 47 +++++++++++++++++++++++++---------------------- 1 file changed, 25 insertions(+), 22 deletions(-) diff --git a/vite.config.ts b/vite.config.ts index 54d8fed..76f26be 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -6,9 +6,10 @@ import vueJsx from '@vitejs/plugin-vue-jsx'; import vueDevTools from 'vite-plugin-vue-devtools'; const line10ApiProxyList: [string, string][] = [ - ['/ws', 'ws://10.18.128.10:18103'], ['/minio', 'http://10.18.128.10:9000'], + ['/api', 'http://10.18.128.10:18760/api'], + ['/1075/api', 'http://10.18.128.10:18760/api'], ['/1001/api', 'http://10.18.129.10:18760/api'], @@ -47,39 +48,41 @@ const line10ApiProxyList: [string, string][] = [ ['/1031/api', 'http://10.18.189.10:18760/api'], ]; const apiProxyList: [string, string][] = [ - // 办公室 - // ['/minio', 'http://172.16.6.248:9002'], - // ['/api', 'http://172.16.6.113:18760/api'], - // ['/113/api', 'http://172.16.6.113:18760/api'], - // ['/114/api', 'http://172.16.6.114:18760/api'], - // ...line4ApiProxyList, + // ...line10ApiProxyList, ]; +const wsProxyList: [string, string][] = [ + // + ['/ws', 'ws://10.18.128.10:18103/ws'], +]; // https://vite.dev/config/ export default defineConfig((/* { command, mode } */) => { const viteProxy: Record = {}; apiProxyList.forEach((apiProxy) => { - const [prefix, target] = apiProxy; - if (prefix === '/ws') { - // from Claude4: 对于WebSocket代理,不应该使用rewrite函数。 - // WebSocket握手和HTTP请求的处理机制不同,rewrite主要是为HTTP请求设计的。 - // 会代理到 ws://:/ws - viteProxy[prefix] = { - target, - changeOrigin: true, - ws: true, - rewriteWsOrigin: true, - }; - return; - } - viteProxy[prefix] = { + const [key, target] = apiProxy; + viteProxy[key] = { target, changeOrigin: true, rewrite: (path) => { console.log(new Date().toLocaleString()); console.log(`请求路径: ${path}`); - const rewrittenPath = path.replace(new RegExp(`^${prefix}`), ''); + const rewrittenPath = path.replace(new RegExp(`^${key}`), ''); + console.log(`将代理到: ${target}${rewrittenPath}`); + return rewrittenPath; + }, + }; + }); + wsProxyList.forEach((wsProxy) => { + const [key, target] = wsProxy; + viteProxy[key] = { + target, + changeOrigin: true, + ws: true, + rewrite: (path) => { + console.log(new Date().toLocaleString()); + console.log(`请求路径: ${path}`); + const rewrittenPath = path.replace(new RegExp(`^${key}`), ''); console.log(`将代理到: ${target}${rewrittenPath}`); return rewrittenPath; },