<template>
<el-upload v-model:file-list="upload.fileList" :action="upload.action" list-type="picture-card"
:before-upload="upload.before" :on-success="upload.success" limit :on-preview="upload.proview"
:data-fileListCount="upload.fileList.length" :name="upload.name" :on-remove="upload.remove">
<el-icon>
<Plus />
</el-icon>
</el-upload>
<el-dialog v-model="upload.dialogVisible" :close-on-click-modal="false" :close-on-press-escape="false"
append-to-body>
<img w-full :src="upload.dialogImageUrl" alt="" style="width:100%;height:100%" />
</el-dialog>
</template>
<script setup>
import { ref, reactive } from 'vue'
import { Plus } from '@element-plus/icons-vue'
import baseUrl from '@/api/baseUrl.js'
const props = defineProps({
imageUrl: String,
});
const emit = defineEmits(['getImageUrl'])
const upload = reactive({
name: 'file',
action: '接口地址',
fileList: props.imageUrl ? [{ url: props.imageUrl }] : [],
dialogImageUrl: '',
dialogVisible: false,
before: (file) => {
const type = ['image/png', 'image/jpeg', 'image/gif'];
if (!type.includes(file.type)) {
ElMessage.error('不支持该类型文件')
return false
}
},
proview: ({ url }) => {
console.log(url);
upload.dialogImageUrl = url;
upload.dialogVisible = true;
},
success: (res, uploadFile, uploadFiles) => {
console.log(res);
console.log(uploadFile);
if (res.url && res.code === 1) {
upload.fileList = [{
url: uploadFile.url,
}];
emit('getImageUrl', res.url, uploadFile.url);
// document.querySelector('.el-upload--picture-card').style.display = "none"
} else {
ElMessage({ type: 'warning', message: res.msg });
upload.fileList = [];
emit('getImageUrl', '', '');
}
},
remove: (file, files) => {
upload.fileList = [];
emit('getImageUrl', '', '');
}
})
</script>
<style scope lang="scss">
[data-fileListCount='1'] {
.el-upload--picture-card {
display: none !important;
}
}
</style>