|
@@ -73,6 +73,14 @@ const refreshToken = async (): Promise<string | null> => {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+// 辅助函数:获取 header 值(大小写不敏感)
|
|
|
|
|
+const getHeaderValue = (headers: Record<string, string>, key: string): string | undefined => {
|
|
|
|
|
+ const lowerKey = key.toLowerCase()
|
|
|
|
|
+ return Object.keys(headers).find(k => k.toLowerCase() === lowerKey)?.[0] !== undefined
|
|
|
|
|
+ ? headers[Object.keys(headers).find(k => k.toLowerCase() === lowerKey)!]
|
|
|
|
|
+ : undefined
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
// API配置
|
|
// API配置
|
|
|
const API_BASE_URL = process.env.TARO_APP_API_BASE_URL || 'http://localhost:3000'
|
|
const API_BASE_URL = process.env.TARO_APP_API_BASE_URL || 'http://localhost:3000'
|
|
|
|
|
|
|
@@ -123,13 +131,15 @@ const taroFetch: any = async (input, init) => {
|
|
|
try {
|
|
try {
|
|
|
console.log('API请求:', options.url)
|
|
console.log('API请求:', options.url)
|
|
|
const response = await Taro.request(options)
|
|
const response = await Taro.request(options)
|
|
|
|
|
+ console.log('API响应', response)
|
|
|
|
|
|
|
|
const responseHeaders = response.header;
|
|
const responseHeaders = response.header;
|
|
|
|
|
+ const contentType = getHeaderValue(responseHeaders, 'content-type')
|
|
|
|
|
|
|
|
// 处理204 No Content响应,不设置body
|
|
// 处理204 No Content响应,不设置body
|
|
|
const body = response.statusCode === 204
|
|
const body = response.statusCode === 204
|
|
|
? null
|
|
? null
|
|
|
- : responseHeaders['content-type']!.includes('application/json')
|
|
|
|
|
|
|
+ : contentType?.includes('application/json')
|
|
|
? JSON.stringify(response.data)
|
|
? JSON.stringify(response.data)
|
|
|
: response.data;
|
|
: response.data;
|
|
|
|
|
|