修复多图上传无法预览已上传的文件
Some checks failed
CI / Test (ubuntu-latest) (push) Has been cancelled
CI / Test (windows-latest) (push) Has been cancelled
CI / Lint (ubuntu-latest) (push) Has been cancelled
CI / Lint (windows-latest) (push) Has been cancelled
CI / Check (ubuntu-latest) (push) Has been cancelled
CI / Check (windows-latest) (push) Has been cancelled
CI / CI OK (push) Has been cancelled
CodeQL / Analyze (javascript-typescript) (push) Has been cancelled
Deploy Website on push / Deploy Push Playground Ftp (push) Has been cancelled
Deploy Website on push / Deploy Push Docs Ftp (push) Has been cancelled
Deploy Website on push / Deploy Push Antd Ftp (push) Has been cancelled
Deploy Website on push / Deploy Push Element Ftp (push) Has been cancelled
Deploy Website on push / Deploy Push Naive Ftp (push) Has been cancelled
Deploy Website on push / Rerun on failure (push) Has been cancelled
Release Drafter / update_release_draft (push) Has been cancelled
Some checks failed
CI / Test (ubuntu-latest) (push) Has been cancelled
CI / Test (windows-latest) (push) Has been cancelled
CI / Lint (ubuntu-latest) (push) Has been cancelled
CI / Lint (windows-latest) (push) Has been cancelled
CI / Check (ubuntu-latest) (push) Has been cancelled
CI / Check (windows-latest) (push) Has been cancelled
CI / CI OK (push) Has been cancelled
CodeQL / Analyze (javascript-typescript) (push) Has been cancelled
Deploy Website on push / Deploy Push Playground Ftp (push) Has been cancelled
Deploy Website on push / Deploy Push Docs Ftp (push) Has been cancelled
Deploy Website on push / Deploy Push Antd Ftp (push) Has been cancelled
Deploy Website on push / Deploy Push Element Ftp (push) Has been cancelled
Deploy Website on push / Deploy Push Naive Ftp (push) Has been cancelled
Deploy Website on push / Rerun on failure (push) Has been cancelled
Release Drafter / update_release_draft (push) Has been cancelled
修复多图上传无法预览已上传的文件
This commit is contained in:
@@ -1,112 +1,135 @@
|
||||
<script setup lang="ts">
|
||||
import { computed, ref, watch } from 'vue';
|
||||
|
||||
import { useVModel } from '@vueuse/core';
|
||||
import { Modal, Upload } from 'ant-design-vue';
|
||||
import type { UploadFile } from 'ant-design-vue';
|
||||
|
||||
// 恢复使用原有的上传接口
|
||||
import { uploadFile } from '#/api/core/upload';
|
||||
import { Icon } from '#/components/icon';
|
||||
|
||||
defineOptions({
|
||||
name: 'UploadImage',
|
||||
inheritAttrs: false,
|
||||
// 定义接口
|
||||
interface FileItem {
|
||||
uid: string;
|
||||
name: string;
|
||||
status: 'uploading' | 'done' | 'error';
|
||||
url: string;
|
||||
}
|
||||
|
||||
// 定义props
|
||||
interface Props {
|
||||
modelValue: string[];
|
||||
multiple?: boolean;
|
||||
maxCount?: number;
|
||||
}
|
||||
|
||||
const props = withDefaults(defineProps<Props>(), {
|
||||
modelValue: () => [],
|
||||
multiple: true,
|
||||
maxCount: 9,
|
||||
});
|
||||
|
||||
const props = defineProps({
|
||||
modelValue: {
|
||||
type: Array as () => string[],
|
||||
default: () => [],
|
||||
},
|
||||
multiple: {
|
||||
type: Boolean,
|
||||
default: true,
|
||||
},
|
||||
maxCount: {
|
||||
type: Number,
|
||||
default: 9,
|
||||
},
|
||||
});
|
||||
const emit = defineEmits<{
|
||||
'update:modelValue': [value: string[]];
|
||||
}>();
|
||||
|
||||
const emits = defineEmits(['update:modelValue']);
|
||||
const fileList = ref<FileItem[]>([]);
|
||||
|
||||
const mValue = useVModel(props, 'modelValue', emits, {
|
||||
passive: true,
|
||||
});
|
||||
|
||||
const fileList = ref(
|
||||
props.modelValue.map((url) => ({
|
||||
// 初始化fileList
|
||||
const initFileList = () => {
|
||||
fileList.value = props.modelValue.map((url) => ({
|
||||
uid: url,
|
||||
name: url.split('/').pop() || 'file',
|
||||
status: 'done',
|
||||
url,
|
||||
})),
|
||||
);
|
||||
|
||||
// 监听 modelValue 变化,同步到 fileList
|
||||
watch(props.modelValue, (newVal) => {
|
||||
fileList.value = newVal.map((url) => ({
|
||||
uid: url,
|
||||
name: url.split('/').pop() || 'file',
|
||||
status: 'done',
|
||||
status: 'done' as const,
|
||||
url,
|
||||
}));
|
||||
});
|
||||
};
|
||||
|
||||
// 监听 fileList 变化,同步到 modelValue
|
||||
watch(fileList, (newVal) => {
|
||||
mValue.value = newVal
|
||||
// 初始化
|
||||
initFileList();
|
||||
|
||||
// 监听外部modelValue变化,同步到fileList
|
||||
watch(() => props.modelValue, (newVal) => {
|
||||
const currentUrls = fileList.value.filter(file => file.status === 'done').map(file => file.url);
|
||||
if (JSON.stringify(newVal) !== JSON.stringify(currentUrls)) {
|
||||
fileList.value = newVal.map((url) => ({
|
||||
uid: url,
|
||||
name: url.split('/').pop() || 'file',
|
||||
status: 'done' as const,
|
||||
url,
|
||||
}));
|
||||
}
|
||||
}, { deep: true });
|
||||
|
||||
// 统一通过updateModelValue函数更新modelValue
|
||||
const updateModelValue = () => {
|
||||
const urls = fileList.value
|
||||
.filter((file) => file.status === 'done' && file.url)
|
||||
.map((file) => file.url);
|
||||
});
|
||||
emit('update:modelValue', urls);
|
||||
};
|
||||
|
||||
// 使用原有的uploadFile接口
|
||||
const customRequest = async (options: any) => {
|
||||
const { file, onProgress, onSuccess, onError } = options;
|
||||
|
||||
// 创建上传中的文件项
|
||||
const uploadingFile: FileItem = {
|
||||
uid: file.uid,
|
||||
name: file.name,
|
||||
status: 'uploading',
|
||||
url: '',
|
||||
};
|
||||
|
||||
fileList.value = [...fileList.value, uploadingFile];
|
||||
|
||||
const customRequest = async (e: any) => {
|
||||
try {
|
||||
// 使用原有的uploadFile接口
|
||||
const res = await uploadFile({
|
||||
file: e.file,
|
||||
file: file,
|
||||
});
|
||||
|
||||
// 更新 fileList
|
||||
fileList.value = [
|
||||
...fileList.value.filter((file) => file.status !== 'uploading'),
|
||||
{
|
||||
uid: res.url,
|
||||
name: e.file.name,
|
||||
status: 'done',
|
||||
url: res.url,
|
||||
},
|
||||
];
|
||||
// 更新文件状态为完成
|
||||
const updatedFileList = fileList.value.map(item =>
|
||||
item.uid === file.uid
|
||||
? { ...item, status: 'done' as const, url: res.url }
|
||||
: item
|
||||
);
|
||||
|
||||
// 更新 modelValue
|
||||
mValue.value = fileList.value.map((file) => file.url);
|
||||
|
||||
// 触发上传完成回调
|
||||
e.onSuccess?.(res);
|
||||
fileList.value = updatedFileList;
|
||||
updateModelValue();
|
||||
onSuccess(res);
|
||||
} catch (error) {
|
||||
console.error('上传失败', error);
|
||||
e.onError?.(error);
|
||||
// 更新文件状态为错误
|
||||
const updatedFileList = fileList.value.map(item =>
|
||||
item.uid === file.uid
|
||||
? { ...item, status: 'error' as const }
|
||||
: item
|
||||
);
|
||||
|
||||
fileList.value = updatedFileList;
|
||||
onError(error);
|
||||
}
|
||||
};
|
||||
|
||||
const handleRemove = (file: any) => {
|
||||
// 从 fileList 中移除
|
||||
// 处理文件删除
|
||||
const handleRemove = (file: UploadFile) => {
|
||||
fileList.value = fileList.value.filter((item) => item.uid !== file.uid);
|
||||
|
||||
// 更新 modelValue
|
||||
mValue.value = fileList.value.map((file) => file.url);
|
||||
updateModelValue();
|
||||
};
|
||||
|
||||
// 预览功能
|
||||
const previewVisible = ref(false);
|
||||
const previewImage = ref('');
|
||||
const previewTitle = ref('');
|
||||
|
||||
const handlePreview = async (file) => {
|
||||
previewImage.value = file.response.url || file.preview;
|
||||
const handlePreview = async (file: UploadFile) => {
|
||||
previewImage.value = file.url || '';
|
||||
previewVisible.value = true;
|
||||
previewTitle.value =
|
||||
file.name || file.url.slice(Math.max(0, file.url.lastIndexOf('/') + 1));
|
||||
previewTitle.value = file.name || file.url?.split('/').pop() || '';
|
||||
};
|
||||
|
||||
// 计算是否还能上传更多图片
|
||||
// 计算是否显示上传按钮
|
||||
const showUploadButton = computed(() =>
|
||||
props.multiple
|
||||
? fileList.value.length < props.maxCount
|
||||
@@ -115,55 +138,39 @@ const showUploadButton = computed(() =>
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<Upload
|
||||
v-model:value="fileList"
|
||||
:custom-request="customRequest"
|
||||
:multiple="multiple"
|
||||
:limit="maxCount"
|
||||
:show-upload-list="{ showPreviewIcon: true, showRemoveIcon: true }"
|
||||
list-type="picture-card"
|
||||
@remove="handleRemove"
|
||||
@preview="handlePreview"
|
||||
>
|
||||
<div v-if="showUploadButton" class="upload-button">
|
||||
<Icon icon="ant-design:plus-outlined" />
|
||||
<div class="ant-upload-text">上传图片</div>
|
||||
</div>
|
||||
</Upload>
|
||||
<Modal
|
||||
v-model:visible="previewVisible"
|
||||
:title="previewTitle"
|
||||
footer=""
|
||||
width="60%"
|
||||
>
|
||||
<img alt="example" style="width: 100%" :src="previewImage" />
|
||||
</Modal>
|
||||
<div>
|
||||
<Upload
|
||||
:file-list="fileList"
|
||||
:custom-request="customRequest"
|
||||
:multiple="multiple"
|
||||
:max-count="maxCount"
|
||||
:show-upload-list="{
|
||||
showPreviewIcon: true,
|
||||
showRemoveIcon: true,
|
||||
}"
|
||||
list-type="picture-card"
|
||||
@remove="handleRemove"
|
||||
@preview="handlePreview"
|
||||
>
|
||||
<div v-if="showUploadButton" class="upload-button">
|
||||
<!-- 恢复使用Icon组件 -->
|
||||
<Icon icon="ant-design:plus-outlined" />
|
||||
<div class="ant-upload-text">上传图片</div>
|
||||
</div>
|
||||
</Upload>
|
||||
|
||||
<Modal
|
||||
v-model:visible="previewVisible"
|
||||
:title="previewTitle"
|
||||
footer=""
|
||||
width="60%"
|
||||
>
|
||||
<img alt="预览图片" style="width: 100%" :src="previewImage" />
|
||||
</Modal>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<style lang="less" scoped>
|
||||
.m-avatar-wrap {
|
||||
position: relative;
|
||||
height: 102px;
|
||||
width: 102px;
|
||||
|
||||
.m-avatar-icon-delete {
|
||||
position: absolute;
|
||||
top: 0;
|
||||
right: 0;
|
||||
cursor: pointer;
|
||||
opacity: 0;
|
||||
transition: opacity 0.3s;
|
||||
background-color: rgba(0, 0, 0, 0.5);
|
||||
color: white;
|
||||
border-radius: 0 0 0 4px;
|
||||
padding: 2px 4px;
|
||||
}
|
||||
|
||||
&:hover .m-avatar-icon-delete {
|
||||
opacity: 1;
|
||||
}
|
||||
}
|
||||
|
||||
.upload-button {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
|
||||
Reference in New Issue
Block a user