feat: 拆分上链与出库管理界面
This commit is contained in:
@@ -0,0 +1,264 @@
|
||||
<template>
|
||||
<n-modal v-model:show="visible" preset="card" :title="title" class="batch-job-modal">
|
||||
<n-space v-if="job" align="center" wrap class="job-summary">
|
||||
<n-statistic :label="t('pages.operations.total')" :value="jobTotal" />
|
||||
<n-statistic :label="t('pages.operations.success')" :value="successCount" />
|
||||
<n-statistic :label="t('pages.operations.failed')" :value="failCount" />
|
||||
<n-statistic :label="t('pages.operations.skipped')" :value="skippedCount" />
|
||||
<n-tag :type="statusType(job.status)">{{ statusText(job.status) }}</n-tag>
|
||||
</n-space>
|
||||
|
||||
<n-progress
|
||||
v-if="job && !terminal"
|
||||
type="line"
|
||||
:percentage="progress"
|
||||
:indicator-placement="'inside'"
|
||||
processing
|
||||
class="job-progress"
|
||||
/>
|
||||
|
||||
<div class="result-toolbar">
|
||||
<n-select
|
||||
v-model:value="itemStatus"
|
||||
:options="itemStatusOptions"
|
||||
clearable
|
||||
:placeholder="t('pages.operations.allResults')"
|
||||
class="status-select"
|
||||
@update:value="reloadItems"
|
||||
/>
|
||||
<div class="spacer" />
|
||||
<n-button quaternary circle :loading="loading" :title="t('common.action.refresh')" @click="refresh">
|
||||
<template #icon><n-icon :component="RefreshOutline" /></template>
|
||||
</n-button>
|
||||
</div>
|
||||
|
||||
<n-data-table
|
||||
:columns="columns"
|
||||
:data="items"
|
||||
:loading="loading"
|
||||
:pagination="false"
|
||||
:row-key="(row) => row.id || `${row.shortCode}-${row.status}`"
|
||||
:scroll-x="760"
|
||||
size="small"
|
||||
max-height="360"
|
||||
virtual-scroll
|
||||
/>
|
||||
<div class="modal-pagination">
|
||||
<n-pagination
|
||||
v-model:page="page"
|
||||
:page-size="pageSize"
|
||||
:item-count="itemTotal"
|
||||
:disabled="loading"
|
||||
@update:page="loadItems"
|
||||
/>
|
||||
</div>
|
||||
|
||||
<template #footer>
|
||||
<div class="footer-actions">
|
||||
<n-button @click="visible = false">{{ t('common.action.close') }}</n-button>
|
||||
</div>
|
||||
</template>
|
||||
</n-modal>
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import { computed, h, onUnmounted, ref, watch } from 'vue';
|
||||
import { useI18n } from 'vue-i18n';
|
||||
import { NIcon, NTag, NText, type DataTableColumns } from 'naive-ui';
|
||||
import { RefreshOutline } from '@vicons/ionicons5';
|
||||
import { jobApi } from '@/api';
|
||||
import { fmt, statusType } from '@/utils/format';
|
||||
import { localizeOperationReason } from '@/utils/operationReason';
|
||||
|
||||
const props = defineProps<{
|
||||
show: boolean;
|
||||
jobId: string;
|
||||
title: string;
|
||||
}>();
|
||||
const emit = defineEmits<{
|
||||
(event: 'update:show', value: boolean): void;
|
||||
(event: 'completed'): void;
|
||||
}>();
|
||||
|
||||
const { t } = useI18n();
|
||||
const visible = computed({
|
||||
get: () => props.show,
|
||||
set: (value) => emit('update:show', value),
|
||||
});
|
||||
const job = ref<any>(null);
|
||||
const items = ref<any[]>([]);
|
||||
const loading = ref(false);
|
||||
const page = ref(1);
|
||||
const pageSize = 20;
|
||||
const itemTotal = ref(0);
|
||||
const itemStatus = ref<string | null>(null);
|
||||
let timer: ReturnType<typeof setTimeout> | undefined;
|
||||
let completedEmitted = false;
|
||||
|
||||
const terminalStatuses = new Set(['SUCCESS', 'PARTIAL', 'FAILED', 'CANCELLED']);
|
||||
const terminal = computed(() => terminalStatuses.has(String(job.value?.status || '')));
|
||||
const jobTotal = computed(() => Number(job.value?.total || itemTotal.value || 0));
|
||||
const successCount = computed(() => Number(job.value?.successCount || 0));
|
||||
const failCount = computed(() => Number(job.value?.failCount || 0));
|
||||
const skippedCount = computed(() => {
|
||||
if (job.value?.skippedCount !== undefined) return Number(job.value.skippedCount || 0);
|
||||
return terminal.value ? Math.max(0, jobTotal.value - successCount.value - failCount.value) : 0;
|
||||
});
|
||||
const progress = computed(() => {
|
||||
const total = jobTotal.value;
|
||||
if (!total) return terminal.value ? 100 : 0;
|
||||
const done = successCount.value + failCount.value + skippedCount.value;
|
||||
return Math.min(100, Math.round((done / total) * 100));
|
||||
});
|
||||
|
||||
const itemStatusOptions = computed(() =>
|
||||
['PENDING', 'PROCESSING', 'SUCCESS', 'FAILED', 'SKIPPED', 'UNKNOWN'].map((value) => ({
|
||||
value,
|
||||
label: t(`pages.operations.itemStatus.${value}`),
|
||||
})),
|
||||
);
|
||||
|
||||
const columns = computed<DataTableColumns<any>>(() => [
|
||||
{
|
||||
title: t('pages.traceCode.shortCode'),
|
||||
key: 'shortCode',
|
||||
width: 150,
|
||||
align: 'center',
|
||||
render: (row) => row.shortCode || row.payload?.shortCode || '-',
|
||||
},
|
||||
{
|
||||
title: t('common.label.status'),
|
||||
key: 'status',
|
||||
width: 120,
|
||||
align: 'center',
|
||||
render: (row) =>
|
||||
h(NTag, { size: 'small', type: statusType(row.status) }, {
|
||||
default: () => t(`pages.operations.itemStatus.${row.status}`),
|
||||
}),
|
||||
},
|
||||
{
|
||||
title: t('pages.operations.processedAt'),
|
||||
key: 'updatedAt',
|
||||
width: 170,
|
||||
align: 'center',
|
||||
render: (row) => fmt(row.processedAt || row.updatedAt),
|
||||
},
|
||||
{
|
||||
title: t('pages.operations.resultReason'),
|
||||
key: 'reason',
|
||||
minWidth: 300,
|
||||
ellipsis: { tooltip: true },
|
||||
render: (row) => {
|
||||
const reason = localizeOperationReason(row.reason || row.result?.reason || row.error);
|
||||
return reason || h(NText, { depth: 3 }, { default: () => '-' });
|
||||
},
|
||||
},
|
||||
]);
|
||||
|
||||
function statusText(status?: string) {
|
||||
if (!status) return '-';
|
||||
const key = `pages.operations.jobStatus.${status}`;
|
||||
const translated = t(key);
|
||||
return translated === key ? status : translated;
|
||||
}
|
||||
|
||||
function stopPolling() {
|
||||
if (timer) clearTimeout(timer);
|
||||
timer = undefined;
|
||||
}
|
||||
|
||||
function schedulePoll() {
|
||||
stopPolling();
|
||||
if (!visible.value || terminal.value) return;
|
||||
timer = setTimeout(() => void refresh(), 1200);
|
||||
}
|
||||
|
||||
async function loadItems() {
|
||||
if (!props.jobId) return;
|
||||
try {
|
||||
const result: any = await jobApi.items(
|
||||
props.jobId,
|
||||
{ page: page.value, pageSize, status: itemStatus.value || undefined },
|
||||
true,
|
||||
);
|
||||
items.value = result.items || result.list || [];
|
||||
itemTotal.value = Number(result.total || 0);
|
||||
} catch {
|
||||
// 任务刚创建时逐项结果可能尚未初始化,下一轮轮询会自动补齐。
|
||||
}
|
||||
}
|
||||
|
||||
function reloadItems() {
|
||||
page.value = 1;
|
||||
void loadItems();
|
||||
}
|
||||
|
||||
async function refresh() {
|
||||
if (!props.jobId || loading.value) return;
|
||||
loading.value = true;
|
||||
try {
|
||||
const nextJob = await jobApi.get(props.jobId, true);
|
||||
job.value = nextJob;
|
||||
await loadItems();
|
||||
if (terminal.value && !completedEmitted) {
|
||||
completedEmitted = true;
|
||||
emit('completed');
|
||||
}
|
||||
} catch {
|
||||
// 临时网络异常不终止任务跟踪。
|
||||
} finally {
|
||||
loading.value = false;
|
||||
schedulePoll();
|
||||
}
|
||||
}
|
||||
|
||||
watch(
|
||||
() => [props.show, props.jobId] as const,
|
||||
([show, id]) => {
|
||||
stopPolling();
|
||||
if (!show || !id) return;
|
||||
completedEmitted = false;
|
||||
job.value = null;
|
||||
items.value = [];
|
||||
itemTotal.value = 0;
|
||||
page.value = 1;
|
||||
itemStatus.value = null;
|
||||
void refresh();
|
||||
},
|
||||
{ immediate: true },
|
||||
);
|
||||
|
||||
onUnmounted(stopPolling);
|
||||
</script>
|
||||
|
||||
<style scoped>
|
||||
.batch-job-modal {
|
||||
width: min(900px, calc(100vw - 32px));
|
||||
}
|
||||
.job-summary {
|
||||
margin-bottom: 12px;
|
||||
}
|
||||
.job-progress {
|
||||
margin-bottom: 12px;
|
||||
}
|
||||
.result-toolbar {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 10px;
|
||||
margin-bottom: 10px;
|
||||
}
|
||||
.status-select {
|
||||
width: 180px;
|
||||
}
|
||||
.spacer {
|
||||
flex: 1 1 auto;
|
||||
}
|
||||
.modal-pagination,
|
||||
.footer-actions {
|
||||
display: flex;
|
||||
justify-content: flex-end;
|
||||
}
|
||||
.modal-pagination {
|
||||
padding-top: 12px;
|
||||
}
|
||||
</style>
|
||||
Reference in New Issue
Block a user