Commit 823aa6f3 by liimin

feat(api): direct 模式后端地址按访问地址动态解析并兼容 http/https

1 parent 12b2021a
...@@ -6,10 +6,10 @@ VITE_API_TARGET=http://192.168.3.197:8788 ...@@ -6,10 +6,10 @@ VITE_API_TARGET=http://192.168.3.197:8788
# ===== 代理 / 直连 开关(切换后端接法只需改这一行,保存后 dev server 会自动重启)===== # ===== 代理 / 直连 开关(切换后端接法只需改这一行,保存后 dev server 会自动重启)=====
# proxy (默认)前端打 /api,由 Vite 代理转发到 VITE_API_TARGET。 # proxy (默认)前端打 /api,由 Vite 代理转发到 VITE_API_TARGET。
# 浏览器只与 localhost:5173 同源通信,跨域由 Vite 服务端完成 → 不触发 CORS。 # 浏览器只与 localhost:5173 同源通信,跨域由 Vite 服务端完成 → 不触发 CORS。
# direct 浏览器直打 http://192.168.1.117:8788,不带 /api 前缀。 # direct 浏览器按「当前访问地址」动态直连后端(默认同源、映射表可改写、VITE_API_TARGET 兜底),不带 /api 前缀。
# 请求变成跨域,必须后端对当前来源返回 CORS 头(ACAO 等),否则浏览器直接拦截。 # 请求变成跨域,必须后端对当前来源返回 CORS 头(ACAO 等),否则浏览器直接拦截。
# 注意:直连时带 /api 前缀会被后端计入签名校验路径 → 与前端签的 /system/auth/login 不一致 → 10011, # 注意:直连时带 /api 前缀会被后端计入签名校验路径 → 与前端签的 /system/auth/login 不一致 → 10011,
# 故 direct 模式的 baseURL 由 VITE_API_TARGET 推导,不含 /api。改完保存即自动重启,无需手动 Ctrl+C。 # 故 direct 模式的 baseURL 由访问地址动态解析(不含 /api)。改完保存即自动重启,无需手动 Ctrl+C。
VITE_API_MODE=direct VITE_API_MODE=direct
# ===== 密钥材料:统一放在项目根 keys/ 目录,不再写进 .env ===== # ===== 密钥材料:统一放在项目根 keys/ 目录,不再写进 .env =====
......
...@@ -13,3 +13,19 @@ ...@@ -13,3 +13,19 @@
- 09:12 补充:聚焦(focus)时淡色背景重现。根因:新版 Chrome 在 input 聚焦时会把 autofill 背景重放一遍,且此时 `:-webkit-autofill` 伪类不稳定匹配(背景改由 UA 内联样式绘制),仅靠伪类盖不住。强化:直接对 `.uj-login-card` 内 `.ant-input` / `.ant-input-affix-wrapper` 的所有态(含 focus/hover/active/focused)强制 `background-color:#fff !important` 压过浏览器与 antd 背景,autofill 伪类仅作 box-shadow 兜底。 - 09:12 补充:聚焦(focus)时淡色背景重现。根因:新版 Chrome 在 input 聚焦时会把 autofill 背景重放一遍,且此时 `:-webkit-autofill` 伪类不稳定匹配(背景改由 UA 内联样式绘制),仅靠伪类盖不住。强化:直接对 `.uj-login-card` 内 `.ant-input` / `.ant-input-affix-wrapper` 的所有态(含 focus/hover/active/focused)强制 `background-color:#fff !important` 压过浏览器与 antd 背景,autofill 伪类仅作 box-shadow 兜底。
- 09:18 修正:用户确认淡蓝/淡黄仍未消除。根因非伪类/聚焦,而是特异性不足——antd 6 CSS-in-JS 把 .ant-input 的 background 注入到 head 末尾(晚于 design.css 加载),原选择器被 antd 规则盖过。修正:选择器重复 class 抬特异性(.ant-input.ant-input 等)+ 补全 antd6 变体(outlined/password/focused),background 改简写 #fff !important;autofill 伪类加 background:#fff !important + inset 阴影 + 9999s 过渡。 - 09:18 修正:用户确认淡蓝/淡黄仍未消除。根因非伪类/聚焦,而是特异性不足——antd 6 CSS-in-JS 把 .ant-input 的 background 注入到 head 末尾(晚于 design.css 加载),原选择器被 antd 规则盖过。修正:选择器重复 class 抬特异性(.ant-input.ant-input 等)+ 补全 antd6 变体(outlined/password/focused),background 改简写 #fff !important;autofill 伪类加 background:#fff !important + inset 阴影 + 9999s 过渡。
- 09:29 最终纠正(用户指出"第一版方向对、后续改崩了"):09:12/09:18 两版**方向全错**——autofill 淡色只能用 `box-shadow: 0 0 0 1000px #fff inset` 盖,改 `background` 无效,所以越改越没效果。聚焦态重现的真正根因是 Chrome 聚焦时用标准 `:autofill`(无前缀)伪类,第一版只盖了 `:-webkit-autofill`,漏了 `:autofill`。最终方案:删除所有 background 强制规则,回归 box-shadow inset #fff 方案,并补 `input:autofill(:hover/:focus/:active)` 覆盖聚焦态。纯 CSS,无 TS 变动。 - 09:29 最终纠正(用户指出"第一版方向对、后续改崩了"):09:12/09:18 两版**方向全错**——autofill 淡色只能用 `box-shadow: 0 0 0 1000px #fff inset` 盖,改 `background` 无效,所以越改越没效果。聚焦态重现的真正根因是 Chrome 聚焦时用标准 `:autofill`(无前缀)伪类,第一版只盖了 `:-webkit-autofill`,漏了 `:autofill`。最终方案:删除所有 background 强制规则,回归 box-shadow inset #fff 方案,并补 `input:autofill(:hover/:focus/:active)` 覆盖聚焦态。纯 CSS,无 TS 变动。
## 后端地址按访问地址动态解析(10:16)
- 诉求:direct 模式下,后端地址随「当前访问地址」自动决定(如访问 youjia.dev.glsx.com.cn → 后端 youjia.dev.glsx.com.cn),支持映射表改写为别的后端,VITE_API_TARGET 仅作兜底。
- 新增 `src/api/core/backend-resolver.ts`(浏览器侧):`resolveBackendOrigin()` 解析优先级 = 映射表 HOST_BACKEND_MAP 命中 → 默认 `window.location.origin`(同源)→ VITE_API_TARGET 兜底(非浏览器/异常 origin)。独立成模块是因为 api-mode.ts 被 Node 侧(vite.config 代理)与浏览器侧共用、禁止用 window;动态解析只能放浏览器侧,只影响 direct 模式。
- `src/api/core/config.ts`:`BACKEND_BASE_URL = API_MODE.useProxy ? '/api' : resolveBackendOrigin()`,direct 的 SIGN_CONFIG.BASE_URL 改用动态值;proxy 模式(开发代理 /api)完全不动,target 仍由 Node 侧 VITE_API_TARGET 定。
- `.env` 注释更新:VITE_API_TARGET 标注为「兜底值」。
- 风险提醒(待超人确认):direct 模式 baseURL 现在是动态 origin,它参与请求签名 canonical 第 2 行,需后端验签口径一致(按当前后端 origin),否则可能 10011。
- tsc --noEmit 通过(EXIT=0)。
## 后端地址兼容 http/https 映射(11:38)
- 诉求:映射值要兼容 http 与 https,不必写死协议。
- backend-resolver.ts 新增 `currentProtocol()`(取 window.location.protocol,非浏览器回退 https)+ `withProtocol(raw)`:已含协议原样;`//host` 协议相对→跟当前页协议;仅 host→补当前页协议。mapped 分支改用 `withProtocol(mapped)`。
- 效果:映射表写 `'192.168.1.237': 'youjia.dev.glsx.com.cn'` 时,http 访问解析为 http 后端、https 访问解析为 https 后端;写全协议则不跟随。
- 已更新映射表示例注释演示三种写法。tsc 通过(EXIT=0)。
/*
* @Author: liimin 17653212@qq.com
* @Date: 2026-10-12 10:21:57
* @LastEditors: liimin 17653212@qq.com
* @LastEditTime: 2026-10-12 11:48:50
* @FilePath: \uj-store\src\api\core\backend-resolver.ts
* @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
*/
// 浏览器侧:根据「当前访问地址」动态解析后端服务地址。
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',
// 'x.dev.cn': 'http://192.168.3.197:8788', // 写全协议则不跟随,固定 http
};
function stripTrailingSlash(url: string): string {
return url.replace(/\/+$/, '');
}
/** 当前页面协议('http:' / 'https:');非浏览器环境回退 https。 */
function currentProtocol(): string {
if (typeof window !== 'undefined' && window.location) return window.location.protocol;
return 'https:';
}
/**
* 给映射值补协议,兼容 http / https:
* - 已含协议(http:// / https://)→ 原样返回;
* - 协议相对(//host)→ 跟随当前页协议;
* - 仅 host(youjia-api.xxx.com / host:port / host/path)→ 跟随当前页协议补上。
* 这样映射表不必写死协议,http 访问解析到 http 后端、https 访问解析到 https 后端。
*/
function withProtocol(raw: string): string {
const v = raw.trim();
if (/^[a-z][a-z0-9+.-]*:\/\//i.test(v)) return v;
if (v.startsWith('//')) return currentProtocol() + v;
return currentProtocol() + '//' + v;
}
/**
* 解析 direct 模式下的后端 origin。
* 仅在浏览器侧调用(config.ts 加载期),Node 侧不会走到此函数。
*/
export function resolveBackendOrigin(): string {
const envTarget = stripTrailingSlash(
String((import.meta.env as Record<string, string | undefined>).VITE_API_TARGET ?? '').trim(),
);
if (typeof window === 'undefined' || !window.location) return envTarget;
const host = window.location.host; // 含端口
const hostNoPort = host.split(':')[0];
const mapped = HOST_BACKEND_MAP[host] ?? HOST_BACKEND_MAP[hostNoPort];
if (mapped) return stripTrailingSlash(withProtocol(mapped));
// 默认同源:访问地址即后端地址(youjia.dev.glsx.com.cn → 后端 youjia.dev.glsx.com.cn)
const origin = window.location.origin;
if (origin && origin !== 'null' && !origin.startsWith('file:')) return origin;
// 极端兜底:非浏览器 / origin 不可用
return envTarget;
}
...@@ -2,21 +2,26 @@ ...@@ -2,21 +2,26 @@
// 规范见《优加门店上架系统 · 前后端 RSA 非对称加签对接说明》 // 规范见《优加门店上架系统 · 前后端 RSA 非对称加签对接说明》
import { resolveApiMode } from './api-mode'; import { resolveApiMode } from './api-mode';
import { resolveBackendOrigin } from './backend-resolver';
// 「代理 / 直连」单一开关。推导逻辑在 ./api-mode.ts,与 vite.config.ts 共用, // 「代理 / 直连」单一开关。推导逻辑在 ./api-mode.ts,与 vite.config.ts 共用,
// 保证 config 里的 server.proxy 与这里的 baseURL 永远一致。 // 保证 config 里的 server.proxy 与这里的 baseURL 永远一致。
export const API_MODE = resolveApiMode(import.meta.env as Record<string, string | undefined>); export const API_MODE = resolveApiMode(import.meta.env as Record<string, string | undefined>);
// direct 模式下 baseURL 不再写死 VITE_API_TARGET,而是按「当前访问地址」动态推导
// (映射表优先、默认同源、VITE_API_TARGET 兜底,见 backend-resolver.ts);
// proxy 模式仍是同源 '/api',由 Node 侧 Vite 代理转发到 VITE_API_TARGET,与此无关。
const BACKEND_BASE_URL = API_MODE.useProxy ? '/api' : resolveBackendOrigin();
export const SIGN_CONFIG = { export const SIGN_CONFIG = {
/** 应用标识,须在服务端 app-ids 白名单内 */ /** 应用标识,须在服务端 app-ids 白名单内 */
APP_ID: 'youjia-web', APP_ID: 'youjia-web',
/** 签名算法,默认 SHA256withRSA */ /** 签名算法,默认 SHA256withRSA */
ALGORITHM: 'SHA256withRSA' as const, ALGORITHM: 'SHA256withRSA' as const,
/** 接口 baseURL(参与请求签名原文第 2 行)。 /** 接口 baseURL(参与请求签名原文第 2 行)。
* 由 VITE_API_MODE 推导,不要再手工填 VITE_API_BASE_URL:
* proxy → '/api'(相对路径,由 Vite 代理转发到 VITE_API_TARGET) * proxy → '/api'(相对路径,由 Vite 代理转发到 VITE_API_TARGET)
* direct → 后端完整地址(无 /api 前缀;带 /api 会被后端计入签名路径 → 10011) */ * direct → 按访问地址动态解析的后端完整地址(无 /api 前缀;带 /api 会被后端计入签名路径 → 10011) */
BASE_URL: API_MODE.baseURL, BASE_URL: BACKEND_BASE_URL,
}; };
// 是否启用「请求加签 + 响应验签」整套机制。 // 是否启用「请求加签 + 响应验签」整套机制。
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!