2
This commit is contained in:
@@ -24,8 +24,17 @@ function buildAuthHeaders(base = {}) {
|
||||
const claims = parseJwtClaims(token)
|
||||
if (claims && claims.shopId) headers['X-Shop-Id'] = claims.shopId
|
||||
if (claims && claims.userId) headers['X-User-Id'] = claims.userId
|
||||
} else if (ENABLE_DEFAULT_USER && DEFAULT_USER_ID) {
|
||||
if (headers['Authorization']) delete headers['Authorization']
|
||||
headers['X-User-Id'] = headers['X-User-Id'] || DEFAULT_USER_ID
|
||||
if (SHOP_ID) headers['X-Shop-Id'] = headers['X-Shop-Id'] || SHOP_ID
|
||||
}
|
||||
} catch (_) { /* noop: 未登录不注入任何店铺/用户头 */ }
|
||||
} catch (_) {
|
||||
if (ENABLE_DEFAULT_USER && DEFAULT_USER_ID) {
|
||||
headers['X-User-Id'] = headers['X-User-Id'] || DEFAULT_USER_ID
|
||||
if (SHOP_ID) headers['X-Shop-Id'] = headers['X-Shop-Id'] || SHOP_ID
|
||||
}
|
||||
}
|
||||
return headers
|
||||
}
|
||||
|
||||
@@ -67,7 +76,12 @@ export function post(path, body = {}) {
|
||||
const headers = buildAuthHeaders({ 'Content-Type': 'application/json' })
|
||||
const options = { url: buildUrl(path), method: 'POST', data: body, header: headers }
|
||||
const p = String(path || '')
|
||||
if (p.includes('/api/auth/wxmp/login') || p.includes('/api/auth/sms/login') || p.includes('/api/auth/sms/send') || p.includes('/api/auth/password/login') || p.includes('/api/auth/register')) options.__noRetry = true
|
||||
if (
|
||||
p.includes('/api/auth/wxmp/login') ||
|
||||
p.includes('/api/auth/sms/login') || p.includes('/api/auth/sms/send') ||
|
||||
p.includes('/api/auth/email/login') || p.includes('/api/auth/email/send') ||
|
||||
p.includes('/api/auth/password/login') || p.includes('/api/auth/register')
|
||||
) options.__noRetry = true
|
||||
requestWithFallback(options, API_BASE_URL_CANDIDATES, 0, resolve, reject)
|
||||
})
|
||||
}
|
||||
@@ -97,6 +111,7 @@ function uploadWithFallback(options, candidates, idx, resolve, reject) {
|
||||
...uploadOptions,
|
||||
success: (res) => {
|
||||
const statusCode = res.statusCode || 0
|
||||
// 2xx: 正常返回
|
||||
if (statusCode >= 200 && statusCode < 300) {
|
||||
try {
|
||||
const data = typeof res.data === 'string' ? JSON.parse(res.data) : res.data
|
||||
@@ -105,8 +120,23 @@ function uploadWithFallback(options, candidates, idx, resolve, reject) {
|
||||
return resolve(res.data)
|
||||
}
|
||||
}
|
||||
// 4xx: 不重试,透传后端 JSON(如 400 未识别、413 文件过大),给调用方展示 message
|
||||
if (statusCode >= 400 && statusCode < 500) {
|
||||
try {
|
||||
const data = typeof res.data === 'string' ? JSON.parse(res.data) : res.data
|
||||
return resolve(data)
|
||||
} catch (e) {
|
||||
return resolve({ success: false, message: 'HTTP ' + statusCode })
|
||||
}
|
||||
}
|
||||
// 5xx:优先尝试下一个候选地址;到达最后一个候选时,尽力解析 JSON 供前端展示
|
||||
if (idx + 1 < candidates.length) return uploadWithFallback(options, candidates, idx + 1, resolve, reject)
|
||||
reject(new Error('HTTP ' + statusCode))
|
||||
try {
|
||||
const data = typeof res.data === 'string' ? JSON.parse(res.data) : res.data
|
||||
return resolve(data)
|
||||
} catch (e) {
|
||||
return resolve({ success: false, message: 'HTTP ' + statusCode })
|
||||
}
|
||||
},
|
||||
fail: (err) => {
|
||||
if (idx + 1 < candidates.length) return uploadWithFallback(options, candidates, idx + 1, resolve, reject)
|
||||
|
||||
Reference in New Issue
Block a user