feat: 支持出库实况图多选上传

This commit is contained in:
Nixevol
2026-07-16 16:27:08 +08:00
parent cd84e0ab11
commit 4afd89ef12
4 changed files with 50 additions and 26 deletions
@@ -48,14 +48,15 @@
</template>
</div>
<n-upload
v-if="!disabled"
:show-file-list="false"
accept=".jpg,.jpeg,.png,.webp,image/jpeg,image/png,image/webp"
:custom-request="upload"
:disabled="uploading"
style="width: auto"
>
<label v-if="!disabled" class="image-upload">
<input
type="file"
accept=".jpg,.jpeg,.png,.webp,image/jpeg,image/png,image/webp"
multiple
hidden
:disabled="uploading"
@change="onPick"
/>
<div :class="['upload-tile', { uploading }]">
<n-spin v-if="uploading" size="small" />
<template v-else>
@@ -63,7 +64,7 @@
<span>{{ t(recorded ? 'pages.outbound.addImage' : 'pages.outbound.recordImage') }}</span>
</template>
</div>
</n-upload>
</label>
</div>
<n-empty v-else :description="t('common.msg.noData')" class="empty-images" />
</n-spin>
@@ -83,7 +84,6 @@
<script setup lang="ts">
import { onMounted, ref, watch } from 'vue';
import { useI18n } from 'vue-i18n';
import type { UploadCustomRequestOptions } from 'naive-ui';
import { NIcon } from 'naive-ui';
import { AddOutline, ArrowDownOutline, ArrowUpOutline } from '@vicons/ionicons5';
import { outboundApi } from '@/api';
@@ -148,25 +148,40 @@ function validateFile(file: File) {
return true;
}
async function upload({ file, onFinish, onError }: UploadCustomRequestOptions) {
const raw = file.file;
if (!raw || !validateFile(raw)) {
onError();
return;
}
async function onPick(event: Event) {
const input = event.target as HTMLInputElement;
const files = Array.from(input.files || []).filter(validateFile);
input.value = '';
if (!files.length || uploading.value) return;
uploading.value = true;
try {
await props.beforeUpload?.();
const data = new FormData();
data.append('file', raw);
await outboundApi.uploadImage(props.traceCodeId, data);
page.value = 1;
await load();
emit('changed');
message.success(t('common.msg.operationSuccess'));
onFinish();
let uploaded = 0;
let failed = 0;
for (const file of files) {
try {
const data = new FormData();
data.append('file', file);
await outboundApi.uploadImage(props.traceCodeId, data);
uploaded += 1;
} catch {
// 单张失败不阻断本次选择中的其他图片。
failed += 1;
}
}
if (uploaded > 0) {
page.value = 1;
await load();
emit('changed');
if (failed > 0) {
message.warning(t('pages.outbound.imageUploadPartial', { success: uploaded, fail: failed }));
} else {
message.success(t('pages.outbound.imageUploadSuccess', { n: uploaded }));
}
}
} catch {
onError();
// 首次出库记录保存失败时不继续上传。
} finally {
uploading.value = false;
}
@@ -240,6 +255,11 @@ defineExpose({ reload: load });
.image-grid.empty {
justify-content: center;
}
.image-upload {
display: block;
width: 96px;
height: 96px;
}
.image-tile,
.upload-tile {
position: relative;
+2
View File
@@ -453,6 +453,8 @@
"moveImageDown": "Move later",
"imageFormatTip": "Only JPG, JPEG, PNG, and WebP images are supported",
"imageSizeTip": "Each image must not exceed 100MB",
"imageUploadSuccess": "Uploaded {n} outbound photo(s)",
"imageUploadPartial": "Upload partially completed: {success} succeeded, {fail} failed",
"deleteImageConfirm": "Delete this outbound photo?",
"selectFile": "Select File",
"importing": "Importing…",
+2
View File
@@ -453,6 +453,8 @@
"moveImageDown": "后移",
"imageFormatTip": "仅支持 JPG、JPEG、PNG、WebP 图片",
"imageSizeTip": "单张图片不能超过 100MB",
"imageUploadSuccess": "已上传 {n} 张出库实况图",
"imageUploadPartial": "部分上传完成:成功 {success} 张,失败 {fail} 张",
"deleteImageConfirm": "确认删除该出库实况图?",
"selectFile": "选择文件",
"importing": "导入中…",