ALIEZ hace 1 mes
padre
commit
e863ac5eb1
Se han modificado 4 ficheros con 35 adiciones y 13 borrados
  1. 1 1
      .env.development
  2. 8 1
      .env.production
  3. 3 0
      ecosystem.config.cjs
  4. 23 11
      src/lib/env.ts

+ 1 - 1
.env.development

@@ -8,7 +8,7 @@ API_PROXY_TARGET=http://192.168.0.33:8005
 NEXT_PUBLIC_REMITTANCE_API_BASE_URL=/api-backend-remittance
 API_PROXY_TARGET_REMITTANCE=http://192.168.0.33:8504
 
-# 与当前访问站点同源(浏览器用页面域名;服务端需配 NEXT_PUBLIC_SITE_URL)
+# __ORIGIN__:浏览器 → 当前域 + /api-backend;SSR → NEXT_PUBLIC_SITE_URL + /api-backend。端口仍由上面 API_PROXY_* 决定。
 NEXT_PUBLIC_API_BASE_URL_TEST=__ORIGIN__
 NEXT_PUBLIC_API_BASE_URL_PRODUCTION=__ORIGIN__
 

+ 8 - 1
.env.production

@@ -1,8 +1,15 @@
 NEXT_PUBLIC_APP_ENV=production
 
 NEXT_PUBLIC_API_BASE_URL_LOCAL=http://127.0.0.1:8000
-# 与当前访问站点同源(浏览器用页面域名;SSR/Node 请求需配 NEXT_PUBLIC_SITE_URL)
+# 动态域名:浏览器为「当前域 + /api-backend」;SSR 需 NEXT_PUBLIC_SITE_URL。端口写在下面两条代理里(8005 / 8504),勿写进公网 URL。
 NEXT_PUBLIC_API_BASE_URL_TEST=__ORIGIN__
 NEXT_PUBLIC_API_BASE_URL_PRODUCTION=__ORIGIN__
 
+# 同源反写前缀(与 next.config rewrites 一致);不设则根据主 base 自动推导为 …/api-backend-remittance
+NEXT_PUBLIC_REMITTANCE_API_BASE_URL=/api-backend-remittance
+
+# next build 时写入 rewrites:主业务 :8005、汇款 :8504(部署时在 CI/机器环境变量里改成真实内网地址)
+API_PROXY_TARGET=http://127.0.0.1:8005
+API_PROXY_TARGET_REMITTANCE=http://127.0.0.1:8504
+
 

+ 3 - 0
ecosystem.config.cjs

@@ -13,6 +13,9 @@ const path = require("path");
  *
  * 若 API 使用 __ORIGIN__(与站点同源),服务端渲染发请求时还需在构建或运行环境提供
  * NEXT_PUBLIC_SITE_URL(例如 https://你的前端域名),否则服务端拿不到页面域名。
+ *
+ * 主业务 / 汇款端口由 Next rewrites 决定:`API_PROXY_TARGET` → 如 :8005,`API_PROXY_TARGET_REMITTANCE` → 如 :8504。
+ * 这两项在 **next build** 时读入 next.config;打包流水线或本机构建前需注入,与公网域名解耦。
  */
 
 module.exports = {

+ 23 - 11
src/lib/env.ts

@@ -10,10 +10,11 @@
  *
  * 优先使用 NEXT_PUBLIC_API_BASE_URL 覆盖(CI / 临时调试)。
  *
- * 动态域名:将对应环境的地址设为字面量 `__ORIGIN__`(不区分大小写)时,
- * - 浏览器:使用 `window.location.origin`(与当前访问域名一致)
- * - 服务端(RSC / Route Handler 等发 axios):需配置 `NEXT_PUBLIC_SITE_URL`(完整站点根,如 https://app.example.com);
- *   部署在 Vercel 时可回退使用 `VERCEL_URL`(自动加 https://)
+ * 动态域名:将对应环境的地址设为字面量 `__ORIGIN__`(不区分大小写)时,主接口 base 为「当前站点 + /api-backend」,
+ * 由 Next `rewrites` 转到 `API_PROXY_TARGET`(主业务端口,如 :8005),不要把端口写进公网 URL。
+ * - 浏览器:`/api-backend`(同源,端口随页面)
+ * - 服务端 axios:需 `NEXT_PUBLIC_SITE_URL`(如 https://app.example.com)拼成 `https://app.example.com/api-backend`;
+ *   Vercel 可回退 `VERCEL_URL`。
  */
 
 const ORIGIN_PLACEHOLDER = "__origin__";
@@ -31,15 +32,17 @@ function resolvePublicSiteOrigin(): string {
   return "";
 }
 
-/** 将 `__ORIGIN__` 解析为当前站点 origin;非占位符则原样返回(已 trim)。 */
+/** 将 `__ORIGIN__` 解析为「当前站点下的 /api-backend」;非占位符则原样返回(已 trim)。 */
 function resolveApiBaseUrlValue(raw: string | undefined): string {
   const s = raw?.trim() ?? "";
   if (!s) return "";
   if (s.toLowerCase() === ORIGIN_PLACEHOLDER) {
     if (typeof window !== "undefined" && window.location?.origin) {
-      return window.location.origin;
+      return `${window.location.origin}/api-backend`;
     }
-    return resolvePublicSiteOrigin();
+    const origin = resolvePublicSiteOrigin();
+    if (origin) return `${origin}/api-backend`;
+    return "/api-backend";
   }
   return s;
 }
@@ -70,15 +73,24 @@ export function getRemittanceApiBaseUrl(): string {
   const explicit = process.env.NEXT_PUBLIC_REMITTANCE_API_BASE_URL?.trim();
   if (explicit) return explicit.replace(/\/$/, "");
 
-  const base = getApiBaseUrl();
+  const base = getApiBaseUrl().replace(/\/$/, "");
   if (!base) return "";
 
+  // 与主接口同源反写:/api-backend → /api-backend-remittance(Next 转发到 API_PROXY_TARGET_REMITTANCE,如 :8504)
+  if (base === "/api-backend" || base.endsWith("/api-backend")) {
+    return base.replace(/\/api-backend$/, "/api-backend-remittance");
+  }
+
   try {
     const url = new URL(base);
-    url.port = "8504";
-    return url.toString().replace(/\/$/, "");
+    // 历史:直连主后端且带非默认端口(如 :8005),用换端口表示汇款服务(不推荐与 __ORIGIN__ 同源反写混用)
+    if (url.port && url.port !== "443" && url.port !== "80") {
+      url.port = "8504";
+      return url.toString().replace(/\/$/, "");
+    }
+    return `${url.origin}/api-backend-remittance`.replace(/\/$/, "");
   } catch {
-    return base.replace(/\/$/, "");
+    return "/api-backend-remittance";
   }
 }