From 3e32af118987667ac112fb2bbf9be792aa87a329 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B5=B5=E5=BF=A0=E6=9E=97?= <170083662@qq.com> Date: Mon, 16 Mar 2026 23:28:14 +0800 Subject: [PATCH] =?UTF-8?q?fix(file):=20=E4=BF=AE=E5=A4=8D=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E4=B8=8A=E4=BC=A0=E5=8A=9F=E8=83=BD=E4=B8=AD=E7=9A=84?= =?UTF-8?q?=E7=A7=9F=E6=88=B7ID=E5=92=8C=E9=94=99=E8=AF=AF=E5=A4=84?= =?UTF-8?q?=E7=90=86=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 添加从本地存储获取租户ID的逻辑,并设置默认值 - 将请求头重构为独立对象以提高可读性 - 添加对HTTP状态码的检查,确保只接受200响应 - 改进响应数据解析逻辑,支持字符串和对象格式 - 增强错误消息解码处理,避免乱码问题 - 完善错误处理流程,提供更准确的错误信息 --- src/api/system/file/index.ts | 27 +++++++++++++++++++++------ src/credit/order/add.tsx | 4 ++-- 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/src/api/system/file/index.ts b/src/api/system/file/index.ts index 38bdd42..2411108 100644 --- a/src/api/system/file/index.ts +++ b/src/api/system/file/index.ts @@ -63,22 +63,37 @@ export async function uploadFileByPath(filePath: string) { return } + const tenantId = Taro.getStorageSync('TenantId') || TenantId + + const header: Record = { + 'content-type': 'application/json', + TenantId: String(tenantId) + } + // 统一走同一个上传接口:既支持图片,也支持文档等文件(由后端决定白名单/大小限制) Taro.uploadFile({ url: 'https://server.websoft.top/api/oss/upload', filePath, name: 'file', - header: { - 'content-type': 'application/json', - TenantId - }, + header, success: (res) => { try { - const data = JSON.parse(res.data); + if ((res as any)?.statusCode && (res as any).statusCode !== 200) { + reject(new Error(`上传失败(HTTP ${(res as any).statusCode})`)) + return + } + const raw = (res as any)?.data + const data = typeof raw === 'string' ? JSON.parse(raw) : raw if (data.code === 0) { resolve(data.data) } else { - reject(new Error(data.message || '上传失败')) + let msg = String(data.message || '上传失败') + try { + msg = decodeURIComponent(escape(msg)) + } catch (_e) { + // ignore + } + reject(new Error(msg || '上传失败')) } } catch (_error) { reject(new Error('解析响应数据失败')) diff --git a/src/credit/order/add.tsx b/src/credit/order/add.tsx index 1c28cd8..a55fd2e 100644 --- a/src/credit/order/add.tsx +++ b/src/credit/order/add.tsx @@ -108,7 +108,7 @@ export default function CreditOrderAddPage() { Taro.showToast({ title: '上传成功', icon: 'success' }) } catch (e) { console.error('上传图片失败:', e) - Taro.showToast({ title: '上传失败,请重试', icon: 'none' }) + Taro.showToast({ title: String((e as any)?.message || '上传失败,请重试'), icon: 'none' }) } finally { setUploading(false) } @@ -151,7 +151,7 @@ export default function CreditOrderAddPage() { Taro.showToast({ title: '上传成功', icon: 'success' }) } catch (e) { console.error('上传文件失败:', e) - Taro.showToast({ title: '上传失败,请重试', icon: 'none' }) + Taro.showToast({ title: String((e as any)?.message || '上传失败,请重试'), icon: 'none' }) } finally { setUploading(false) }