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

View File

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