fix(file): 修复文件上传功能中的租户ID和错误处理问题
- 添加从本地存储获取租户ID的逻辑,并设置默认值 - 将请求头重构为独立对象以提高可读性 - 添加对HTTP状态码的检查,确保只接受200响应 - 改进响应数据解析逻辑,支持字符串和对象格式 - 增强错误消息解码处理,避免乱码问题 - 完善错误处理流程,提供更准确的错误信息
This commit is contained in:
@@ -63,22 +63,37 @@ export async function uploadFileByPath(filePath: string) {
|
||||
return
|
||||
}
|
||||
|
||||
const tenantId = Taro.getStorageSync('TenantId') || TenantId
|
||||
|
||||
const header: Record<string, string> = {
|
||||
'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('解析响应数据失败'))
|
||||
|
||||
Reference in New Issue
Block a user