This commit is contained in:
liu
2026-08-14 20:21:50 +08:00
parent 4a5b7003ca
commit 03434feb35
21 changed files with 1513 additions and 629 deletions
+20
View File
@@ -90,6 +90,25 @@ function handleBusinessError(data: ApiResponse): void {
}
}
/**
* 清理请求参数
* data 中的 undefined 在 GET 查询串里会被序列化为字符串 'undefined'null 同理),
* 导致后端收到字符串而非缺省;统一在此剔除。GET 额外剔除 null,
* POST/PUT 等请求体保留 null(JSON 序列化语义可能依赖它)
*/
function sanitizeData(data: any, method?: RequestConfig['method']): any {
if (!data || typeof data !== 'object' || Array.isArray(data)) return data
const isGet = !method || method === 'GET'
const result: Record<string, any> = {}
Object.keys(data).forEach(key => {
const value = data[key]
if (value === undefined) return
if (isGet && value === null) return
result[key] = value
})
return result
}
/**
* 发起网络请求
*
@@ -126,6 +145,7 @@ export function request<T = any>(config: RequestConfig): Promise<ApiResponse<T>>
Taro.request({
...restConfig,
url: BASE_URL + restConfig.url,
data: sanitizeData(restConfig.data, restConfig.method),
header,
timeout: restConfig.timeout || DEFAULT_TIMEOUT,
success(res) {