fix: 文件上传压缩功能修复。

This commit is contained in:
tianyongbao
2026-07-08 10:10:38 +08:00
parent 6aaf564800
commit aeb1e39920
5 changed files with 445 additions and 261 deletions

View File

@@ -114,6 +114,7 @@
<script setup>
import { getDoctorRecord, addDoctorRecord, updateDoctorRecord, uploadFile } from '@/api/health/doctorRecord'
import config from '@/config'
import { compressImage } from '@/utils/imageCompress'
import { listPerson } from '@/api/health/person'
import { listHealthRecord } from '@/api/health/healthRecord'
const { proxy } = getCurrentInstance()
@@ -457,30 +458,32 @@ function chooseImage() {
}
// 递归上传图片
function uploadImages(paths, index) {
async function uploadImages(paths, index) {
if (index >= paths.length) {
uploading.value = false
return
}
uploading.value = true
// H5 端 mask: true 会让页面无法点击,一旦上传出问题会导致用户被“锁住”
// 这里改为 mask: false允许用户取消会提示正在上传
uni.showLoading({ title: `上传中 ${index + 1}/${paths.length}`, mask: false })
uploadFile({
filePath: paths[index],
name: 'file'
}).then(res => {
uni.showLoading({ title: `处理中 ${index + 1}/${paths.length}`, mask: false })
try {
// 上传前先压缩(超过 500KB 才压,避免小图多走一道)
const compressedPath = await compressImage(paths[index])
uni.showLoading({ title: `上传中 ${index + 1}/${paths.length}`, mask: false })
const res = await uploadFile({
filePath: compressedPath,
name: 'file'
})
const url = (res.data && res.data.url) || res.fileName || res.url
if (url) {
attachmentList.value.push(normalizeAttachmentUrl(url))
}
uni.hideLoading()
uploadImages(paths, index + 1)
}).catch(() => {
} catch (e) {
uni.hideLoading()
uploading.value = false
proxy.$refs['uToast'].show({ message: `${index + 1} 张上传失败`, type: 'error' })
})
}
}
// 删除图片