Files
Artveris/apps/admin/src/views/Outbound.vue
T

319 lines
10 KiB
Vue

<template>
<div class="page table-page outbound-page">
<div class="page-toolbar outbound-toolbar">
<div class="section-title">{{ t('pages.outbound.batchImportTitle') }}</div>
<div class="spacer" />
<n-button :title="t('pages.outbound.downloadZipTemplate')" @click="downloadTemplate">
<template #icon><n-icon :component="DownloadOutline" /></template>
{{ t('pages.outbound.downloadZipTemplate') }}
</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>
{{ t('pages.outbound.uploadZipPreview') }}
</n-button>
</n-upload>
</div>
<n-card v-if="session" embedded class="summary-card" :bordered="false">
<n-space align="center" size="large" wrap>
<n-statistic :label="t('pages.outbound.totalRows')" :value="session.total || 0" />
<n-statistic :label="t('pages.outbound.validRows')">
<span class="success-text">{{ session.validCount || 0 }}</span>
</n-statistic>
<n-statistic :label="t('pages.outbound.invalidRows')">
<span class="danger-text">{{ session.invalidCount || 0 }}</span>
</n-statistic>
<n-tag :type="statusType(session.status)">{{ sessionStatusLabel(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"
>
{{ t('pages.outbound.confirmImport') }}
</n-button>
<n-button v-if="result?.resultFileUrl" tag="a" :href="result.resultFileUrl" target="_blank">
<template #icon><n-icon :component="DownloadOutline" /></template>
{{ t('pages.outbound.downloadResult') }}
</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="1480"
:row-key="(r) => r.id"
@update:sorter="onSorterChange"
/>
<div class="table-pagination">
<n-pagination v-bind="pagination" :disabled="loading" />
</div>
</div>
</template>
<script setup lang="ts">
import { computed, h, ref } from 'vue';
import { useI18n } from 'vue-i18n';
import type { UploadCustomRequestOptions } from 'naive-ui';
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 { downloadFile } from '@/utils/download';
import { fmt, statusType } from '@/utils/format';
import { useTablePage } from '@/composables/useTablePage';
import { openMediaViewer } from '@/composables/openMediaViewer';
import { useRemoteSort } from '@/utils/tableSort';
const { t } = useI18n();
const auth = useAuthStore();
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);
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 { sortProps, onSorterChange, sortParams } = useRemoteSort(() => {
resetPage();
return loadRows();
});
const columns = computed<DataTableColumns<any>>(() => [
{ title: t('pages.outbound.rowNo'), key: 'rowNo', width: 80, align: 'center' },
{
title: t('common.label.status'),
key: 'status',
width: 110,
align: 'center',
render: (row) => h(NTag, { size: 'small', type: row.status === 'VALID' || row.status === 'IMPORTED' ? 'success' : 'error' }, { default: () => statusLabel(row.status) }),
},
{ title: t('pages.traceCode.shortCode'), key: 'shortCode', minWidth: 150, align: 'center', ellipsis: { tooltip: true } },
{ title: t('pages.traceCode.batchNo'), key: 'batchNo', minWidth: 180, align: 'center', ellipsis: { tooltip: true } },
{ title: t('pages.outbound.outTime'), key: 'outTime', width: 170, align: 'center', render: (row) => fmt(row.outTime || row.preview?.outTime), ...sortProps('outTime') },
{
title: t('pages.outbound.outImage'),
key: 'images',
width: 240,
align: 'center',
render: (row) => renderImages(row),
},
{ title: t('pages.blockchain.txHash'), key: 'txHash', minWidth: 260, ellipsis: { tooltip: true } },
{ title: t('common.label.remark'), key: 'remark', minWidth: 220, ellipsis: { tooltip: true } },
{
title: t('pages.outbound.errorReason'),
key: 'reason',
minWidth: 300,
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) : [];
}
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 outboundApi.preview(fd);
sessionId.value = res.sessionId;
await pollJob(res.jobId, 'preview');
await refreshSession();
resetPage();
await loadRows();
message.success(t('pages.outbound.previewReady'));
onFinish();
} catch (e) {
message.error(t('pages.outbound.uploadFailed'));
onError();
} finally {
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(t('pages.outbound.importDone'));
} 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, ...sortParams() });
rows.value = res.list;
setPageResult(res);
} finally {
loading.value = false;
}
}
function statusLabel(value?: string) {
return value ? t(`pages.outbound.rowStatus.${value}`) : '-';
}
function sessionStatusLabel(value?: string) {
return value ? t(`pages.outbound.sessionStatus.${value}`) : '-';
}
</script>
<style scoped>
.outbound-page {
min-width: 0;
}
.outbound-toolbar {
margin-bottom: 12px;
}
.outbound-toolbar :deep(.n-upload) {
width: auto;
}
.section-title {
font-size: 16px;
font-weight: 600;
white-space: nowrap;
}
.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>