账单
This commit is contained in:
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user