Browse Source

fix(http): 移除默认的Content-Type请求头
refactor(hooks): 改进上传钩子的响应解析逻辑

移除http拦截器中硬编码的Content-Type头,让请求更灵活
修改useUpload钩子以适配不同平台的返回格式,增强容错性

feige996 7 tháng trước cách đây
mục cha
commit
6b899c712e
2 tập tin đã thay đổi với 14 bổ sung4 xóa
  1. 14 3
      src/hooks/useUpload.ts
  2. 0 1
      src/http/interceptor.ts

+ 14 - 3
src/hooks/useUpload.ts

@@ -55,10 +55,21 @@ export default function useUpload<T extends TfileType>(options: TOptions<T> = {}
       tempFilePath,
       formData,
       onSuccess: (res) => {
-        const { data: _data } = JSON.parse(res)
-        data.value = _data
+        // 修改这里的解析逻辑,适应不同平台的返回格式
+        let parsedData = res
+        try {
+          // 尝试解析为JSON
+          const jsonData = JSON.parse(res)
+          // 检查是否包含data字段
+          parsedData = jsonData.data || jsonData
+        }
+        catch (e) {
+          // 如果解析失败,使用原始数据
+          console.log('Response is not JSON, using raw data:', res)
+        }
+        data.value = parsedData
         // console.log('上传成功', res)
-        success?.(_data)
+        success?.(parsedData)
       },
       onError: (err) => {
         error.value = err

+ 0 - 1
src/http/interceptor.ts

@@ -47,7 +47,6 @@ const httpInterceptor = {
     // 2. (可选)添加小程序端请求头标识
     options.header = {
       ...options.header,
-      'Content-Type': 'application/json; charset=utf-8',
     }
     // 3. 添加 token 请求头标识
     const tokenStore = useTokenStore()