perf: 优化组件

This commit is contained in:
zyn
2024-01-04 10:56:10 +08:00
parent cdf80001bc
commit 01700b8030
3 changed files with 105 additions and 3 deletions
+96 -3
View File
@@ -19,10 +19,16 @@
:action="action" :action="action"
:headers="headers" :headers="headers"
name="file" name="file"
:file-list="fileList"
:accept="accept" :accept="accept"
:auto-upload="autoUpload" :auto-upload="autoUpload"
:limit="limit"
:before-upload="beforeUpload" :before-upload="beforeUpload"
:on-success="onSuccess" :on-success="onSuccess"
:on-remove="onRemove"
:on-preview="onPreview"
:on-exceed="onExceed"
:on-error="onError"
:show-file-list="true" :show-file-list="true"
> >
<i class="el-icon-upload" style="color: #3399ff" /> <i class="el-icon-upload" style="color: #3399ff" />
@@ -93,7 +99,21 @@ export default {
manualUpload: { manualUpload: {
type: Boolean, type: Boolean,
default: false default: false
} },
limit: {
type: Number,
default: 8
},
ids: {
required: false,
type: String,
default: ''
},
names: {
required: false,
type: String,
default: ''
},
}, },
data () { data () {
return { return {
@@ -101,10 +121,28 @@ export default {
token: JSON.parse(localStorage.getItem('userInfo')).token token: JSON.parse(localStorage.getItem('userInfo')).token
}, },
importFailed: false, importFailed: false,
importForm: {} importForm: {},
fileList: [],
} }
}, },
created () {
this.initFileList()
},
methods: { methods: {
initFileList () {
if (this.ids && this.ids !== '' && this.names && this.names !== '') {
const fileList = []
const idArr = this.ids.split(',')
const nameArr = this.names.split(',')
for (let i = 0; i < idArr.length; i++) {
fileList.push({
id: idArr[i],
name: nameArr[i]
})
}
this.fileList = fileList
}
},
onClose () { onClose () {
this.$emit('update:visible', false) this.$emit('update:visible', false)
}, },
@@ -131,8 +169,25 @@ export default {
return true return true
}, },
// 导入标准数据成功后执行 // 导入标准数据成功后执行
onSuccess (response, file) { onSuccess (response, file, fileList) {
if (response.ok) { if (response.ok) {
const fileObj = {
id: file.response.data.id,
name: file.response.data.name
}
let ids;
let names;
if (this.ids && this.ids.length > 0) {
ids = this.ids + ',' + fileObj.id
names = this.names + ',' + fileObj.name
} else {
ids = fileObj.id
names = fileObj.name
}
this.$set(this.fileList, this.fileList.length, fileObj)
this.$emit('update:ids', ids)
this.$emit('update:names', names)
this.$emit('update:visible', false) this.$emit('update:visible', false)
this.$emit('onSuccess') this.$emit('onSuccess')
this.$message({ this.$message({
@@ -144,6 +199,44 @@ export default {
this.importForm.content = response.message this.importForm.content = response.message
} }
}, },
onRemove (file, fileList) {
const ids = fileList
.reduce((init, file) => init + ',' + (file.id || file.response.data.id), '')
.replace(/^,|,$/g, '')
const names = fileList
.reduce((init, file) => init + ',' + (file.name || file.response.data.name), '')
.replace(/^,|,$/g, '')
this.fileList = fileList.map(file => ({
id: file.id || file.response.data.id,
name: file.name
}))
this.$emit('update:ids', ids)
this.$emit('update:names', names)
this.$emit('onRemove')
},
onPreview (file) {
const id = file.id || file.response.data.id
this.$preview(id)
},
onExceed (files, fileList) {
this.$message.warning(`当前限制上传 1 个文件,本次上传了 ${files.length} 个文件,共上传了 ${files.length + fileList.length} 个文件`);
},
onError (err, file, fileList) {
const ids = fileList
.reduce((init, file) => init + ',' + (file.id || file.response.data.id), '')
.replace(/^,|,$/g, '')
const names = fileList
.reduce((init, file) => init + ',' + (file.name || file.response.data.name), '')
.replace(/^,|,$/g, '')
this.fileList = fileList.map(file => ({
id: file.id || file.response.data.id,
name: file.name
}))
this.$emit('update:ids', ids)
this.$emit('update:names', names)
this.$emit('onError')
this.$message.error('上传失败')
},
ManualUpload () { ManualUpload () {
this.$refs.importfile.submit(); this.$refs.importfile.submit();
} }
@@ -64,6 +64,7 @@ export default {
methods: { methods: {
handleChange (event) { handleChange (event) {
this.$emit('input', event) this.$emit('input', event)
this.$emit('change', event)
} }
} }
} }
@@ -242,4 +242,12 @@ export default {
.fileClass{ .fileClass{
line-height: 22px !important; line-height: 22px !important;
} }
.add-form-item /deep/ .el-form-item__content .el-form-item__error {
padding: 3px;
top: 15px;
right: 45px !important;
left: auto;
background: #fff3f3;
z-index: 9;
}
</style> </style>