refactor: ♻️ 多图上传修改最大上传图片数量

Former-commit-id: f429edf57f9a86b3811d180c7980585d275f93c7
This commit is contained in:
haoxr
2023-05-22 07:59:23 +08:00
parent 4784fbf475
commit 878e8abaf5

View File

@@ -28,11 +28,11 @@ import {
UploadRequestOptions, UploadRequestOptions,
UploadUserFile, UploadUserFile,
UploadFile, UploadFile,
UploadProps UploadProps,
} from 'element-plus'; } from "element-plus";
import { uploadFileApi, deleteFileApi } from '@/api/file'; import { uploadFileApi, deleteFileApi } from "@/api/file";
const emit = defineEmits(['update:modelValue']); const emit = defineEmits(["update:modelValue"]);
const props = defineProps({ const props = defineProps({
/** /**
@@ -40,36 +40,36 @@ const props = defineProps({
*/ */
modelValue: { modelValue: {
type: Array<string>, type: Array<string>,
default: [] as Array<string> default: [] as Array<string>,
}, },
/** /**
* 文件上传数量限制 * 文件上传数量限制
*/ */
limit: { limit: {
type: Number, type: Number,
default: 5 default: 10,
} },
}); });
const previewImgUrl = ref(''); const previewImgUrl = ref("");
const dialogVisible = ref(false); const dialogVisible = ref(false);
const fileList = ref([] as UploadUserFile[]); const fileList = ref([] as UploadUserFile[]);
watch( watch(
() => props.modelValue, () => props.modelValue,
(newVal: string[]) => { (newVal: string[]) => {
const filePaths = fileList.value.map(file => file.url); const filePaths = fileList.value.map((file) => file.url);
// 监听modelValue文件集合值未变化时跳过赋值 // 监听modelValue文件集合值未变化时跳过赋值
if ( if (
filePaths.length > 0 && filePaths.length > 0 &&
filePaths.length === newVal.length && filePaths.length === newVal.length &&
filePaths.every(x => newVal.some(y => y === x)) && filePaths.every((x) => newVal.some((y) => y === x)) &&
newVal.every(y => filePaths.some(x => x === y)) newVal.every((y) => filePaths.some((x) => x === y))
) { ) {
return; return;
} }
fileList.value = newVal.map(filePath => { fileList.value = newVal.map((filePath) => {
return { url: filePath } as UploadUserFile; return { url: filePath } as UploadUserFile;
}); });
}, },
@@ -87,17 +87,17 @@ async function handleUpload(options: UploadRequestOptions): Promise<any> {
// 上传成功需手动替换文件路径为远程URL否则图片地址为预览地址 blob:http:// // 上传成功需手动替换文件路径为远程URL否则图片地址为预览地址 blob:http://
const fileIndex = fileList.value.findIndex( const fileIndex = fileList.value.findIndex(
file => file.uid == (options.file as any).uid (file) => file.uid == (options.file as any).uid
); );
fileList.value.splice(fileIndex, 1, { fileList.value.splice(fileIndex, 1, {
name: fileInfo.name, name: fileInfo.name,
url: fileInfo.url url: fileInfo.url,
} as UploadUserFile); } as UploadUserFile);
emit( emit(
'update:modelValue', "update:modelValue",
fileList.value.map(file => file.url) fileList.value.map((file) => file.url)
); );
} }
@@ -111,8 +111,8 @@ function handleRemove(removeFile: UploadFile) {
deleteFileApi(filePath).then(() => { deleteFileApi(filePath).then(() => {
// 删除成功回调 // 删除成功回调
emit( emit(
'update:modelValue', "update:modelValue",
fileList.value.map(file => file.url) fileList.value.map((file) => file.url)
); );
}); });
} }
@@ -123,7 +123,7 @@ function handleRemove(removeFile: UploadFile) {
*/ */
function handleBeforeUpload(file: UploadRawFile) { function handleBeforeUpload(file: UploadRawFile) {
if (file.size > 2 * 1048 * 1048) { if (file.size > 2 * 1048 * 1048) {
ElMessage.warning('上传图片不能大于2M'); ElMessage.warning("上传图片不能大于2M");
return false; return false;
} }
return true; return true;
@@ -132,7 +132,7 @@ function handleBeforeUpload(file: UploadRawFile) {
/** /**
* 预览图片 * 预览图片
*/ */
const previewImg: UploadProps['onPreview'] = uploadFile => { const previewImg: UploadProps["onPreview"] = (uploadFile) => {
previewImgUrl.value = uploadFile.url!; previewImgUrl.value = uploadFile.url!;
dialogVisible.value = true; dialogVisible.value = true;
}; };