<a-form-item v-show="false" :labelCol="labelCol" :wrapperCol="wrapperCol" > <a-input v-decorator="[‘qrcodeUrl‘, {rules: [{required: false, message: ‘请上传二维码‘}]}]"> </a-input> </a-form-item> <a-form-item label="二维码" :labelCol="labelCol" :wrapperCol="wrapperCol" > <a-spin tip=‘Loading‘ :spinning="false"> <a-upload name="file" list-type="picture-card" :showUploadList="showUploadList" //是否展示 uploadList, 可设为一个对象,用于单独设定 showPreviewIcon 和 showRemoveIcon :action="url"//后端文件上传接口 :headers="headers"//请求头! :remove="handleFileRemove"//点击移除文件时的回调 :beforeUpload="beforeFileUpload"//上传文件之前的钩子,参数为上传的文件,若返回false
则停止上传。支持返回一个 Promise 对象,Promise 对象 reject 时则停止上传,resolve 时开始上传( resolve 传入File
或Blob
对象则上传 resolve 传入对象)
:fileList="defaultFileList"//已经上传的文件列表
:customRequest="uploadFunc" //通过覆盖默认的上传行为,可以自定义自己的上传实现
>
<a-button :disabled=‘defaultFileList.length>0‘ v-if=‘!defaultFileList.length>0‘> <a-icon type="upload"/> 上传二维码 </a-button> </a-upload>
</a-spin>
</a-form-item>
data () { return { showUploadList: { showPreviewIcon: false, showRemoveIcon: true }, url: process.env.VUE_APP_API_BASE_URL + ‘/storage/upload‘, defaultFileList: [], confirmLoading: false, form: this.$form.createForm(this), headers: { Authorization: ‘Bearer ‘ + this.$store.state.user.token }, } }, methods: { // 自定义上传方法 uploadFunc (file) { this.spinning = true const fileName = file.file.name const formData = new FormData() formData.append(‘file‘,file.file) upload(formData).then(res =>{ this.spinning = false const item = { uid: res.data.newFileName, name: res.data.original_file_name, status: ‘done‘, url: res.data.qrcode_url, } this.defaultFileList.push(item) file.onSuccess(res.data,file) this.$message.success(‘上传成功‘) }).catch(err=>{ this.$message.error(‘上传失败‘) this.spinning = false file.onError(res.data,file) }) }, handleFileRemove (file) { const name = file.name this.defaultFileList = this.defaultFileList.filter(item => { if (name !== item.name) return item }) }, beforeFileUpload (file) { return new Promise((resolve, reject) => { const isJPG = file.type === ‘image/jpeg‘ if (!isJPG) { this.$message.error(‘您只能上传jpg文件‘) return reject(new Error(‘您只能上传jpg文件‘)) } const isLt2M = file.size / 1024 / 1024 < 2 if (!isLt2M) { this.$message.error(‘文件大小不能大于2MB‘) return reject(new Error(‘文件大小不能大于2MB‘)) } this.$message.info(‘文件正在上传中...‘) return resolve(true) }) },
}
handleOk () { //提交表单
this.form.validateFields(async (err, values) => {
if(this.defaultFileList.length<1){
this.$message.error(‘请上传二维码‘)
return
}
values.qrcodeUrl = this.defaultFileList[0].url
values.originalFileName = this.defaultFileList[0].name
if (!err) {
this.confirmLoading = true
try {
if (this.type === ‘add‘) {
await addObj(values)
} else if (this.type === ‘edit‘) {
await putObj(values)
}
setTimeout(() => {
this.confirmLoading = false
this.defaultFileList.pop()
this.$message.success(‘操作成功‘)
this.ImgKey = Math.random()
this.$emit(‘ok‘)
this.handleCancel()
}, 1500)
} catch (e) {
this.confirmLoading = false
this.$message .error(e.msg)
}
}
})
}