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 reportUrl = data.reportFiles?.map(f => f.url).filter(Boolean) || []
if (!certificateUrl.length) {
$message.warning('请上传证书')
return
}
if (!reportUrl.length) {
$message.warning('请上传报告')
return
}
const payload = {
...props.detailData,

View File

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