fix: 修复后台文件上传无反馈

This commit is contained in:
Nixevol
2026-07-04 07:22:16 +08:00
parent f51f45bcb1
commit 0f076dff45
2 changed files with 163 additions and 45 deletions
+2
View File
@@ -107,6 +107,7 @@
- 艺术品新建页子资源提示(2026-07-04):艺术品档案新建时仍先保存基础信息以获得 `artworkId`,溯源时间轴与认证文件是挂在作品下的子资源,必须在作品存在后维护。后台 `ArtworkEdit.vue` 新建态不再隐藏这两个区块,而是显示锁定空状态,提示保存基础信息后可维护;保存后自动跳转详情页并启用原有节点/认证文件管理。
- 艺术品档案删除约束(2026-07-04):艺术品档案删除仅用于误建/草稿类数据,后端 `ArtworkService.remove` 仍为软删除但会先检查该档案是否已有追溯码或查询日志;一旦存在业务痕迹则返回 `ARTWORK_IN_USE`,提示改为停用或归档。后台艺术品档案列表新增删除图标,仅 `codeCount=0` 时可点,已有追溯码的档案删除按钮置灰。
- 追溯码列表表头筛选(2026-07-04):`TraceCodePanel` 工具栏保留短码/追溯码/档案号等输入查询,批次、出库状态、分组、追溯码状态改为表头下拉筛选,支持多条件叠加;追溯码列表新增创建时间列。后端 `QueryTraceCodeDto` / `ExportTraceCodeDto` 新增 `outboundStatus=OUTBOUND|NOT_OUTBOUND`,`TraceCodeService` 用 `AND` 合并出库状态和关键词等 OR 条件,避免筛选互相覆盖;导出当前筛选会带上出库状态。
- 后台上传组件修复(2026-07-04):`MediaUpload.vue` 不再使用 Naive UI `n-upload` 的受控 `file-list + custom-request` 组合,改为原生文件 input + 自管上传状态。选择文件后立即显示上传占位,成功后回写 `{ key,url,name,type }`,失败时明确提示;该组件覆盖出库实况图、认证文件、动态字段图片/文件等入口。`MediaSorter.vue` 仍用于作品/节点多媒体排序上传。
## 当前代码基准与注意事项
@@ -116,6 +117,7 @@
- 出库导入:以短码定位具体一套码,导入列不再包含 SKU 或手填档案编号;档案编号由作品自动生成并在日志/导出/C 端展示。
- 后台列表约定:新列表页优先复用 `apps/admin/src/composables/useTablePage.ts`,页面根容器使用 `class="page table-page"`,`n-data-table` 使用 `class="table-fill" flex-height` 且关闭内置分页,底部统一外置 `n-pagination`;后端列表查询如继承 `PaginationDto`,需遵守 `pageSize=0` 不分页语义。
- 后台筛选约定:列表中输入型检索保留在工具栏;枚举/下拉型筛选优先放到对应表头下拉中,并把所有筛选条件集中到同一个 query 对象后叠加请求。
- 后台上传约定:单文件或少量附件优先复用 `MediaUpload.vue`,多图/视频排序场景使用 `MediaSorter.vue`;上传组件必须在选择文件后立即显示可见反馈,不能只依赖第三方 Upload 内部列表状态。
- 开发脚本约定:`python scripts/dev.py` 会启动 `nest start --watch`,API 运行时不要手动或脚本化删除 `apps/api/dist`;如遇缺模块或半残 `dist`,停止当前 dev 进程后重新运行 `python scripts/dev.py`。
- 客户端:当前 `Trace.vue` 依赖 `openMediaViewer.ts` + PhotoSwipe,不再使用旧 `MediaViewer.vue`;顶部语言菜单和追溯码复制是用户当前未提交改动的一部分,后续若继续修改客户端,需保护这些改动。
- 文案残留:`apps/client/src/locales/*` 仍保留 `privacy.note`,但当前页面不显示底部隐私说明;若按合规要求恢复,需要在 `Trace.vue` 重新渲染对应文案。
+161 -45
View File
@@ -1,29 +1,47 @@
<template>
<n-upload
:list-type="listType"
:file-list="fileList"
:max="max"
:accept="accept"
:custom-request="doUpload"
@remove="onRemove"
>
<template v-if="listType === 'image-card'">
<div class="upd-trigger">
<div class="media-upload" :class="{ text: listType === 'text' }">
<div v-if="listType === 'image-card'" class="image-grid">
<div v-for="m in items" :key="m.key" class="image-item">
<img v-if="isImage(m)" class="image-thumb" :src="m.url || m.key" :alt="m.name || m.key" />
<div v-else class="file-thumb">{{ fileExt(m) }}</div>
<button class="remove" type="button" :title="t('common.action.delete')" @click="remove(m.key)">×</button>
</div>
<div v-for="u in uploading" :key="u.id" class="image-item uploading">
<n-spin size="small" />
</div>
<label v-if="canUpload" class="image-item add">
<input type="file" :accept="accept" :multiple="allowMultiple" hidden @change="onPick" />
<n-icon :component="AddOutline" :size="22" />
<span>{{ t('common.action.upload') }}</span>
</label>
</div>
<div v-else class="file-list">
<div v-for="m in items" :key="m.key" class="file-row">
<span class="file-name" :title="m.name || m.key">{{ m.name || m.key }}</span>
<n-button quaternary circle size="tiny" type="error" :title="t('common.action.delete')" @click="remove(m.key)">
×
</n-button>
</div>
</template>
<n-button v-else>
<template #icon><n-icon :component="CloudUploadOutline" /></template>
{{ t('common.action.upload') }}
</n-button>
</n-upload>
<div v-for="u in uploading" :key="u.id" class="file-row uploading-row">
<n-spin size="small" />
<span>{{ t('common.action.upload') }}...</span>
</div>
<label v-if="canUpload" class="file-button">
<input type="file" :accept="accept" :multiple="allowMultiple" hidden @change="onPick" />
<n-button tag="span">
<template #icon><n-icon :component="CloudUploadOutline" /></template>
{{ t('common.action.upload') }}
</n-button>
</label>
</div>
</div>
</template>
<script setup lang="ts">
import { computed } from 'vue';
import { computed, ref } from 'vue';
import { useI18n } from 'vue-i18n';
import type { UploadCustomRequestOptions, UploadFileInfo } from 'naive-ui';
import { NButton, NIcon, NSpin } from 'naive-ui';
import { AddOutline, CloudUploadOutline } from '@vicons/ionicons5';
import { post } from '@/api/request';
import { message } from '@/utils/feedback';
@@ -42,50 +60,148 @@ const props = withDefaults(
const emit = defineEmits<{ (e: 'update:modelValue', v: MediaItem[]): void }>();
const { t } = useI18n();
const uploading = ref<{ id: number }[]>([]);
let seq = 0;
const items = computed(() => props.modelValue || []);
const listType = computed(() => (props.type === 'image' ? 'image-card' : 'text'));
const accept = computed(() =>
props.type === 'image' ? 'image/*' : props.type === 'video' ? 'video/*' : 'application/pdf,image/*',
);
const allowMultiple = computed(() => props.max > 1);
const canUpload = computed(() => items.value.length + uploading.value.length < props.max);
const fileList = computed<UploadFileInfo[]>(() =>
(props.modelValue || []).map((m) => ({
id: m.key,
name: m.name || m.key,
status: 'finished',
url: m.url,
})),
);
function isImage(m: MediaItem) {
return props.type === 'image' || /^image\//.test(m.type || '') || /\.(png|jpe?g|gif|webp|bmp|svg)$/i.test(m.url || m.key);
}
async function doUpload({ file, onFinish, onError }: UploadCustomRequestOptions) {
try {
const fd = new FormData();
fd.append('file', file.file as File);
const res: any = await post(`/upload?type=${props.type}`, fd);
emit('update:modelValue', [
...(props.modelValue || []),
{ key: res.key, url: res.url, name: res.name, type: props.type },
]);
onFinish();
} catch (e) {
message.error((e as Error)?.message || 'upload failed');
onError();
function fileExt(m: MediaItem) {
const name = m.name || m.key;
const ext = name.split('.').pop();
return ext && ext.length <= 5 ? ext.toUpperCase() : 'FILE';
}
async function onPick(e: Event) {
const input = e.target as HTMLInputElement;
const files = Array.from(input.files || []);
input.value = '';
for (const file of files) {
if (items.value.length + uploading.value.length >= props.max) {
message.warning(t('common.msg.maxReached', { n: props.max }));
break;
}
await uploadOne(file);
}
}
function onRemove({ file }: { file: UploadFileInfo }) {
emit('update:modelValue', (props.modelValue || []).filter((m) => m.key !== file.id));
return true;
async function uploadOne(file: File) {
const holder = { id: ++seq };
uploading.value.push(holder);
try {
const fd = new FormData();
fd.append('file', file);
const res: any = await post(`/upload?type=${props.type}`, fd);
emit('update:modelValue', [
...items.value,
{ key: res.key, url: res.url, name: res.name || file.name, type: props.type },
]);
} catch (err: any) {
message.error(err?.message || 'upload failed');
} finally {
uploading.value = uploading.value.filter((u) => u.id !== holder.id);
}
}
function remove(key: string) {
emit('update:modelValue', items.value.filter((m) => m.key !== key));
}
</script>
<style scoped>
.upd-trigger {
.media-upload {
width: 100%;
}
.image-grid {
display: flex;
flex-wrap: wrap;
gap: 10px;
}
.image-item {
position: relative;
width: 96px;
height: 96px;
border-radius: 8px;
overflow: hidden;
border: 1px solid var(--n-border-color, #e0e0e6);
background: #f4f7fc;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
gap: 4px;
}
.image-thumb {
width: 100%;
height: 100%;
object-fit: cover;
}
.file-thumb {
font-size: 12px;
color: #6b7280;
}
.image-item.add,
.image-item.uploading {
flex-direction: column;
gap: 4px;
font-size: 12px;
color: #6b7280;
cursor: pointer;
border-style: dashed;
background: #fafcff;
}
.image-item.uploading {
cursor: default;
}
.remove {
position: absolute;
top: 2px;
right: 2px;
width: 20px;
height: 20px;
border: none;
border-radius: 50%;
background: rgba(0, 0, 0, 0.55);
color: #fff;
font-size: 14px;
line-height: 20px;
cursor: pointer;
padding: 0;
}
.remove:hover {
background: #d03050;
}
.file-list {
display: flex;
flex-direction: column;
gap: 8px;
align-items: flex-start;
}
.file-row {
display: inline-flex;
align-items: center;
gap: 8px;
max-width: 100%;
min-height: 28px;
}
.file-name {
max-width: 360px;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
.uploading-row {
color: #6b7280;
font-size: 13px;
}
.file-button {
display: inline-flex;
}
</style>