2025-03-05 17:30:00 +08:00
|
|
|
|
<template>
|
|
|
|
|
|
<el-upload
|
|
|
|
|
|
ref="upload"
|
|
|
|
|
|
class="upload-demo"
|
|
|
|
|
|
:limit="props.limit"
|
|
|
|
|
|
:auto-upload="false"
|
|
|
|
|
|
:file-list="fileList"
|
|
|
|
|
|
:on-change="fileChange"
|
|
|
|
|
|
:before-remove="handleDelFile"
|
|
|
|
|
|
>
|
|
|
|
|
|
<el-button type="primary" :disabled="fileList.length == 5">点击上传</el-button>
|
|
|
|
|
|
<template #tip>
|
|
|
|
|
|
<div v-if="props.format.length" class="el-upload__tip">只能上传{{ props.format.join() }} 文件,且不超过20MB</div>
|
|
|
|
|
|
</template>
|
|
|
|
|
|
</el-upload>
|
|
|
|
|
|
</template>
|
|
|
|
|
|
|
|
|
|
|
|
<script setup>
|
|
|
|
|
|
import { ref, watch } from 'vue';
|
|
|
|
|
|
import { ElMessage } from 'element-plus';
|
2025-03-10 17:32:04 +08:00
|
|
|
|
import { CommonUpload } from '@/apis';
|
2025-03-05 17:30:00 +08:00
|
|
|
|
|
2025-03-10 17:32:04 +08:00
|
|
|
|
const emit = defineEmits(['update:attrs']);
|
2025-03-05 17:30:00 +08:00
|
|
|
|
const props = defineProps({
|
2025-03-10 17:32:04 +08:00
|
|
|
|
attrs: {
|
2025-03-05 17:30:00 +08:00
|
|
|
|
type: Array,
|
|
|
|
|
|
default: () => [],
|
|
|
|
|
|
},
|
|
|
|
|
|
limit: {
|
|
|
|
|
|
type: Number,
|
|
|
|
|
|
default: 5,
|
|
|
|
|
|
},
|
|
|
|
|
|
format: {
|
|
|
|
|
|
type: Array,
|
|
|
|
|
|
default: () => [],
|
|
|
|
|
|
},
|
|
|
|
|
|
size: {
|
|
|
|
|
|
type: Number,
|
|
|
|
|
|
default: 20,
|
|
|
|
|
|
},
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
/* --------------- data --------------- */
|
|
|
|
|
|
// #region
|
|
|
|
|
|
|
|
|
|
|
|
const fileList = ref([]);
|
|
|
|
|
|
watch(
|
2025-03-10 17:32:04 +08:00
|
|
|
|
() => props.attrs,
|
2025-03-05 17:30:00 +08:00
|
|
|
|
(val) => {
|
|
|
|
|
|
fileList.value = val;
|
|
|
|
|
|
},
|
|
|
|
|
|
{ deep: true, immediate: true }
|
|
|
|
|
|
);
|
|
|
|
|
|
// #endregion
|
|
|
|
|
|
|
|
|
|
|
|
/* --------------- methods --------------- */
|
|
|
|
|
|
// #region
|
|
|
|
|
|
|
2025-03-10 17:32:04 +08:00
|
|
|
|
async function fileChange(file, list) {
|
2025-03-05 17:30:00 +08:00
|
|
|
|
let formatReg = true;
|
|
|
|
|
|
if (props.format.length > 0) {
|
|
|
|
|
|
formatReg = props.format.includes(file.name.slice(file.name.lastIndexOf('.') + 1));
|
|
|
|
|
|
}
|
|
|
|
|
|
const fileSize = file.size / 1024 / 1024 < props.size;
|
|
|
|
|
|
if (!formatReg) {
|
|
|
|
|
|
ElMessage.error(`上传文件格式不正确,仅支持${props.format.join('/ ')}格式`);
|
|
|
|
|
|
delAttr(file.uid, list);
|
|
|
|
|
|
}
|
|
|
|
|
|
if (!fileSize) {
|
|
|
|
|
|
ElMessage.error(`上传文件大小不能超过${props.size}MB`);
|
|
|
|
|
|
delAttr(file.uid, list);
|
|
|
|
|
|
}
|
2025-03-10 17:32:04 +08:00
|
|
|
|
await rowUploadPicture(file);
|
2025-03-05 17:30:00 +08:00
|
|
|
|
}
|
|
|
|
|
|
function handleDelFile(file) {
|
|
|
|
|
|
delAttr(file.uid, fileList.value);
|
|
|
|
|
|
}
|
2025-03-10 17:32:04 +08:00
|
|
|
|
function delAttr(uid, list = []) {
|
|
|
|
|
|
if (!uid || !list) return false;
|
|
|
|
|
|
let i = list.findIndex((v) => v.uid == uid);
|
2025-03-05 17:30:00 +08:00
|
|
|
|
if (i < 0) return;
|
|
|
|
|
|
else list.splice(i, 1);
|
2025-03-10 17:32:04 +08:00
|
|
|
|
emit('update:attrs', fileList.value);
|
2025-03-05 17:30:00 +08:00
|
|
|
|
}
|
2025-03-10 17:32:04 +08:00
|
|
|
|
|
|
|
|
|
|
async function rowUploadPicture({ raw, name, uid }) {
|
|
|
|
|
|
const formData = new FormData();
|
|
|
|
|
|
formData.append('file', raw);
|
|
|
|
|
|
const res = await CommonUpload(formData);
|
|
|
|
|
|
if (res.code === 200) {
|
|
|
|
|
|
fileList.value.push({
|
|
|
|
|
|
name,
|
|
|
|
|
|
url: res.data.url,
|
|
|
|
|
|
status: 'success',
|
|
|
|
|
|
uid,
|
|
|
|
|
|
});
|
|
|
|
|
|
emit('update:attrs', fileList.value);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-03-05 17:30:00 +08:00
|
|
|
|
// #endregion
|
|
|
|
|
|
</script>
|
|
|
|
|
|
|
|
|
|
|
|
<style lang="scss" scoped></style>
|