fix:修复投诉与建议图片多文件上传bug

This commit is contained in:
2025-12-01 16:45:06 +08:00
parent 65a168daed
commit 8dd95a325b
7 changed files with 221 additions and 142 deletions
+63 -71
View File
@@ -71,15 +71,11 @@
onLoad
} from "@dcloudio/uni-app"
import {
URL,
appid
} from '../../config/url'
addUserFeedback
} from '../../config/api/feedback'
import {
uploadOssResource
} from '../../config/api/user'
import {
addUserFeedback
} from '../../config/api/feedback'
import {
useI18n
} from '@/utils/i18n.js'
@@ -113,7 +109,6 @@
const description = ref('')
const images = ref([])
const contact = ref('')
const apiUrl = URL
// 方法
const selectType = (index) => {
@@ -124,29 +119,10 @@
const chooseImage = () => {
uni.chooseImage({
count: 3 - images.value.length,
success: async (res) => {
console.log(res);
success: (res) => {
// 直接保存本地路径,不上传
const toUpload = res.tempFilePaths || []
for (const localPath of toUpload) {
// 先追加本地预览,再上传并替换为远程URL
images.value.push(localPath)
try {
const remoteUrl = await uploadOssResource(localPath)
const idx = images.value.indexOf(localPath)
if (idx !== -1) {
images.value.splice(idx, 1, remoteUrl)
}
} catch (e) {
const idx = images.value.indexOf(localPath)
if (idx !== -1) {
images.value.splice(idx, 1)
}
uni.showToast({
title: '图片上传失败',
icon: 'none'
})
}
}
images.value.push(...toUpload)
}
})
}
@@ -187,52 +163,68 @@
paramsType.value = 'suggestion'
}
// 构建反馈数据
const feedbackData = {
type: paramsType.value,
content: description.value,
phone: contact.value,
picturePath: images.value[0]
}
try {
// 显示上传进度
uni.showLoading({
title: $t('feedback.uploading') || '上传中...',
mask: true
})
uni.request({
url: `${apiUrl}/app/feedback/add`,
method: 'POST',
data: feedbackData,
header: {
'Content-Type': 'application/json',
'appid': appid,
'Authorization': 'Bearer ' + uni.getStorageSync('token'),
'Clientid': uni.getStorageSync('client_id')
},
dataType: 'json',
success: (res) => {
// 兼容后端返回 { code: 200 } 或 HTTP 200 情况
if ((res.statusCode === 200) && ((res.data && res.data.code === 200) || res.data ===
true || res.data?.success === true)) {
uni.showToast({
title: $t('feedback.submitSuccess'),
icon: 'success'
})
setTimeout(() => {
uni.navigateBack();
}, 1500);
return
// 先逐步上传所有文件到OSS
const files = []
if (images.value.length > 0) {
for (let i = 0; i < images.value.length; i++) {
const filePath = images.value[i]
try {
const remoteUrl = await uploadOssResource(filePath)
files.push(remoteUrl)
} catch (err) {
console.error(`文件 ${i + 1} 上传失败:`, err)
uni.hideLoading()
uni.showToast({
title: $t('feedback.imageUploadFailed'),
icon: 'none'
})
return
}
}
}
// 构建反馈数据
const feedbackData = {
type: paramsType.value,
content: description.value,
phone: contact.value,
files: files
}
// 调用API提交反馈(使用 hideLoading 避免重复显示loading
const res = await addUserFeedback(feedbackData)
uni.hideLoading()
// 处理响应
if (res && (res.code === 200 || res === true || res?.success === true)) {
uni.showToast({
title: (res.data && (res.data.msg || res.data.message)) || $t(
'feedback.submitFailed'),
icon: 'none'
title: $t('feedback.submitSuccess'),
icon: 'success'
})
},
fail: (err) => {
console.error('feedback request failed:', err)
setTimeout(() => {
uni.navigateBack();
}, 1500);
} else {
uni.showToast({
title: $t('error.networkError'),
title: (res && (res.msg || res.message)) || $t('feedback.submitFailed'),
icon: 'none'
})
}
})
} catch (err) {
console.error('feedback submit failed:', err)
uni.hideLoading()
uni.showToast({
title: $t('error.networkError') || '网络错误,请重试',
icon: 'none'
})
}
}
</script>
@@ -365,8 +357,8 @@
flex-wrap: wrap;
.upload-item {
width: 200rpx;
height: 200rpx;
width: 180rpx;
height: 180rpx;
margin-right: 20rpx;
margin-bottom: 20rpx;
position: relative;
@@ -394,8 +386,8 @@
}
.upload-btn {
width: 200rpx;
height: 200rpx;
width: 180rpx;
height: 180rpx;
background: #f5f5f5;
border-radius: 10rpx;
display: flex;