| | |
| | | <template> |
| | | <a-modal :title="title" :visible="visible" @cancel="handleModalClose" :maskClosable="false"> |
| | | <a-upload :multiple="true" :file-list="fileList" :remove="handleRemove" :before-upload="beforeUpload" > |
| | | <!-- 移除multiple属性,设置fileList为单个文件 --> |
| | | <a-upload :file-list="fileList" :remove="handleRemove" :before-upload="beforeUpload" > |
| | | <a-button type="primary" :disabled="uploading"> |
| | | <a-icon type="import"/> |
| | | 选取文件 |
| | |
| | | 确认 |
| | | </a-button> |
| | | </template> |
| | | |
| | | </a-modal> |
| | | </template> |
| | | |
| | | <script> |
| | | import JUpload from '@/components/jeecg/JUpload' |
| | | import { getAction, postAction, uploadAction } from '@/api/manage' |
| | | import { uploadAction } from '@/api/manage' |
| | | |
| | | export default { |
| | | name: 'MaintenanceStandardImportModule', |
| | | components: { |
| | | JUpload, |
| | | }, |
| | | data() { |
| | | return { |
| | | visible: false, |
| | | title: '', |
| | | id:'', |
| | | fileList: [], |
| | | uploadParams: {}, |
| | | id: '', |
| | | fileList: [], // 初始空文件列表 |
| | | uploading: false, |
| | | isUploadMultiple: true, |
| | | currentDeviceDocClassCode: 'SEND', |
| | | currentTitleAfterClass: '', |
| | | |
| | | url: { |
| | | add: "/eam/maintenanceStandard/importUpgrade", |
| | | }, |
| | | } |
| | | }, |
| | | |
| | | created() { |
| | | }, |
| | | methods: { |
| | | |
| | | /** |
| | | * 选择好文件点击确定后 |
| | | * @param file 文件对象 |
| | | */ |
| | | beforeUpload(file) { |
| | | if (this.isUploadMultiple) { |
| | | if (!this.fileList.find(item => item.name === file.name)) this.fileList = [...this.fileList, file] |
| | | } else this.fileList.splice(0, 1, file) |
| | | // 只保留最新选择的文件 |
| | | this.fileList = [file] |
| | | return false |
| | | }, |
| | | |
| | | /** |
| | | * 删除文件列表项时触发 |
| | | * @param file 文件对象 |
| | | */ |
| | | handleRemove(file) { |
| | | const index = this.fileList.indexOf(file) |
| | | const newFileList = this.fileList.slice() |
| | | newFileList.splice(index, 1) |
| | | this.fileList = newFileList |
| | | handleRemove() { |
| | | // 清空文件列表 |
| | | this.fileList = [] |
| | | }, |
| | | |
| | | // 控制文件上传窗口关闭并清空文件列表 |
| | | handleModalClose() { |
| | | this.visible = false |
| | | this.fileList = [] |
| | | }, |
| | | |
| | | upload(id){ |
| | | console.log("id",id) |
| | | upload(id) { |
| | | this.id = id |
| | | this.isUploadMultiple = true |
| | | this.visible = true |
| | | }, |
| | | |
| | | handleUpload() { |
| | | const { fileList, $notification, handleModalClose} = this |
| | | this.uploading = true |
| | | let uploadedFileCount = 0 |
| | | let uploadSuccessFileCount = 0 |
| | | if (this.fileList.length === 0) { |
| | | const { fileList, $notification, handleModalClose } = this |
| | | |
| | | // 检查是否有文件 |
| | | if (fileList.length === 0) { |
| | | this.$message.info("请上传文件") |
| | | this.uploading = false; |
| | | return |
| | | } |
| | | fileList.forEach((file, index) => { |
| | | const formData = new FormData() |
| | | formData.append('file', file) |
| | | formData.append('id',this.id) |
| | | uploadAction(this.url.add, formData).then(res => { |
| | | |
| | | this.uploading = true |
| | | |
| | | const file = fileList[0] // 只取第一个文件 |
| | | const formData = new FormData() |
| | | formData.append('file', file) |
| | | formData.append('id', this.id) |
| | | |
| | | uploadAction(this.url.add, formData) |
| | | .then(res => { |
| | | if (res.success) { |
| | | file.status = 'done' |
| | | uploadSuccessFileCount++ |
| | | $notification.success({ |
| | | message: '消息', |
| | | description: res.message |
| | | }) |
| | | this.$emit('ok') |
| | | handleModalClose(); |
| | | } else { |
| | | file.status = 'error' |
| | | $notification.error({ |
| | | message: '消息', |
| | | description: res.message |
| | | }) |
| | | } |
| | | }) |
| | | .catch(err => { |
| | | file.status = 'error' |
| | | .catch(err => { |
| | | $notification.error({ |
| | | message: '错误', |
| | | description: '文件上传失败' |
| | | }) |
| | | .finally(() => { |
| | | uploadedFileCount++ |
| | | fileList.splice(index, 1, file) |
| | | if (uploadedFileCount === fileList.length) { |
| | | if (uploadSuccessFileCount > 0) { |
| | | this.$emit('ok') |
| | | } |
| | | // 当文件全部上传完成后 |
| | | if (uploadSuccessFileCount === fileList.length) { |
| | | handleModalClose(); |
| | | } |
| | | this.uploading = false |
| | | } |
| | | }) |
| | | }) |
| | | }) |
| | | .finally(() => { |
| | | this.uploading = false |
| | | }) |
| | | } |
| | | } |
| | | } |