This commit is contained in:
2025-09-27 22:57:59 +08:00
parent 8a458ff0a4
commit ed26244cdb
12585 changed files with 1914308 additions and 3474 deletions

View File

@@ -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)