fix(http): 网关分服务前缀——登录接口走 /api/user,业务接口走 /api/biz

This commit is contained in:
ozh 2026-09-17 10:51:55 +08:00
parent 6889e9b4fa
commit 5582fbb863
2 changed files with 14 additions and 8 deletions

View File

@ -5,10 +5,12 @@ import { ROUTES } from '@/constants/routes'
import { reLaunch } from '@/utils/navigation' import { reLaunch } from '@/utils/navigation'
import { ApiError, BusinessError, AuthExpiredError } from './errors' import { ApiError, BusinessError, AuthExpiredError } from './errors'
// 测试环境(内网,开发者工具需勾选「不校验合法域名」);正式域名为占位符,上线前必须替换(spec D2) // 网关分服务前缀:登录走 user 服务,其余业务接口走 biz 服务(后端约定 2026-09-17)
const BASE_URL = process.env.NODE_ENV === 'development' const GATEWAY = process.env.NODE_ENV === 'development'
? 'http://192.168.10.11:39101/api/user' ? 'http://192.168.10.11:39101'
: 'https://PROD_PLACEHOLDER/api/user' : 'https://PROD_PLACEHOLDER' // 正式域名占位符,上线前必须替换(spec D2)
const USER_BASE = `${GATEWAY}/api/user`
const BIZ_BASE = `${GATEWAY}/api/biz`
// 后端约定的未登录业务码(占位 '401',联调确认后更新——spec D5/Open Questions 1) // 后端约定的未登录业务码(占位 '401',联调确认后更新——spec D5/Open Questions 1)
const UNAUTHORIZED_CODES = new Set(['401']) const UNAUTHORIZED_CODES = new Set(['401'])
@ -70,10 +72,13 @@ export async function request<T>(
if (tokenAtStart) headers.token = tokenAtStart if (tokenAtStart) headers.token = tokenAtStart
} }
// 登录接口走 user 服务前缀,其余业务接口走 biz 前缀
const base = url.startsWith('/thirdPlatform/') ? USER_BASE : BIZ_BASE
let res: Taro.request.SuccessCallbackResult<ApiEnvelope<T>> let res: Taro.request.SuccessCallbackResult<ApiEnvelope<T>>
try { try {
res = await Taro.request({ res = await Taro.request({
url: BASE_URL + url, url: base + url,
method, method,
// Taro.request 的 GET 参数同样走 data,运行时自动序列化为 query string // Taro.request 的 GET 参数同样走 data,运行时自动序列化为 query string
data: params, data: params,

View File

@ -115,11 +115,12 @@ describe('client envelope 解析', () => {
expect(mockRequest.mock.calls[0][0].header.token).toBeUndefined() 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)) mockRequest.mockResolvedValue(okBody(null))
await request.get('/thirdPlatform/wxDevLogin/miniapp/login') await request.post('/thirdPlatform/wxDevLogin/miniapp/login', {})
// BASE_URL 前缀由后端网关决定(当前 /api/user),此处只锁定 path 拼接
expect(mockRequest.mock.calls[0][0].url).toMatch(/\/api\/user\/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$/)
}) })
}) })