fix: 修复报告页
This commit is contained in:
parent
ce523e5a66
commit
c22250ba53
@ -289,7 +289,11 @@ const buildPendingItem = (localPath, mediaType) => ({
|
||||
})
|
||||
|
||||
const setUploadSuccess = (item, uploadedUrl) => {
|
||||
try {
|
||||
item.url = imgUrl(uploadedUrl)
|
||||
} catch (_) {
|
||||
item.url = uploadedUrl || ''
|
||||
}
|
||||
item.uploading = false
|
||||
item.failed = false
|
||||
item.error = ''
|
||||
@ -301,6 +305,40 @@ const setUploadFail = (item, reason) => {
|
||||
item.error = reason || '上传失败'
|
||||
}
|
||||
|
||||
/** 微信端 uploadFile:statusCode 可能为字符串或缺失;须按 2xx 判断 */
|
||||
function uploadHttpOk(sc) {
|
||||
if (sc == null || sc === '') return true
|
||||
const n = Number(sc)
|
||||
if (!Number.isNaN(n)) return n >= 200 && n < 300
|
||||
return String(sc) === '200'
|
||||
}
|
||||
|
||||
function parseUploadJson(raw) {
|
||||
if (raw == null || raw === '') return null
|
||||
if (typeof raw === 'object' && !Array.isArray(raw)) return raw
|
||||
const s = String(raw).replace(/^\uFEFF/, '').trim()
|
||||
if (!s) return null
|
||||
try {
|
||||
return JSON.parse(s)
|
||||
} catch {
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
function extractUploadedUrl(data) {
|
||||
if (!data || typeof data !== 'object') return ''
|
||||
const inner = data.data
|
||||
if (inner && typeof inner === 'object' && inner.url) return String(inner.url)
|
||||
if (typeof inner === 'string' && inner.startsWith('/')) return inner
|
||||
if (data.url) return String(data.url)
|
||||
return ''
|
||||
}
|
||||
|
||||
function isBizSuccess(data) {
|
||||
const c = data?.code
|
||||
return c === 200 || c === '200'
|
||||
}
|
||||
|
||||
const uploadSingleFile = (filePath) =>
|
||||
new Promise((resolve) => {
|
||||
let settled = false
|
||||
@ -319,25 +357,25 @@ const uploadSingleFile = (filePath) =>
|
||||
name: 'file',
|
||||
timeout: UPLOAD_TIMEOUT_MS,
|
||||
success: (uploadRes) => {
|
||||
try {
|
||||
clearTimeout(timer)
|
||||
const sc = uploadRes.statusCode
|
||||
if (sc !== 200) {
|
||||
done({ ok: false, message: friendlyUploadError(`HTTP ${sc}`) })
|
||||
if (!uploadHttpOk(uploadRes.statusCode)) {
|
||||
done({ ok: false, message: friendlyUploadError(`HTTP ${uploadRes.statusCode}`) })
|
||||
return
|
||||
}
|
||||
const raw = uploadRes.data || ''
|
||||
try {
|
||||
const data = typeof raw === 'string' ? JSON.parse(raw || '{}') : raw
|
||||
if (data.code === 200 && data.data?.url) {
|
||||
done({ ok: true, url: data.data.url })
|
||||
} else {
|
||||
done({ ok: false, message: friendlyUploadError(data.message) })
|
||||
const data = parseUploadJson(uploadRes.data)
|
||||
const serverUrl = extractUploadedUrl(data)
|
||||
if (isBizSuccess(data) && serverUrl) {
|
||||
done({ ok: true, url: serverUrl })
|
||||
return
|
||||
}
|
||||
} catch (_) {
|
||||
const msg = data?.message || data?.msg || ''
|
||||
done({
|
||||
ok: false,
|
||||
message: raw && String(raw).length < 80 ? friendlyUploadError(raw) : '服务返回异常,请重试'
|
||||
message: msg ? friendlyUploadError(msg) : '上传返回异常,请重试'
|
||||
})
|
||||
} catch (e) {
|
||||
done({ ok: false, message: '处理上传结果失败,请重试' })
|
||||
}
|
||||
},
|
||||
fail: (err) => {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user