<div class="clearfix">
<a-upload
:customRequest="uploadImg"
:remove="handleRemove"
list-type="picture-card"
:file-list="fileList"
@preview="handlePreview"
class="avatar-uploader"
>
<div v-if="fileList.length < 1">
<a-icon type="plus"/>
</div>
</a-upload>
<a-modal :visible="previewVisible" :footer="null" @cancel="handleCancel">
<img alt="example" style="width: 100%" :src="previewImage">
</a-modal>
</div>
<script>
import {upload} from '@/api/function'
function getBase64(file) {
return new Promise((resolve, reject) => {
const reader = new FileReader()
reader.readAsDataURL(file)
reader.onload = () => resolve(reader.result)
reader.onerror = error => reject(error)
})
}
export default {
name: 'Function',
components: {
STable
},
data() {
return {
previewVisible: false,
previewImage: '',
fileList: [],
}
},
methods: {
async onEditClick(record) {
try {
this.fileList = []
this.$nextTick(() => {
setTimeout(() => {
this.fileList1.push({
uid:-1,
status: 'done',
url: record.recordUrl
})
})
})
} catch (error) {}
},
handleCancel() {
this.previewVisible = false
},
async handlePreview(file) {
if (!file.url && !file.preview) {
file.preview = await getBase64(file.originFileObj)
}
this.previewImage = file.url || file.preview
this.previewVisible = true
},
async uploadPicture(file) {
try {
const res = await upload({ file })
return
} catch (error) {}
},
uploadImg(file) {
const newData = new FormData()
const uid = file.file.uid
newData.append('File', file.file)
upload(newData).then(res => {
this.fileList = []
this.fileList.push({
uid: uid,
status: 'done',
url: res.url
})
})
.catch(err => {
console.log('失败', err)
})
},
handleRemove(file) {
const index = this.fileList.indexOf(file)
const newFileList = this.fileList.slice()
newFileList.splice(index, 1)
this.fileList = newFileList
},
handleSubmit() {
try {
this.form.validateFields(async (err, values) => {
if (err) return
const requestPayload = {purchaseVoucherUrl: this.fileList[0].url}
const res = await add(requestPayload)
if(res.code !== 200) return this.$message.error('添加设备失败!')
this.$message.success('添加设备成功!')
this.getListData(1, 10)
this.visible = false
})
} catch (error) {
console.log('错误')
}
},
}
}
</script>
.avatar-uploader > .ant-upload {
width: 128px;
height: 128px;
}
.ant-upload-select-picture-card i {
font-size: 32px;
color: #999;
}
.ant-upload-select-picture-card .ant-upload-text {
margin-top: 8px;
color: #666;
}