244 lines
7.7 KiB
Vue
244 lines
7.7 KiB
Vue
<!--
|
||
features/chat/components/messages/TransferMessageView.vue
|
||
用途:在途调拨消息卡片的自驱动挂载视图——按 payload 形态渲染卡片,并把卡片按钮
|
||
(直接生成调拨单 / 下载 / 重试 / 关闭)经 useChatTransfer 真实调用 API。
|
||
依赖:Transfer*Card、useChatTransfer、i18n chat.transfer.*。
|
||
逻辑:payload 作为初始状态(ChatLog 回放);执行后切换为结果卡;下载走 requestBlob;
|
||
不展示执行审计入口(决策 19)。
|
||
-->
|
||
<script setup lang="ts">
|
||
import { computed, onMounted, ref, watch } from 'vue';
|
||
import TransferPrecheckCard from './TransferPrecheckCard.vue';
|
||
import TransferFileProgressCard from './TransferFileProgressCard.vue';
|
||
import TransferProgressCard from './TransferProgressCard.vue';
|
||
import TransferResultCard from './TransferResultCard.vue';
|
||
import TransferBacklogSummaryCard from './TransferBacklogSummaryCard.vue';
|
||
import TransferManualCloseDialog from './TransferManualCloseDialog.vue';
|
||
import TransferSourceSelectCard from './TransferSourceSelectCard.vue';
|
||
import { useChatTransfer } from '../../composables/useChatTransfer';
|
||
import { useMessage } from '@/shared/composables/useMessage';
|
||
import type {
|
||
TransferBacklogItem,
|
||
TransferPrecheckItem,
|
||
TransferPrecheckSummary,
|
||
TransferRunResult,
|
||
TransferSourceProgress,
|
||
} from '../../types/transfer.types';
|
||
|
||
const props = defineProps<{
|
||
payload?: Record<string, unknown> | null;
|
||
}>();
|
||
|
||
const { error: notifyError } = useMessage();
|
||
|
||
/** 统一错误提示:BusinessError 带本地化 message,其余回退到 String(e)。 */
|
||
function reportError(e: unknown) {
|
||
notifyError(e instanceof Error ? e.message : String(e));
|
||
}
|
||
|
||
const kind = computed(() => String(props.payload?.kind ?? 'precheck'));
|
||
const mode = ref(kind.value);
|
||
const summaryRef = ref<TransferPrecheckSummary | null>((props.payload?.summary as TransferPrecheckSummary) ?? null);
|
||
const items = ref<TransferPrecheckItem[]>((props.payload?.items as TransferPrecheckItem[]) ?? []);
|
||
const runNo = ref(String(props.payload?.run_no ?? ''));
|
||
const status = ref(String(props.payload?.status ?? 'executing'));
|
||
const run = ref<TransferRunResult | null>((props.payload?.run as TransferRunResult) ?? null);
|
||
const busy = ref(false);
|
||
const closeTarget = ref<TransferBacklogItem | null>(null);
|
||
/** 正在重试收料 RPA 的来源编码集合(按来源独立展示重试中)。 */
|
||
const retryingSource = ref<Set<string>>(new Set());
|
||
|
||
const transfer = useChatTransfer();
|
||
|
||
/** 待处理异常视图:进入时拉取列表与汇总 */
|
||
onMounted(() => {
|
||
if (kind.value === 'backlog') {
|
||
void transfer.loadBacklog();
|
||
}
|
||
if (kind.value === 'source-select') {
|
||
void transfer.loadSources();
|
||
}
|
||
});
|
||
|
||
/** payload 更新时重置本地状态(历史回放/刷新);初始化组合式函数上下文 */
|
||
function applyPayload(payload?: Record<string, unknown> | null) {
|
||
if (!payload) return;
|
||
mode.value = String(payload.kind ?? 'precheck');
|
||
summaryRef.value = (payload.summary as TransferPrecheckSummary) ?? null;
|
||
items.value = (payload.items as TransferPrecheckItem[]) ?? [];
|
||
runNo.value = String(payload.run_no ?? '');
|
||
status.value = String(payload.status ?? 'executing');
|
||
run.value = (payload.run as TransferRunResult) ?? null;
|
||
const summaryValue = (payload.summary as TransferPrecheckSummary) ?? null;
|
||
if (summaryValue) {
|
||
transfer.precheckId.value = summaryValue.precheck_id;
|
||
transfer.selected.value = new Set(
|
||
(payload.items as TransferPrecheckItem[] ?? [])
|
||
.filter((item) => item.result === 'executable')
|
||
.map((item) => item.invoice_no),
|
||
);
|
||
}
|
||
if (runNo.value) {
|
||
transfer.runNo.value = runNo.value;
|
||
}
|
||
}
|
||
|
||
applyPayload(props.payload);
|
||
watch(() => props.payload, applyPayload);
|
||
|
||
/** 直接生成调拨单(决策 4/5:无独立确认页) */
|
||
async function onExecute(invoiceNos: string[]) {
|
||
busy.value = true;
|
||
try {
|
||
transfer.selected.value = new Set(invoiceNos);
|
||
const outcome = await transfer.execute();
|
||
if (outcome) {
|
||
mode.value = 'result';
|
||
runNo.value = outcome.run_no;
|
||
status.value = outcome.status;
|
||
run.value = transfer.run.value;
|
||
}
|
||
} catch (e) {
|
||
reportError(e);
|
||
} finally {
|
||
busy.value = false;
|
||
}
|
||
}
|
||
|
||
async function onDownloadReceipt() {
|
||
try {
|
||
await transfer.downloadReceiptFiles();
|
||
} catch (e) {
|
||
reportError(e);
|
||
}
|
||
}
|
||
|
||
async function onDownloadErp() {
|
||
try {
|
||
await transfer.downloadErpInventory();
|
||
} catch (e) {
|
||
reportError(e);
|
||
}
|
||
}
|
||
|
||
async function onDownloadExceptions() {
|
||
try {
|
||
await transfer.downloadExceptions();
|
||
} catch (e) {
|
||
reportError(e);
|
||
}
|
||
}
|
||
|
||
async function onBacklogRetry(itemId: number) {
|
||
busy.value = true;
|
||
try {
|
||
await transfer.retry(itemId);
|
||
await transfer.loadBacklog();
|
||
} catch (e) {
|
||
reportError(e);
|
||
} finally {
|
||
busy.value = false;
|
||
}
|
||
}
|
||
|
||
function onBacklogClose(item: TransferBacklogItem) {
|
||
closeTarget.value = item;
|
||
}
|
||
|
||
async function onCloseConfirm(reason: string) {
|
||
if (!closeTarget.value) return;
|
||
busy.value = true;
|
||
try {
|
||
await transfer.manualClose(closeTarget.value.item_id, reason);
|
||
closeTarget.value = null;
|
||
await transfer.loadBacklog();
|
||
} catch (e) {
|
||
reportError(e);
|
||
} finally {
|
||
busy.value = false;
|
||
}
|
||
}
|
||
|
||
/** 来源实例选择确认 → 创建预检并切换预检卡(决策 4:两步第一步) */
|
||
async function onSourceConfirm(sourceCodes: string[]) {
|
||
busy.value = true;
|
||
try {
|
||
await transfer.createPrecheck(sourceCodes);
|
||
mode.value = 'precheck';
|
||
summaryRef.value = transfer.precheck.value;
|
||
items.value = transfer.items.value;
|
||
run.value = null;
|
||
} catch (e) {
|
||
reportError(e);
|
||
} finally {
|
||
busy.value = false;
|
||
}
|
||
}
|
||
|
||
/** 对来源(单个或批量)重新触发收料 RPA 抓取,重试期间标记该来源(busy 独立于 confirm busy)。 */
|
||
async function onSourceRetry(sourceCodes: string[]) {
|
||
const codes = Array.isArray(sourceCodes) ? sourceCodes : [sourceCodes];
|
||
retryingSource.value = new Set([...retryingSource.value, ...codes]);
|
||
try {
|
||
// 逐来源触发,避免一次并发 EPR/RPA 竞态;失败中断剩余。
|
||
for (const code of codes) {
|
||
await transfer.retrySourceRpa(code);
|
||
}
|
||
} catch (e) {
|
||
reportError(e);
|
||
} finally {
|
||
const next = new Set(retryingSource.value);
|
||
for (const code of codes) next.delete(code);
|
||
retryingSource.value = next;
|
||
}
|
||
}
|
||
</script>
|
||
|
||
<template>
|
||
<div>
|
||
<TransferFileProgressCard
|
||
v-if="mode === 'file-progress'"
|
||
:sources="(props.payload?.sources as TransferSourceProgress[]) ?? []"
|
||
/>
|
||
<TransferSourceSelectCard
|
||
v-else-if="mode === 'source-select'"
|
||
:sources="transfer.sourceOptions.value"
|
||
:busy="busy"
|
||
:retrying="retryingSource"
|
||
@confirm="onSourceConfirm"
|
||
@retry="onSourceRetry"
|
||
/>
|
||
<TransferPrecheckCard
|
||
v-else-if="mode === 'precheck' && summaryRef"
|
||
:summary="summaryRef"
|
||
:items="items"
|
||
:selected="transfer.selected.value"
|
||
:busy="busy"
|
||
@toggle="(invoiceNo, checked) => transfer.toggleSelect(invoiceNo, checked)"
|
||
@execute="onExecute"
|
||
/>
|
||
<TransferProgressCard v-else-if="mode === 'progress'" :run-no="runNo" :status="status" :busy="busy" />
|
||
<TransferResultCard
|
||
v-else-if="mode === 'result' && run"
|
||
:run="run"
|
||
@download-receipt="onDownloadReceipt"
|
||
@download-erp="onDownloadErp"
|
||
@download-exceptions="onDownloadExceptions"
|
||
/>
|
||
<TransferBacklogSummaryCard
|
||
v-else-if="mode === 'backlog'"
|
||
:items="transfer.backlog.value"
|
||
@retry="onBacklogRetry"
|
||
@close="onBacklogClose"
|
||
@refresh="() => transfer.loadBacklog()"
|
||
/>
|
||
<TransferManualCloseDialog
|
||
v-if="closeTarget"
|
||
:open="Boolean(closeTarget)"
|
||
:invoice-no="closeTarget.invoice_no"
|
||
@confirm="onCloseConfirm"
|
||
@cancel="closeTarget = null"
|
||
/>
|
||
</div>
|
||
</template>
|