refactor: 优化证书上传模态框文件处理逻辑并移除强制上传校验

This commit is contained in:
Wei_佳 2025-11-25 10:26:28 +08:00
parent db31e25c61
commit 695769076a
2 changed files with 26 additions and 65 deletions

View File

@ -323,15 +323,6 @@ const handleCertificateConfirm = async (data) => {
const certificateUrl = data.certificateFiles?.map(f => f.url).filter(Boolean) || [] const certificateUrl = data.certificateFiles?.map(f => f.url).filter(Boolean) || []
const reportUrl = data.reportFiles?.map(f => f.url).filter(Boolean) || [] const reportUrl = data.reportFiles?.map(f => f.url).filter(Boolean) || []
if (!certificateUrl.length) {
$message.warning('请上传证书')
return
}
if (!reportUrl.length) {
$message.warning('请上传报告')
return
}
const payload = { const payload = {
...props.detailData, ...props.detailData,

View File

@ -33,11 +33,6 @@ const props = defineProps({
const emit = defineEmits(['update:visible', 'confirm']) const emit = defineEmits(['update:visible', 'confirm'])
const message = useMessage() const message = useMessage()
const formData = ref({
reportFiles: [],
certificateFiles: [],
})
const reportFileList = ref([]) const reportFileList = ref([])
const certificateFileList = ref([]) const certificateFileList = ref([])
@ -46,6 +41,7 @@ const uploadHeaders = computed(() => ({
Authorization: `Bearer ${getToken()}`, Authorization: `Bearer ${getToken()}`,
})) }))
//
// //
watch( watch(
() => props.visible, () => props.visible,
@ -53,14 +49,10 @@ watch(
if (val) { if (val) {
if (props.mode === 'view') { if (props.mode === 'view') {
// //
reportFileList.value = props.certificateData?.reportFiles || [] reportFileList.value = (props.certificateData?.reportFiles || []).map(f => ({ ...f, status: 'finished' }))
certificateFileList.value = props.certificateData?.certificateFiles || [] certificateFileList.value = (props.certificateData?.certificateFiles || []).map(f => ({ ...f, status: 'finished' }))
} else { } else {
// //
formData.value = {
reportFiles: [],
certificateFiles: [],
}
reportFileList.value = [] reportFileList.value = []
certificateFileList.value = [] certificateFileList.value = []
} }
@ -73,11 +65,19 @@ const handleClose = () => {
emit('update:visible', false) emit('update:visible', false)
} }
//
// //
const handleConfirm = () => { const handleConfirm = () => {
const getFiles = (list) => list.map(f => ({
id: f.id,
name: f.name,
url: f.url,
type: f.type
})).filter(f => f.url)
emit('confirm', { emit('confirm', {
reportFiles: formData.value.reportFiles, reportFiles: getFiles(reportFileList.value),
certificateFiles: formData.value.certificateFiles, certificateFiles: getFiles(certificateFileList.value)
}) })
handleClose() handleClose()
} }
@ -104,23 +104,17 @@ const beforeUpload = (data) => {
return true return true
} }
//
// //
const handleReportUploadFinish = ({ file, event }) => { const handleReportUploadFinish = ({ file, event }) => {
try { try {
const res = JSON.parse(event.target.response) const res = JSON.parse(event.target.response)
if (res.code === 200 && res.data?.url) { if (res.code === 200 && res.data?.url) {
const targetFile = reportFileList.value.find(f => f.id === file.id) || file file.url = res.data.url
targetFile.url = res.data.url file.name = res.data.filename || file.name
targetFile.name = res.data.filename || targetFile.name file.status = 'finished'
targetFile.status = 'finished'
formData.value.reportFiles = reportFileList.value.map(f => ({
id: f.id,
name: f.name,
url: f.url,
type: f.type
}))
message.success('报告上传成功') message.success('报告上传成功')
return file
} else { } else {
message.error(res.message || '上传失败') message.error(res.message || '上传失败')
const index = reportFileList.value.findIndex((item) => item.id === file.id) const index = reportFileList.value.findIndex((item) => item.id === file.id)
@ -132,26 +126,19 @@ const handleReportUploadFinish = ({ file, event }) => {
const index = reportFileList.value.findIndex((item) => item.id === file.id) const index = reportFileList.value.findIndex((item) => item.id === file.id)
if (index > -1) reportFileList.value.splice(index, 1) if (index > -1) reportFileList.value.splice(index, 1)
} }
return file
} }
//
// //
const handleCertificateUploadFinish = ({ file, event }) => { const handleCertificateUploadFinish = ({ file, event }) => {
try { try {
const res = JSON.parse(event.target.response) const res = JSON.parse(event.target.response)
if (res.code === 200 && res.data?.url) { if (res.code === 200 && res.data?.url) {
const targetFile = certificateFileList.value.find(f => f.id === file.id) || file file.url = res.data.url
targetFile.url = res.data.url file.name = res.data.filename || file.name
targetFile.name = res.data.filename || targetFile.name file.status = 'finished'
targetFile.status = 'finished'
formData.value.certificateFiles = certificateFileList.value.map(f => ({
id: f.id,
name: f.name,
url: f.url,
type: f.type
}))
message.success('证书上传成功') message.success('证书上传成功')
return file
} else { } else {
message.error(res.message || '上传失败') message.error(res.message || '上传失败')
const index = certificateFileList.value.findIndex((item) => item.id === file.id) const index = certificateFileList.value.findIndex((item) => item.id === file.id)
@ -163,26 +150,9 @@ const handleCertificateUploadFinish = ({ file, event }) => {
const index = certificateFileList.value.findIndex((item) => item.id === file.id) const index = certificateFileList.value.findIndex((item) => item.id === file.id)
if (index > -1) certificateFileList.value.splice(index, 1) if (index > -1) certificateFileList.value.splice(index, 1)
} }
return file
} }
//
const handleRemove = () => {
// formData
formData.value.reportFiles = reportFileList.value.map(f => ({
id: f.id,
name: f.name,
url: f.url,
type: f.type
}))
formData.value.certificateFiles = certificateFileList.value.map(f => ({
id: f.id,
name: f.name,
url: f.url,
type: f.type
}))
return true
}
const modalTitle = computed(() => { const modalTitle = computed(() => {
return props.mode === 'upload' ? '上传' : '查看' return props.mode === 'upload' ? '上传' : '查看'
@ -239,7 +209,7 @@ const handlePreview = (file) => {
:headers="uploadHeaders" :headers="uploadHeaders"
:before-upload="beforeUpload" :before-upload="beforeUpload"
@finish="handleReportUploadFinish" @finish="handleReportUploadFinish"
@remove="handleRemove"
:disabled="!isUploadMode" :disabled="!isUploadMode"
show-preview-button show-preview-button
show-download-button show-download-button
@ -261,7 +231,7 @@ const handlePreview = (file) => {
:headers="uploadHeaders" :headers="uploadHeaders"
:before-upload="beforeUpload" :before-upload="beforeUpload"
@finish="handleCertificateUploadFinish" @finish="handleCertificateUploadFinish"
@remove="handleRemove"
:disabled="!isUploadMode" :disabled="!isUploadMode"
show-preview-button show-preview-button
show-download-button show-download-button