mirror of
https://gitee.com/qqtt-ah/ecom-dmb.git
synced 2026-11-23 23:19:19 +08:00
217 lines
5.3 KiB
Vue
217 lines
5.3 KiB
Vue
<script lang="ts" setup>
|
|
/**
|
|
* 反馈多图:先本地预览(可选/粘贴),提交时由 persist() 调 uploadFiles 批量上传。
|
|
*/
|
|
import type { UploadFile } from 'ant-design-vue';
|
|
|
|
import { computed, onBeforeUnmount, onMounted, ref } from 'vue';
|
|
|
|
import { PlusOutlined } from '@ant-design/icons-vue';
|
|
import { Upload, message } from 'ant-design-vue';
|
|
|
|
import { FileCategory } from '#/constants/file-category';
|
|
import { uploadFiles } from '#/utils/upload-file';
|
|
|
|
defineOptions({ name: 'FeedbackImageUpload' });
|
|
|
|
type PendingItem = {
|
|
uid: string;
|
|
file: File;
|
|
previewUrl: string;
|
|
};
|
|
|
|
const props = withDefaults(
|
|
defineProps<{
|
|
/** 最大张数 */
|
|
maxCount?: number;
|
|
disabled?: boolean;
|
|
}>(),
|
|
{
|
|
maxCount: 9,
|
|
disabled: false,
|
|
},
|
|
);
|
|
|
|
const rootRef = ref<HTMLElement | null>(null);
|
|
const pending = ref<PendingItem[]>([]);
|
|
const persisting = ref(false);
|
|
|
|
const canAdd = computed(
|
|
() => !props.disabled && pending.value.length < props.maxCount,
|
|
);
|
|
|
|
const fileList = computed<UploadFile[]>(() =>
|
|
pending.value.map((p) => ({
|
|
uid: p.uid,
|
|
name: p.file.name,
|
|
status: 'done' as const,
|
|
url: p.previewUrl,
|
|
thumbUrl: p.previewUrl,
|
|
})),
|
|
);
|
|
|
|
function revokeAll() {
|
|
for (const p of pending.value) {
|
|
URL.revokeObjectURL(p.previewUrl);
|
|
}
|
|
}
|
|
|
|
function clear() {
|
|
revokeAll();
|
|
pending.value = [];
|
|
}
|
|
|
|
function addFiles(files: File[]) {
|
|
if (props.disabled) return;
|
|
let remain = props.maxCount - pending.value.length;
|
|
if (remain <= 0) {
|
|
message.warning(`最多选择 ${props.maxCount} 张图片`);
|
|
return;
|
|
}
|
|
const next = [...pending.value];
|
|
for (const raw of files) {
|
|
if (remain <= 0) {
|
|
message.warning(`最多选择 ${props.maxCount} 张图片`);
|
|
break;
|
|
}
|
|
if (!raw.type?.startsWith('image/')) {
|
|
message.error('只能选择图片');
|
|
continue;
|
|
}
|
|
if (raw.size > 5 * 1024 * 1024) {
|
|
message.error(`「${raw.name || '图片'}」超过 5MB`);
|
|
continue;
|
|
}
|
|
const named =
|
|
raw.name && raw.name.trim()
|
|
? raw
|
|
: new File(
|
|
[raw],
|
|
`paste-${Date.now()}-${remain}.${raw.type.split('/')[1] || 'png'}`,
|
|
{ type: raw.type },
|
|
);
|
|
next.push({
|
|
uid: `local-${Date.now()}-${Math.random().toString(36).slice(2, 8)}`,
|
|
file: named,
|
|
previewUrl: URL.createObjectURL(named),
|
|
});
|
|
remain -= 1;
|
|
}
|
|
pending.value = next;
|
|
}
|
|
|
|
/** 拦截自动上传:只入本地预览列表 */
|
|
function beforeUpload(file: File) {
|
|
addFiles([file]);
|
|
return false;
|
|
}
|
|
|
|
function handleRemove(file: UploadFile) {
|
|
const idx = pending.value.findIndex((p) => p.uid === file.uid);
|
|
if (idx >= 0) {
|
|
URL.revokeObjectURL(pending.value[idx]!.previewUrl);
|
|
pending.value = pending.value.filter((_, i) => i !== idx);
|
|
}
|
|
return true;
|
|
}
|
|
|
|
function extractClipboardImages(event: ClipboardEvent): File[] {
|
|
const files: File[] = [];
|
|
const items = event.clipboardData?.items;
|
|
if (items) {
|
|
for (const item of Array.from(items)) {
|
|
if (item.kind === 'file' && item.type.startsWith('image/')) {
|
|
const f = item.getAsFile();
|
|
if (f) files.push(f);
|
|
}
|
|
}
|
|
}
|
|
if (files.length === 0 && event.clipboardData?.files?.length) {
|
|
for (const f of Array.from(event.clipboardData.files)) {
|
|
if (f.type.startsWith('image/')) files.push(f);
|
|
}
|
|
}
|
|
return files;
|
|
}
|
|
|
|
function onPaste(event: ClipboardEvent) {
|
|
if (props.disabled || persisting.value) return;
|
|
const images = extractClipboardImages(event);
|
|
if (images.length === 0) return;
|
|
event.preventDefault();
|
|
event.stopPropagation();
|
|
addFiles(images);
|
|
}
|
|
|
|
function onWindowPaste(event: ClipboardEvent) {
|
|
const root = rootRef.value;
|
|
if (!root || props.disabled) return;
|
|
const active = document.activeElement as Node | null;
|
|
if (!active) return;
|
|
if (root === active || root.contains(active)) {
|
|
onPaste(event);
|
|
return;
|
|
}
|
|
const form = root.closest('form');
|
|
if (form && form.contains(active) && extractClipboardImages(event).length > 0) {
|
|
onPaste(event);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 批量上传本地待传图,返回 fileId 列表;无图返回 []。
|
|
* 上传成功后不清空本地预览(由父级关闭/重置时 clear)。
|
|
*/
|
|
async function persist(): Promise<number[]> {
|
|
if (pending.value.length === 0) return [];
|
|
persisting.value = true;
|
|
try {
|
|
const infos = await uploadFiles(
|
|
pending.value.map((p) => p.file),
|
|
{
|
|
category: FileCategory.IMAGE,
|
|
errorMessage: '图片上传失败',
|
|
},
|
|
);
|
|
return [...new Set(infos.map((i) => i.id).filter(Boolean))];
|
|
} finally {
|
|
persisting.value = false;
|
|
}
|
|
}
|
|
|
|
onMounted(() => {
|
|
window.addEventListener('paste', onWindowPaste);
|
|
});
|
|
|
|
onBeforeUnmount(() => {
|
|
window.removeEventListener('paste', onWindowPaste);
|
|
revokeAll();
|
|
});
|
|
|
|
defineExpose({ persist, clear, getPendingCount: () => pending.value.length });
|
|
</script>
|
|
|
|
<template>
|
|
<div
|
|
ref="rootRef"
|
|
tabindex="0"
|
|
class="feedback-image-upload outline-none"
|
|
@paste="onPaste"
|
|
>
|
|
<Upload
|
|
list-type="picture-card"
|
|
:file-list="fileList"
|
|
:disabled="disabled || persisting"
|
|
accept="image/*"
|
|
:max-count="maxCount"
|
|
:before-upload="beforeUpload"
|
|
@remove="handleRemove"
|
|
>
|
|
<div v-if="canAdd">
|
|
<PlusOutlined />
|
|
<div class="mt-1 text-xs">上传/ctrl+v</div>
|
|
</div>
|
|
</Upload>
|
|
</div>
|
|
</template>
|