mirror of
				https://github.com/soybeanjs/soybean-admin.git
				synced 2025-11-04 07:43:42 +08:00 
			
		
		
		
	fix(projects): 修复多个后端服务时的本地代理
This commit is contained in:
		
							
								
								
									
										2
									
								
								.env
									
									
									
									
									
								
							
							
						
						
									
										2
									
								
								.env
									
									
									
									
									
								
							@@ -7,7 +7,7 @@ VITE_APP_TITLE=Soybean管理系统
 | 
			
		||||
VITE_APP_DESC=SoybeanAdmin是一个中后台管理系统模版
 | 
			
		||||
 | 
			
		||||
# 权限路由模式: static | dynamic
 | 
			
		||||
VITE_AUTH_ROUTE_MODE=dynamic
 | 
			
		||||
VITE_AUTH_ROUTE_MODE=static
 | 
			
		||||
 | 
			
		||||
# 路由首页(根路由重定向), 用于static模式的权限路由,dynamic模式取决于后端返回的路由首页
 | 
			
		||||
VITE_ROUTE_HOME_PATH=/dashboard/analysis
 | 
			
		||||
 
 | 
			
		||||
@@ -4,19 +4,22 @@ type ServiceEnv = Record<ServiceEnvType, ServiceEnvConfig>;
 | 
			
		||||
/** 不同请求服务的环境配置 */
 | 
			
		||||
const serviceEnv: ServiceEnv = {
 | 
			
		||||
  dev: {
 | 
			
		||||
    pattern: '/proxy-pattrn',
 | 
			
		||||
    url: 'http://localhost:8080',
 | 
			
		||||
    secondUrl: 'http://localhost:8081'
 | 
			
		||||
    urlPattern: '/url-pattern',
 | 
			
		||||
    secondUrl: 'http://localhost:8081',
 | 
			
		||||
    secondUrlPattern: '/second-url-pattern'
 | 
			
		||||
  },
 | 
			
		||||
  test: {
 | 
			
		||||
    pattern: '/proxy-pattrn',
 | 
			
		||||
    url: 'http://localhost:8080',
 | 
			
		||||
    secondUrl: 'http://localhost:8081'
 | 
			
		||||
    urlPattern: '/url-pattern',
 | 
			
		||||
    secondUrl: 'http://localhost:8081',
 | 
			
		||||
    secondUrlPattern: '/second-url-pattern'
 | 
			
		||||
  },
 | 
			
		||||
  prod: {
 | 
			
		||||
    pattern: '/proxy-pattrn',
 | 
			
		||||
    url: 'http://localhost:8080',
 | 
			
		||||
    secondUrl: 'http://localhost:8081'
 | 
			
		||||
    urlPattern: '/url-pattern',
 | 
			
		||||
    secondUrl: 'http://localhost:8081',
 | 
			
		||||
    secondUrlPattern: '/second-url-pattern'
 | 
			
		||||
  }
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -9,15 +9,15 @@ export function createViteProxy(isOpenProxy: boolean, envConfig: ServiceEnvConfi
 | 
			
		||||
  if (!isOpenProxy) return undefined;
 | 
			
		||||
 | 
			
		||||
  const proxy: Record<string, string | ProxyOptions> = {
 | 
			
		||||
    [envConfig.pattern]: {
 | 
			
		||||
    [envConfig.urlPattern]: {
 | 
			
		||||
      target: envConfig.url,
 | 
			
		||||
      changeOrigin: true,
 | 
			
		||||
      rewrite: path => path.replace(new RegExp(`^${envConfig.pattern}`), '')
 | 
			
		||||
      rewrite: path => path.replace(new RegExp(`^${envConfig.urlPattern}`), '')
 | 
			
		||||
    },
 | 
			
		||||
    [envConfig.pattern]: {
 | 
			
		||||
    [envConfig.secondUrlPattern]: {
 | 
			
		||||
      target: envConfig.secondUrl,
 | 
			
		||||
      changeOrigin: true,
 | 
			
		||||
      rewrite: path => path.replace(new RegExp(`^${envConfig.pattern}`), '')
 | 
			
		||||
      rewrite: path => path.replace(new RegExp(`^${envConfig.secondUrlPattern}`), '')
 | 
			
		||||
    }
 | 
			
		||||
  };
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -1,12 +1,12 @@
 | 
			
		||||
import { getServiceEnvConfig } from '~/.env-config';
 | 
			
		||||
import { createRequest } from './request';
 | 
			
		||||
 | 
			
		||||
const { pattern, url, secondUrl } = getServiceEnvConfig(import.meta.env);
 | 
			
		||||
const { url, urlPattern, secondUrl, secondUrlPattern } = getServiceEnvConfig(import.meta.env);
 | 
			
		||||
 | 
			
		||||
const isHttpProxy = import.meta.env.VITE_HTTP_PROXY === 'Y';
 | 
			
		||||
 | 
			
		||||
export const request = createRequest({ baseURL: isHttpProxy ? pattern : url });
 | 
			
		||||
export const request = createRequest({ baseURL: isHttpProxy ? urlPattern : url });
 | 
			
		||||
 | 
			
		||||
export const secondRequest = createRequest({ baseURL: isHttpProxy ? pattern : secondUrl });
 | 
			
		||||
export const secondRequest = createRequest({ baseURL: isHttpProxy ? secondUrlPattern : secondUrl });
 | 
			
		||||
 | 
			
		||||
export const mockRequest = createRequest({ baseURL: '/mock' });
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										6
									
								
								src/typings/env.d.ts
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										6
									
								
								src/typings/env.d.ts
									
									
									
									
										vendored
									
									
								
							@@ -8,12 +8,14 @@ type ServiceEnvType = 'dev' | 'test' | 'prod';
 | 
			
		||||
 | 
			
		||||
/** 后台服务的环境配置 */
 | 
			
		||||
interface ServiceEnvConfig {
 | 
			
		||||
  /** 匹配路径的正则字符串, 用于拦截地址转发代理(任意以 /开头 + 字符串, 单个/不起作用) */
 | 
			
		||||
  pattern: '/proxy-pattrn';
 | 
			
		||||
  /** 请求地址 */
 | 
			
		||||
  url: string;
 | 
			
		||||
  /** 匹配路径的正则字符串, 用于拦截地址转发代理(任意以 /开头 + 字符串, 单个/不起作用) */
 | 
			
		||||
  urlPattern: '/url-pattern';
 | 
			
		||||
  /** 另一个后端请求地址(有多个不同的后端服务时) */
 | 
			
		||||
  secondUrl: string;
 | 
			
		||||
  /** 匹配路径的正则字符串, 用于拦截地址转发代理(任意以 /开头 + 字符串, 单个/不起作用) */
 | 
			
		||||
  secondUrlPattern: '/second-url-pattern';
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
interface ImportMetaEnv {
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user