diff --git a/.env-config.ts b/.env-config.ts index 10d8c297d..be7f8bf47 100644 --- a/.env-config.ts +++ b/.env-config.ts @@ -4,22 +4,13 @@ type ServiceEnv = Record; /** 不同请求服务的环境配置 */ const serviceEnv: ServiceEnv = { dev: { - url: 'http://localhost:8080', - urlPattern: '/url-pattern', - secondUrl: 'http://localhost:8081', - secondUrlPattern: '/second-url-pattern' + url: 'http://localhost:8080' }, test: { - url: 'http://localhost:8080', - urlPattern: '/url-pattern', - secondUrl: 'http://localhost:8081', - secondUrlPattern: '/second-url-pattern' + url: 'http://localhost:8080' }, prod: { - url: 'http://localhost:8080', - urlPattern: '/url-pattern', - secondUrl: 'http://localhost:8081', - secondUrlPattern: '/second-url-pattern' + url: 'http://localhost:8080' } }; @@ -27,10 +18,13 @@ const serviceEnv: ServiceEnv = { * 获取当前环境模式下的请求服务的配置 * @param env 环境 */ -export function getServiceEnvConfig(env: ImportMetaEnv) { +export function getServiceEnvConfig(env: ImportMetaEnv): ServiceEnvConfigWithProxyPattern { const { VITE_SERVICE_ENV = 'dev' } = env; const config = serviceEnv[VITE_SERVICE_ENV]; - return config; + return { + ...config, + proxyPattern: 'proxy-pattern' + }; } diff --git a/build/config/proxy.ts b/build/config/proxy.ts index f97a4aff0..a220e9d86 100644 --- a/build/config/proxy.ts +++ b/build/config/proxy.ts @@ -5,19 +5,14 @@ import type { ProxyOptions } from 'vite'; * @param isOpenProxy - 是否开启代理 * @param envConfig - env环境配置 */ -export function createViteProxy(isOpenProxy: boolean, envConfig: ServiceEnvConfig) { +export function createViteProxy(isOpenProxy: boolean, envConfig: ServiceEnvConfigWithProxyPattern) { if (!isOpenProxy) return undefined; const proxy: Record = { - [envConfig.urlPattern]: { + [envConfig.proxyPattern]: { target: envConfig.url, changeOrigin: true, - rewrite: path => path.replace(new RegExp(`^${envConfig.urlPattern}`), '') - }, - [envConfig.secondUrlPattern]: { - target: envConfig.secondUrl, - changeOrigin: true, - rewrite: path => path.replace(new RegExp(`^${envConfig.secondUrlPattern}`), '') + rewrite: path => path.replace(new RegExp(`^${envConfig.proxyPattern}`), '') } }; diff --git a/src/service/request/index.ts b/src/service/request/index.ts index 434ff4fe2..56618be72 100644 --- a/src/service/request/index.ts +++ b/src/service/request/index.ts @@ -1,12 +1,10 @@ import { getServiceEnvConfig } from '~/.env-config'; import { createRequest } from './request'; -const { url, urlPattern, secondUrl, secondUrlPattern } = getServiceEnvConfig(import.meta.env); +const { url, proxyPattern } = getServiceEnvConfig(import.meta.env); const isHttpProxy = import.meta.env.VITE_HTTP_PROXY === 'Y'; -export const request = createRequest({ baseURL: isHttpProxy ? urlPattern : url }); - -export const secondRequest = createRequest({ baseURL: isHttpProxy ? secondUrlPattern : secondUrl }); +export const request = createRequest({ baseURL: isHttpProxy ? proxyPattern : url }); export const mockRequest = createRequest({ baseURL: '/mock' }); diff --git a/src/typings/env.d.ts b/src/typings/env.d.ts index 65c774e9e..1a382f87d 100644 --- a/src/typings/env.d.ts +++ b/src/typings/env.d.ts @@ -10,12 +10,16 @@ type ServiceEnvType = 'dev' | 'test' | 'prod'; interface ServiceEnvConfig { /** 请求地址 */ url: string; - /** 匹配路径的正则字符串, 用于拦截地址转发代理(任意以 /开头 + 字符串, 单个/不起作用) */ - urlPattern: '/url-pattern'; - /** 另一个后端请求地址(有多个不同的后端服务时) */ - secondUrl: string; - /** 匹配路径的正则字符串, 用于拦截地址转发代理(任意以 /开头 + 字符串, 单个/不起作用) */ - secondUrlPattern: '/second-url-pattern'; +} + +interface ServiceEnvConfigWithProxyPattern extends ServiceEnvConfig { + /** + * 匹配路径的正则字符串 + * - 用于拦截地址转发代理(任意以 /开头 + 字符串, 单个/不起作用) + * - 和后端请求地址的前缀无关 + * - 有多个后端请求实例时,需要创建不同的值 + */ + proxyPattern: 'proxy-pattern'; } interface ImportMetaEnv {