diff --git a/Dockerfile.k3s b/Dockerfile.k3s index 0a93a1b..6d3ac67 100644 --- a/Dockerfile.k3s +++ b/Dockerfile.k3s @@ -1,5 +1,5 @@ # k3s / Kaniko:静态 SPA(方案 A)。API 由浏览器按站点域名直连 -# app.test.ideship.com → api.test.ideship.com;其余 → api.ideship.com +# test.ideship.com → api.test.ideship.com;其余 → api.ideship.com # 镜像内 nginx 不做反代;也可用构建期 VITE_API_ORIGIN / VITE_API_ADMIN_URL 覆盖 FROM docker.m.daocloud.io/library/node:20-alpine AS build WORKDIR /app diff --git a/src/utils/rewriteBrowserApiPath.ts b/src/utils/rewriteBrowserApiPath.ts index d3998b7..8431aeb 100644 --- a/src/utils/rewriteBrowserApiPath.ts +++ b/src/utils/rewriteBrowserApiPath.ts @@ -1,7 +1,7 @@ /** * 方案 A:浏览器直连 API 网关 PATHBASE;开发仍走 Vite 代理旧路径。 * - prod:https://api.ideship.com - * - test(app.test.ideship.com):https://api.test.ideship.com + * - test(test.ideship.com):https://api.test.ideship.com * - 可用 VITE_API_ORIGIN 覆盖 */ @@ -21,15 +21,20 @@ const SPA_API_PREFIXES = [ const DEFAULT_PROD_API = 'https://api.ideship.com' const DEFAULT_TEST_API = 'https://api.test.ideship.com' +function isTestFrontHost(hostname: string): boolean { + return ( + hostname === 'test.ideship.com' || + hostname === 'app.test.ideship.com' || // 旧域名兼容 + hostname.endsWith('.test.ideship.com') + ) +} + /** 生产/测试 API 根(无尾斜杠) */ export function prodApiOrigin(): string { const override = (import.meta.env.VITE_API_ORIGIN as string | undefined)?.trim() if (override) return override.replace(/\/$/, '') - if (typeof window !== 'undefined') { - const h = window.location.hostname - if (h === 'app.test.ideship.com' || h.endsWith('.test.ideship.com')) { - return DEFAULT_TEST_API - } + if (typeof window !== 'undefined' && isTestFrontHost(window.location.hostname)) { + return DEFAULT_TEST_API } return DEFAULT_PROD_API }