feat: 完善后台导入日志权限与备份恢复

This commit is contained in:
Nixevol
2026-07-04 09:06:35 +08:00
parent 5ae3b12fff
commit d484da3862
61 changed files with 2743 additions and 436 deletions
+281 -59
View File
@@ -1,90 +1,312 @@
<template>
<div class="page">
<n-card :title="t('pages.outbound.import')" :bordered="false">
<n-alert type="info" :show-icon="true" style="margin-bottom: 16px">
{{ t('pages.outbound.tip') }}
</n-alert>
<n-space>
<n-button @click="downloadTemplate">
<template #icon><n-icon :component="DownloadOutline" /></template>
{{ t('common.action.downloadTemplate') }}
</n-button>
<n-upload
:show-file-list="false"
accept=".xlsx,.xls"
:custom-request="doImport"
:disabled="importing || !auth.canWrite"
>
<n-button type="primary" :loading="importing">
<template #icon><n-icon :component="CloudUploadOutline" /></template>
{{ t('pages.outbound.selectFile') }}
</n-button>
</n-upload>
</n-space>
<n-card v-if="result" embedded :title="t('pages.outbound.importResult')" style="margin-top: 16px">
<n-space align="center" size="large">
<n-statistic :label="t('common.label.total', { n: result.total })" :value="result.total" />
<n-statistic :label="t('pages.outbound.successCount')">
<span style="color: #138a72">{{ result.successCount }}</span>
</n-statistic>
<n-statistic :label="t('pages.outbound.failCount')">
<span style="color: #c0392b">{{ result.failCount }}</span>
</n-statistic>
<n-tag :type="statusType(result.status)">{{ t(`common.status.${result.status}`) }}</n-tag>
<n-button v-if="result.resultFileUrl" text tag="a" :href="result.resultFileUrl" target="_blank" type="primary">
<div class="page table-page outbound-page">
<n-card :bordered="false" class="outbound-card">
<template #header>出库批量导入</template>
<template #header-extra>
<n-space>
<n-button @click="downloadTemplate">
<template #icon><n-icon :component="DownloadOutline" /></template>
{{ t('pages.outbound.downloadResult') }}
下载 ZIP 模板
</n-button>
<n-upload
:show-file-list="false"
accept=".zip"
:custom-request="uploadPreview"
:disabled="busy || !auth.canWrite"
>
<n-button type="primary" :loading="previewing">
<template #icon><n-icon :component="CloudUploadOutline" /></template>
上传 ZIP 并预览
</n-button>
</n-upload>
</n-space>
</template>
<n-alert type="info" :show-icon="true" class="section-alert">
ZIP 内需要包含 1 个 xlsx 文件和 images 文件夹;xlsx 中出库实况图路径填写 images/xxx.jpg,多张用英文分号分隔。
</n-alert>
<n-card v-if="session" embedded class="summary-card" :bordered="false">
<n-space align="center" size="large" wrap>
<n-statistic label="总行数" :value="session.total || 0" />
<n-statistic label="有效行">
<span class="success-text">{{ session.validCount || 0 }}</span>
</n-statistic>
<n-statistic label="错误行">
<span class="danger-text">{{ session.invalidCount || 0 }}</span>
</n-statistic>
<n-tag :type="statusType(session.status)">{{ session.status }}</n-tag>
<n-progress
v-if="busy"
type="line"
:percentage="progressPercent"
:indicator-placement="'inside'"
class="progress"
/>
<n-button
v-if="auth.canWrite"
type="primary"
:disabled="!canCommit"
:loading="committing"
@click="commitImport"
>
确认导入
</n-button>
<n-button v-if="result?.resultFileUrl" tag="a" :href="result.resultFileUrl" target="_blank">
<template #icon><n-icon :component="DownloadOutline" /></template>
下载导入结果
</n-button>
</n-space>
<n-text v-if="session.error" type="error" class="session-error">{{ session.error }}</n-text>
</n-card>
<n-data-table
:columns="columns"
:data="rows"
:loading="loading"
:pagination="false"
class="table-fill"
flex-height
remote
:scroll-x="1320"
:row-key="(r) => r.id"
/>
<div class="table-pagination">
<n-pagination v-bind="pagination" :disabled="loading" />
</div>
</n-card>
</div>
</template>
<script setup lang="ts">
import { ref } from 'vue';
import { useI18n } from 'vue-i18n';
import { computed, h, ref } from 'vue';
import type { UploadCustomRequestOptions } from 'naive-ui';
import { DownloadOutline, CloudUploadOutline } from '@vicons/ionicons5';
import { jobApi, post } from '@/api';
import { NButton, NIcon, NImage, NTag, NText, type DataTableColumns } from 'naive-ui';
import { CloudUploadOutline, DownloadOutline } from '@vicons/ionicons5';
import { jobApi, outboundApi } from '@/api';
import { useAuthStore } from '@/stores/auth';
import { message } from '@/utils/feedback';
import { statusType } from '@/utils/format';
import { downloadFile } from '@/utils/download';
import { fmt, statusType } from '@/utils/format';
import { useTablePage } from '@/composables/useTablePage';
import { openMediaViewer } from '@/composables/openMediaViewer';
const { t } = useI18n();
const auth = useAuthStore();
const importing = ref(false);
const rows = ref<any[]>([]);
const session = ref<any>(null);
const result = ref<any>(null);
const sessionId = ref('');
const loading = ref(false);
const previewing = ref(false);
const committing = ref(false);
const progressPercent = ref(0);
async function downloadTemplate() {
await downloadFile('/outbound/import/template', 'outbound-import-template.xlsx');
const busy = computed(() => previewing.value || committing.value);
const canCommit = computed(() => session.value?.status === 'READY' && Number(session.value?.invalidCount || 0) === 0);
const { pagination, page, pageSize, resetPage, setPageResult } = useTablePage(loadRows, 20);
const columns = computed<DataTableColumns<any>>(() => [
{ title: '行号', key: 'rowNo', width: 80 },
{
title: '状态',
key: 'status',
width: 110,
align: 'center',
render: (row) => h(NTag, { size: 'small', type: row.status === 'VALID' || row.status === 'IMPORTED' ? 'success' : 'error' }, { default: () => row.status }),
},
{ title: '追溯短码', key: 'shortCode', width: 150, ellipsis: { tooltip: true } },
{ title: '批次编号', key: 'batchNo', width: 180, ellipsis: { tooltip: true } },
{ title: '出库时间', key: 'outTime', width: 170, render: (row) => fmt(row.outTime || row.preview?.outTime) },
{
title: '出库实况图',
key: 'images',
width: 240,
render: (row) => renderImages(row),
},
{ title: '上链哈希', key: 'txHash', width: 240, ellipsis: { tooltip: true } },
{ title: '备注', key: 'remark', width: 180, ellipsis: { tooltip: true } },
{
title: '错误原因',
key: 'reason',
minWidth: 260,
ellipsis: { tooltip: true },
render: (row) => row.reason || h(NText, { depth: 3 }, { default: () => '-' }),
},
]);
function rowImages(row: any) {
const list = Array.isArray(row.outImageKeys) && row.outImageKeys.length ? row.outImageKeys : row.preview?.images;
return Array.isArray(list) ? list.filter((item) => item?.url || item?.key) : [];
}
async function doImport({ file, onFinish, onError }: UploadCustomRequestOptions) {
importing.value = true;
function renderImages(row: any) {
const images = rowImages(row);
if (!images.length) return h(NText, { depth: 3 }, { default: () => '-' });
const visible = images.slice(0, 3);
return h('div', { class: 'preview-images' }, [
...visible.map((item: any, index: number) =>
h(NImage, {
width: 38,
height: 38,
objectFit: 'cover',
src: item.url || item.key,
previewDisabled: true,
onClick: () => previewImages(images, index),
}),
),
images.length > visible.length
? h(
NButton,
{ size: 'tiny', quaternary: true, onClick: () => previewImages(images, visible.length) },
{ default: () => `+${images.length - visible.length}` },
)
: null,
]);
}
function previewImages(images: any[], index: number) {
void openMediaViewer(
images.map((item) => ({ url: item.url || item.key, name: item.name || item.key, type: item.type || 'image' })),
index,
);
}
async function downloadTemplate() {
await downloadFile('/outbound/import/template', 'outbound-import-template.zip');
}
async function uploadPreview({ file, onFinish, onError }: UploadCustomRequestOptions) {
previewing.value = true;
result.value = null;
progressPercent.value = 5;
try {
const fd = new FormData();
fd.append('file', file.file as File);
const res: any = await post('/outbound/import', fd);
const jobId = res.jobId;
let job: any = null;
for (let i = 0; i < 40; i++) {
await new Promise((r) => setTimeout(r, 700));
job = await jobApi.get(jobId);
if (['SUCCESS', 'FAILED', 'PARTIAL'].includes(job.status)) break;
}
result.value = job;
message.success(t('common.msg.operationSuccess'));
const res: any = await outboundApi.preview(fd);
sessionId.value = res.sessionId;
await pollJob(res.jobId, 'preview');
await refreshSession();
resetPage();
await loadRows();
message.success('预览解析完成');
onFinish();
} catch (e) {
message.error((e as Error)?.message || 'import failed');
message.error((e as Error)?.message || '上传失败');
onError();
} finally {
importing.value = false;
previewing.value = false;
progressPercent.value = 0;
}
}
async function commitImport() {
if (!sessionId.value) return;
committing.value = true;
result.value = null;
progressPercent.value = 5;
try {
const res: any = await outboundApi.commit(sessionId.value);
const job = await pollJob(res.jobId, 'commit');
result.value = job;
await refreshSession();
await loadRows();
message.success('导入完成');
} finally {
committing.value = false;
progressPercent.value = 0;
}
}
async function pollJob(jobId: string, phase: 'preview' | 'commit') {
let job: any = null;
for (let i = 0; i < 120; i++) {
await new Promise((resolve) => setTimeout(resolve, 700));
job = await jobApi.get(jobId);
const total = Number(job.total || session.value?.total || 0);
const done = Number(job.successCount || 0) + Number(job.failCount || 0);
progressPercent.value = total ? Math.min(99, Math.round((done / total) * 100)) : Math.min(95, 10 + i);
if (['SUCCESS', 'FAILED', 'PARTIAL'].includes(job.status)) {
progressPercent.value = 100;
break;
}
if (phase === 'preview' && sessionId.value) {
await refreshSession(false);
}
}
return job;
}
async function refreshSession(showLoading = true) {
if (!sessionId.value) return;
if (showLoading) loading.value = true;
try {
session.value = await outboundApi.session(sessionId.value);
} finally {
if (showLoading) loading.value = false;
}
}
async function loadRows() {
if (!sessionId.value) {
rows.value = [];
setPageResult({ total: 0, page: page.value, pageSize: pageSize.value });
return;
}
loading.value = true;
try {
const res: any = await outboundApi.rows(sessionId.value, { page: page.value, pageSize: pageSize.value });
rows.value = res.list;
setPageResult(res);
} finally {
loading.value = false;
}
}
</script>
<style scoped>
.outbound-page {
min-width: 0;
}
.outbound-card {
flex: 1 1 auto;
min-height: 0;
display: flex;
flex-direction: column;
}
.outbound-card :deep(.n-card__content) {
flex: 1 1 auto;
min-height: 0;
display: flex;
flex-direction: column;
}
.section-alert {
margin-bottom: 12px;
}
.summary-card {
margin-bottom: 12px;
flex: 0 0 auto;
}
.success-text {
color: #138a72;
}
.danger-text {
color: #c0392b;
}
.progress {
width: 180px;
}
.session-error {
display: block;
margin-top: 8px;
}
.preview-images {
display: inline-flex;
align-items: center;
justify-content: center;
gap: 6px;
}
.preview-images :deep(img) {
border-radius: 4px;
cursor: zoom-in;
}
</style>