This commit is contained in:
YeMingfei666 2025-03-31 16:29:06 +08:00
commit 8aef2b3bc6
2 changed files with 21 additions and 12 deletions

View File

@ -1,8 +1,8 @@
<!-- 图片上传组件 -->
<template>
<el-upload v-model:file-list="fileList" list-type="picture-card" :before-upload="handleBeforeUpload"
:http-request="handleUpload" :on-success="handleSuccess" :on-error="handleError" :on-exceed="handleExceed"
:accept="props.accept" :limit="props.limit" multiple>
:http-request="handleUpload" :on-success="handleSuccess" :on-progress="handleProgress" :on-error="handleError"
:on-exceed="handleExceed" :accept="props.accept" :limit="props.limit" multiple>
<el-icon>
<Plus />
</el-icon>
@ -15,11 +15,11 @@
<el-icon><zoom-in /></el-icon>
</span>
<!-- 删除 -->
<span @click="handleRemove(file.url!)">
<!-- <span @click="handleRemove(file.url!)">
<el-icon>
<Delete />
</el-icon>
</span>
</span> -->
</span>
</div>
</template>
@ -71,10 +71,10 @@ const props = defineProps({
default: "image/*", // '.png,.jpg,.jpeg,.gif,.bmp'
},
});
const emit = defineEmits(['upDataEvent'])
const previewVisible = ref(false); //
const previewImageIndex = ref(0); //
const progress = ref(0);
const modelValue = defineModel("modelValue", {
type: [Array] as PropType<string[]>,
required: true,
@ -96,14 +96,20 @@ function handleRemove(imageUrl: string) {
}
});
}
function handleProgress(event: any, file: any, fileList: any) {
// console.log("handleProgress", evt, file, fileList);
// this.progress = Math.round(event.percent); // Element UI
// console.log(` ${file.name} : ${this.progress}%`);
// progress.value = Math.round(event.percent);
console.log("文件上传进度", Math.round(event.percent));
}
/**
* 上传前校验
*/
function handleBeforeUpload(file: UploadRawFile) {
// accept accept
const acceptTypes = props.accept.split(",").map((type) => type.trim());
// accept
const isValidType = acceptTypes.some((type) => {
if (type === "image/*") {
@ -128,6 +134,7 @@ function handleBeforeUpload(file: UploadRawFile) {
ElMessage.warning("上传图片不能大于" + props.maxFileSize + "M");
return false;
}
emit('upDataEvent', true)
return true;
}
@ -145,7 +152,6 @@ function handleUpload(options: UploadRequestOptions) {
Object.keys(props.data).forEach((key) => {
formData.append(key, props.data[key]);
});
FileAPI.upload(formData)
.then((data) => {
resolve(data);
@ -168,6 +174,7 @@ function handleExceed(files: File[], uploadFiles: UploadUserFile[]) {
*/
const handleSuccess = (fileInfo: FileInfo, uploadFile: UploadUserFile) => {
ElMessage.success("上传成功");
emit('upDataEvent', false)
const index = fileList.value.findIndex((file) => file.uid === uploadFile.uid);
if (index !== -1) {
fileList.value[index].url = fileInfo;
@ -181,6 +188,7 @@ const handleSuccess = (fileInfo: FileInfo, uploadFile: UploadUserFile) => {
*/
const handleError = (error: any) => {
console.log("handleError");
emit('upDataEvent', false)
ElMessage.error("上传失败: " + error.message);
};

View File

@ -14,7 +14,7 @@
</li>
</ul>
<el-dialog v-model="showUpload" title="">
<MultiImageUpload v-model="fileList" />
<MultiImageUpload v-model="fileList" @upDataEvent="upDataEvent" />
<template #footer>
<el-button type="primary" :loading="loadings" @click="doSubmit">确认</el-button>
</template>
@ -41,18 +41,19 @@ export default {
mounted() {
},
methods: {
upDataEvent(d) {
this.loadings = d
},
upShowUp() {
this.showUpload = true;
},
async doSubmit() {
if (this.fileList.length) {
this.loadings = true
let res = await API.add({
pictureClassifyId: this.activedata,
urls: this.fileList
})
if (res.code == 200) {
this.loadings = false
this.showUpload = false;
this.fileList = []
this.$emit('getList')