From 5582fbb8638cc13f3005496310018d0ce6b1c408 Mon Sep 17 00:00:00 2001 From: ozh Date: Thu, 17 Sep 2026 10:51:55 +0800 Subject: [PATCH] =?UTF-8?q?fix(http):=20=E7=BD=91=E5=85=B3=E5=88=86?= =?UTF-8?q?=E6=9C=8D=E5=8A=A1=E5=89=8D=E7=BC=80=E2=80=94=E2=80=94=E7=99=BB?= =?UTF-8?q?=E5=BD=95=E6=8E=A5=E5=8F=A3=E8=B5=B0=20/api/user=EF=BC=8C?= =?UTF-8?q?=E4=B8=9A=E5=8A=A1=E6=8E=A5=E5=8F=A3=E8=B5=B0=20/api/biz?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/http/client.ts | 15 ++++++++++----- tests/unit/http/client.spec.ts | 7 ++++--- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/src/http/client.ts b/src/http/client.ts index 514456c..159e00e 100644 --- a/src/http/client.ts +++ b/src/http/client.ts @@ -5,10 +5,12 @@ import { ROUTES } from '@/constants/routes' import { reLaunch } from '@/utils/navigation' import { ApiError, BusinessError, AuthExpiredError } from './errors' -// 测试环境(内网,开发者工具需勾选「不校验合法域名」);正式域名为占位符,上线前必须替换(spec D2) -const BASE_URL = process.env.NODE_ENV === 'development' - ? 'http://192.168.10.11:39101/api/user' - : 'https://PROD_PLACEHOLDER/api/user' +// 网关分服务前缀:登录走 user 服务,其余业务接口走 biz 服务(后端约定 2026-09-17) +const GATEWAY = process.env.NODE_ENV === 'development' + ? 'http://192.168.10.11:39101' + : 'https://PROD_PLACEHOLDER' // 正式域名占位符,上线前必须替换(spec D2) +const USER_BASE = `${GATEWAY}/api/user` +const BIZ_BASE = `${GATEWAY}/api/biz` // 后端约定的未登录业务码(占位 '401',联调确认后更新——spec D5/Open Questions 1) const UNAUTHORIZED_CODES = new Set(['401']) @@ -70,10 +72,13 @@ export async function request( if (tokenAtStart) headers.token = tokenAtStart } + // 登录接口走 user 服务前缀,其余业务接口走 biz 前缀 + const base = url.startsWith('/thirdPlatform/') ? USER_BASE : BIZ_BASE + let res: Taro.request.SuccessCallbackResult> try { res = await Taro.request({ - url: BASE_URL + url, + url: base + url, method, // Taro.request 的 GET 参数同样走 data,运行时自动序列化为 query string data: params, diff --git a/tests/unit/http/client.spec.ts b/tests/unit/http/client.spec.ts index 2267928..ace49e7 100644 --- a/tests/unit/http/client.spec.ts +++ b/tests/unit/http/client.spec.ts @@ -115,11 +115,12 @@ describe('client envelope 解析', () => { expect(mockRequest.mock.calls[0][0].header.token).toBeUndefined() }) - it('BASE_URL 拼接:请求 url = BASE_URL + path', async () => { + it('BASE_URL 拼接:登录接口走 /api/user,业务接口走 /api/biz', async () => { mockRequest.mockResolvedValue(okBody(null)) - await request.get('/thirdPlatform/wxDevLogin/miniapp/login') - // BASE_URL 前缀由后端网关决定(当前 /api/user),此处只锁定 path 拼接 + await request.post('/thirdPlatform/wxDevLogin/miniapp/login', {}) expect(mockRequest.mock.calls[0][0].url).toMatch(/\/api\/user\/thirdPlatform\/wxDevLogin\/miniapp\/login$/) + await request.post('/yanglao/app/ylHome/getList') + expect(mockRequest.mock.calls[1][0].url).toMatch(/\/api\/biz\/yanglao\/app\/ylHome\/getList$/) }) })