Commit 0b7bc11b by liimin

feat(api): 本地/开发地址自动回退 .env 后端,避免开发者 IP 请求失败

1 parent 28500a8a
...@@ -38,3 +38,10 @@ ...@@ -38,3 +38,10 @@
- `src/api/core/http.ts`:拦截器顶部按 `API_CONTEXT_PATH` 给相对路径 url 补前缀(绝对 URL/已带前缀跳过)。proxy 模式实际请求 `/api/youjia/...`(vite 代理 rewrite 仅剥 /api,保留 /youjia → 转发 /youjia/...);direct 模式 `origin/youjia/...`。两种模式的 canonPath 均正确得到 `/youjia/system/...`,与后端一致。 - `src/api/core/http.ts`:拦截器顶部按 `API_CONTEXT_PATH` 给相对路径 url 补前缀(绝对 URL/已带前缀跳过)。proxy 模式实际请求 `/api/youjia/...`(vite 代理 rewrite 仅剥 /api,保留 /youjia → 转发 /youjia/...);direct 模式 `origin/youjia/...`。两种模式的 canonPath 均正确得到 `/youjia/system/...`,与后端一致。
- `.env` 新增 `VITE_API_CONTEXT_PATH=/youjia` 及说明。 - `.env` 新增 `VITE_API_CONTEXT_PATH=/youjia` 及说明。
- 全程唯一 axios 实例(http.ts),无绕过拦截器的 fetch;initSign 拉公钥也统一带上前缀。tsc --noEmit 通过(EXIT=0)。 - 全程唯一 axios 实例(http.ts),无绕过拦截器的 fetch;initSign 拉公钥也统一带上前缀。tsc --noEmit 通过(EXIT=0)。
## backend-resolver 迭代:本地开发回退 + DEV 判断(15:27)
- 问题:原逻辑映射表未命中→默认同源,开发者用 192.168.x.x/localhost 访问前端时打到自己机器上不存在的后端,需每人把自己 IP 写进映射表,不可持续。
- 第一版(15:19):新增 isLocalDevHost(hostNoPort) 判断 localhost/127.0.0.1/私有网段(10.x/172.16-31.x/192.168.x/169.254.x),命中则回退 VITE_API_TARGET;映射表清空开发者 IP 示例。
- 第二版(15:27,用户提议用 DEV 判断):isLocalDevHost 按访问地址、import.meta.env.DEV 按构建模式,维度不同不能简单互替(反例:测试服务器以 IP 部署 build 产物,DEV=false 但 IP 应回退)。最终合并为 (import.meta.env.DEV || isLocalDevHost(hostNoPort)) && envTarget → 回退 envTarget;开发者 pnpm dev 零配置,测试服务器 IP 部署也能兜底。
- 文件:src/api/core/backend-resolver.ts。tsc --noEmit 通过(EXIT=0)。
...@@ -7,10 +7,13 @@ ...@@ -7,10 +7,13 @@
* @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
*/ */
// 浏览器侧:根据「当前访问地址」动态解析后端服务地址。 // 浏览器侧:根据「当前访问地址」动态解析后端服务地址。
// 映射表用于「访问域名 → 不同后端」的显式改写(可选,优先级最高)。
// 开发者无需在此登记自己的 IP —— 本地 / 开发地址(localhost、127.0.0.1、私有网段 IP 如 192.168.x.x)
// 会自动回退到 .env 的 VITE_API_TARGET(默认测试服务器),避免「用自己 IP 访问前端却打到自己机器上
// 根本不存在的后端」导致请求全部失败。只有正式域名(youjia.dev.glsx.com.cn 等)才走同源。
const HOST_BACKEND_MAP: Record<string, string> = { const HOST_BACKEND_MAP: Record<string, string> = {
'192.168.1.237': '192.168.3.197:8788', // 省略协议 → 访问 http 拼 http、访问 https 拼 https // 例:把某个生产/预发前端域名映射到指定后端(不受本地回退规则影响)
// 'localhost:5173': '192.168.3.197:8788', // 'youjia.dev.glsx.com.cn': 'youjia-api.glsx.com.cn',
// 'x.dev.cn': 'http://192.168.3.197:8788', // 写全协议则不跟随,固定 http
}; };
function stripTrailingSlash(url: string): string { function stripTrailingSlash(url: string): string {
...@@ -24,6 +27,26 @@ function currentProtocol(): string { ...@@ -24,6 +27,26 @@ function currentProtocol(): string {
} }
/** /**
* 判断 host(不含端口)是否为「本地 / 开发地址」,需要回退到 .env 默认服务端:
* - localhost / 127.0.0.1 / ::1
* - 私有网段:10.x.x.x、172.16-31.x.x、192.168.x.x、169.254.x.x(链路本地)
* 这些地址下开发者机器上通常没有后端,用同源 IP 访问必然失败。
*/
function isLocalDevHost(hostNoPort: string): boolean {
const h = hostNoPort.toLowerCase();
if (h === 'localhost' || h === '127.0.0.1' || h === '::1' || h === '[::1]') return true;
const m = h.match(/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/);
if (!m) return false;
const a = Number(m[1]);
const b = Number(m[2]);
if (a === 10) return true; // 10.0.0.0/8
if (a === 172 && b >= 16 && b <= 31) return true; // 172.16.0.0/12
if (a === 192 && b === 168) return true; // 192.168.0.0/16
if (a === 169 && b === 254) return true; // 169.254.0.0/16 链路本地
return false;
}
/**
* 给映射值补协议,兼容 http / https: * 给映射值补协议,兼容 http / https:
* - 已含协议(http:// / https://)→ 原样返回; * - 已含协议(http:// / https://)→ 原样返回;
* - 协议相对(//host)→ 跟随当前页协议; * - 协议相对(//host)→ 跟随当前页协议;
...@@ -38,8 +61,9 @@ function withProtocol(raw: string): string { ...@@ -38,8 +61,9 @@ function withProtocol(raw: string): string {
} }
/** /**
* 解析 direct 模式下的后端 origin。 * 解析 direct 模式下的后端 origin。仅在浏览器侧调用(config.ts 加载期),Node 侧不会走到此函数。
* 仅在浏览器侧调用(config.ts 加载期),Node 侧不会走到此函数。 * 优先级:① 映射表 HOST_BACKEND_MAP(显式改写,最高)② 本地/开发地址(localhost、私有网段 IP)→ .env 的 VITE_API_TARGET
* ③ 非本地(正式域名)→ 同源 window.location.origin ④ 兜底 → VITE_API_TARGET
*/ */
export function resolveBackendOrigin(): string { export function resolveBackendOrigin(): string {
const envTarget = stripTrailingSlash( const envTarget = stripTrailingSlash(
...@@ -54,10 +78,14 @@ export function resolveBackendOrigin(): string { ...@@ -54,10 +78,14 @@ export function resolveBackendOrigin(): string {
const mapped = HOST_BACKEND_MAP[host] ?? HOST_BACKEND_MAP[hostNoPort]; const mapped = HOST_BACKEND_MAP[host] ?? HOST_BACKEND_MAP[hostNoPort];
if (mapped) return stripTrailingSlash(withProtocol(mapped)); if (mapped) return stripTrailingSlash(withProtocol(mapped));
// 默认同源:访问地址即后端地址(youjia.dev.glsx.com.cn → 后端 youjia.dev.glsx.com.cn) // 开发模式(DEV,即 pnpm dev)→ 直接用 .env 默认服务端,开发者无需配自己的 IP;
// 非开发模式但用本地/私有 IP 访问(如测试服务器以 IP 部署)→ 同样回退到默认服务端;
// 正式域名(youjia.dev.glsx.com.cn 等)→ 走下方同源逻辑。
if ((import.meta.env.DEV || isLocalDevHost(hostNoPort)) && envTarget) return envTarget;
const origin = window.location.origin; const origin = window.location.origin;
if (origin && origin !== 'null' && !origin.startsWith('file:')) return origin; if (origin && origin !== 'null' && !origin.startsWith('file:')) return origin;
// 极端兜底:非浏览器 / origin 不可用 // 极端兜底:非浏览器 / origin 不可用 / env 未配置
return envTarget; return envTarget;
} }
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!