fix(file): 修复文件上传功能中的租户ID和错误处理问题
- 添加从本地存储获取租户ID的逻辑,并设置默认值 - 将请求头重构为独立对象以提高可读性 - 添加对HTTP状态码的检查,确保只接受200响应 - 改进响应数据解析逻辑,支持字符串和对象格式 - 增强错误消息解码处理,避免乱码问题 - 完善错误处理流程,提供更准确的错误信息
This commit is contained in:
@@ -63,22 +63,37 @@ export async function uploadFileByPath(filePath: string) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const tenantId = Taro.getStorageSync('TenantId') || TenantId
|
||||||
|
|
||||||
|
const header: Record<string, string> = {
|
||||||
|
'content-type': 'application/json',
|
||||||
|
TenantId: String(tenantId)
|
||||||
|
}
|
||||||
|
|
||||||
// 统一走同一个上传接口:既支持图片,也支持文档等文件(由后端决定白名单/大小限制)
|
// 统一走同一个上传接口:既支持图片,也支持文档等文件(由后端决定白名单/大小限制)
|
||||||
Taro.uploadFile({
|
Taro.uploadFile({
|
||||||
url: 'https://server.websoft.top/api/oss/upload',
|
url: 'https://server.websoft.top/api/oss/upload',
|
||||||
filePath,
|
filePath,
|
||||||
name: 'file',
|
name: 'file',
|
||||||
header: {
|
header,
|
||||||
'content-type': 'application/json',
|
|
||||||
TenantId
|
|
||||||
},
|
|
||||||
success: (res) => {
|
success: (res) => {
|
||||||
try {
|
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) {
|
if (data.code === 0) {
|
||||||
resolve(data.data)
|
resolve(data.data)
|
||||||
} else {
|
} 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) {
|
} catch (_error) {
|
||||||
reject(new Error('解析响应数据失败'))
|
reject(new Error('解析响应数据失败'))
|
||||||
|
|||||||
@@ -108,7 +108,7 @@ export default function CreditOrderAddPage() {
|
|||||||
Taro.showToast({ title: '上传成功', icon: 'success' })
|
Taro.showToast({ title: '上传成功', icon: 'success' })
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error('上传图片失败:', e)
|
console.error('上传图片失败:', e)
|
||||||
Taro.showToast({ title: '上传失败,请重试', icon: 'none' })
|
Taro.showToast({ title: String((e as any)?.message || '上传失败,请重试'), icon: 'none' })
|
||||||
} finally {
|
} finally {
|
||||||
setUploading(false)
|
setUploading(false)
|
||||||
}
|
}
|
||||||
@@ -151,7 +151,7 @@ export default function CreditOrderAddPage() {
|
|||||||
Taro.showToast({ title: '上传成功', icon: 'success' })
|
Taro.showToast({ title: '上传成功', icon: 'success' })
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error('上传文件失败:', e)
|
console.error('上传文件失败:', e)
|
||||||
Taro.showToast({ title: '上传失败,请重试', icon: 'none' })
|
Taro.showToast({ title: String((e as any)?.message || '上传失败,请重试'), icon: 'none' })
|
||||||
} finally {
|
} finally {
|
||||||
setUploading(false)
|
setUploading(false)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user