From b83541d4834e65f35f2eae8552b7d5b91fd209e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8F=B2=E9=B8=BD?= <1020103647@qq.com> Date: Sat, 18 May 2024 17:24:21 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20add=20VITE=5FAPP=5FPROXY=20=E6=8E=A7?= =?UTF-8?q?=E5=88=B6H5=E4=BB=A3=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- env/.env | 2 ++ src/env.d.ts | 2 ++ src/interceptors/request.ts | 11 +++++++++++ src/types/global.d.ts | 2 ++ vite.config.ts | 25 +++++++++++++++++-------- 5 files changed, 34 insertions(+), 8 deletions(-) diff --git a/env/.env b/env/.env index 8f89c8f..93a53d6 100644 --- a/env/.env +++ b/env/.env @@ -3,6 +3,8 @@ VITE_APP_PORT = 9000 # h5部署网站的base,配置到 manifest.config.ts 里的 h5.router.base VITE_APP_PUBLIC_BASE=/ +# h5是否需要配置代理 +VITE_APP_PROXY=true VITE_UNI_APPID = 'H57F2ACE4' VITE_WX_APPID = 'wxa2abb91f64032a2b' diff --git a/src/env.d.ts b/src/env.d.ts index f19eda8..e98d4e6 100644 --- a/src/env.d.ts +++ b/src/env.d.ts @@ -15,6 +15,8 @@ interface ImportMetaEnv { readonly VITE_SERVER_PORT: string /** 后台接口地址 */ readonly VITE_SERVER_BASEURL: string + /** H5是否需要代理 */ + readonly VITE_APP_PROXY: boolean /** 上传图片地址 */ readonly VITE_UPLOAD_BASEURL: string /** 是否清除console */ diff --git a/src/interceptors/request.ts b/src/interceptors/request.ts index 5ae62f3..499285e 100644 --- a/src/interceptors/request.ts +++ b/src/interceptors/request.ts @@ -27,7 +27,18 @@ const httpInterceptor = { } // 非 http 开头需拼接地址 if (!options.url.startsWith('http')) { + // #ifdef H5 + console.log(__VITE_APP_PROXY__) + if (JSON.parse(__VITE_APP_PROXY__)) { + // 啥都不需要做 + } else { + options.url = baseUrl + options.url + } + // #endif + // 非H5正常拼接 + // #ifndef H5 options.url = baseUrl + options.url + // #endif // TIPS: 如果需要对接多个后端服务,也可以在这里处理,拼接成所需要的地址 } // 1. 请求超时 diff --git a/src/types/global.d.ts b/src/types/global.d.ts index 2e67ed8..c243ef0 100644 --- a/src/types/global.d.ts +++ b/src/types/global.d.ts @@ -12,3 +12,5 @@ declare const __UNI_PLATFORM__: | 'quickapp-webview' | 'quickapp-webview-huawei' | 'quickapp-webview-union' + +declare const __VITE_APP_PROXY__: 'true' | 'false' diff --git a/vite.config.ts b/vite.config.ts index 24acf1c..f2feb8b 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -35,7 +35,13 @@ export default ({ command, mode }) => { console.log('UNI_PLATFORM -> ', UNI_PLATFORM) // 得到 mp-weixin, h5, app 等 const env = loadEnv(mode, path.resolve(process.cwd(), 'env')) - const { VITE_APP_PORT, VITE_SERVER_BASEURL, VITE_DELETE_CONSOLE, VITE_SHOW_SOURCEMAP } = env + const { + VITE_APP_PORT, + VITE_SERVER_BASEURL, + VITE_DELETE_CONSOLE, + VITE_SHOW_SOURCEMAP, + VITE_APP_PROXY, + } = env console.log('环境变量 env -> ', env) return defineConfig({ @@ -87,6 +93,7 @@ export default ({ command, mode }) => { ], define: { __UNI_PLATFORM__: JSON.stringify(UNI_PLATFORM), + __VITE_APP_PROXY__: JSON.stringify(VITE_APP_PROXY), }, css: { postcss: { @@ -110,13 +117,15 @@ export default ({ command, mode }) => { hmr: true, port: Number.parseInt(VITE_APP_PORT, 10), // 仅 H5 端生效,其他端不生效(其他端走build,不走devServer) - // proxy: { - // '/api': { - // target: VITE_SERVER_BASEURL, - // changeOrigin: true, - // rewrite: (path) => path.replace(/^\/api/, ''), - // }, - // }, + proxy: JSON.parse(VITE_APP_PROXY) + ? { + '/api': { + target: VITE_SERVER_BASEURL, + changeOrigin: true, + rewrite: (path) => path.replace(/^\/api/, ''), + }, + } + : undefined, }, build: { // 方便非h5端调试