327 lines
12 KiB
Vue
327 lines
12 KiB
Vue
<!--
|
||
features/chat/components/shipping-review/ReviewAuditTrailDrawer.vue
|
||
用途:复核 Case 业务审计时间线按需抽屉(含变更前后快照与字段差异)。
|
||
依赖:vue-i18n、listWorkspaceShippingOrderReviewAuditTimeline。
|
||
逻辑:open 时拉取第一页;展开优先展示业务可读差异(字段名/前后值/原因);
|
||
原始 JSON 折叠为技术详情;敏感事件显示脱敏提示。
|
||
-->
|
||
<script setup lang="ts">
|
||
import { ref, watch } from 'vue';
|
||
import { useI18n } from 'vue-i18n';
|
||
import { BusinessError, resolveLocalizedErrorMessage } from '@/shared/api/client';
|
||
import { useMessage } from '@/shared/composables/useMessage';
|
||
import { listWorkspaceShippingOrderReviewAuditTimeline } from '@/features/chat/api/shipping.api';
|
||
import type { ShippingOrderReviewAuditTimelineItem } from '@/features/chat/types/shipping.api.types';
|
||
|
||
type AuditDiffRow = {
|
||
key?: string;
|
||
path?: string;
|
||
field?: string;
|
||
label?: string;
|
||
change?: string;
|
||
before?: unknown;
|
||
after?: unknown;
|
||
};
|
||
|
||
const props = defineProps<{
|
||
open: boolean;
|
||
caseNo: string;
|
||
}>();
|
||
|
||
const emit = defineEmits<{
|
||
close: [];
|
||
}>();
|
||
|
||
const { t, te } = useI18n();
|
||
const msg = useMessage();
|
||
const items = ref<ShippingOrderReviewAuditTimelineItem[]>([]);
|
||
const loading = ref(false);
|
||
const page = ref(1);
|
||
const total = ref(0);
|
||
const expandedId = ref<number | null>(null);
|
||
const pageSize = 50;
|
||
|
||
/**
|
||
* 格式化审计时间。
|
||
*/
|
||
function formatTime(value: string | null | undefined): string {
|
||
if (!value) return '—';
|
||
const date = new Date(value);
|
||
return Number.isNaN(date.getTime()) ? value : date.toLocaleString();
|
||
}
|
||
|
||
/**
|
||
* 事件类型本地化。
|
||
*/
|
||
function eventLabel(item: ShippingOrderReviewAuditTimelineItem): string {
|
||
const eventType = String(item.event_type || item.action || '').trim();
|
||
const key = `chat.shippingOrderReview.auditTrail.events.${eventType}`;
|
||
if (eventType && te(key)) return t(key);
|
||
return eventType || t('chat.shippingOrderReview.auditTrail.unknownEvent');
|
||
}
|
||
|
||
/**
|
||
* 将审计标量格式化为业务可读短文本。
|
||
*/
|
||
function formatScalar(value: unknown): string {
|
||
if (value == null || value === '') return '—';
|
||
if (typeof value === 'boolean') {
|
||
return value
|
||
? t('chat.shippingOrderReview.auditTrail.boolTrue')
|
||
: t('chat.shippingOrderReview.auditTrail.boolFalse');
|
||
}
|
||
if (typeof value === 'object') {
|
||
try {
|
||
return JSON.stringify(value);
|
||
} catch {
|
||
return String(value);
|
||
}
|
||
}
|
||
return String(value);
|
||
}
|
||
|
||
/**
|
||
* 快照/差异技术详情(原始 JSON)。
|
||
*/
|
||
function formatJson(value: unknown): string {
|
||
if (value == null) return '—';
|
||
try {
|
||
return JSON.stringify(value, null, 2);
|
||
} catch {
|
||
return String(value);
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 复制技术详情 JSON 到剪贴板。
|
||
*/
|
||
async function copyJson(value: unknown) {
|
||
const text = formatJson(value);
|
||
try {
|
||
await navigator.clipboard.writeText(text);
|
||
msg.success(t('chat.shippingOrderReview.auditTrail.copyDone'));
|
||
} catch {
|
||
msg.warning(t('chat.shippingOrderReview.auditTrail.copyFailed'));
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 差异字段业务名称(优先 label/field,其次 path)。
|
||
*/
|
||
function diffFieldLabel(row: AuditDiffRow): string {
|
||
const label = String(row.label || row.field || row.key || row.path || '').trim();
|
||
return label || t('chat.shippingOrderReview.auditTrail.unknownField');
|
||
}
|
||
|
||
/**
|
||
* 规范化差异列表,供业务表格渲染。
|
||
*/
|
||
function normalizeDiffRows(diff: unknown): AuditDiffRow[] {
|
||
if (!Array.isArray(diff)) return [];
|
||
return diff.filter((row): row is AuditDiffRow => Boolean(row) && typeof row === 'object');
|
||
}
|
||
|
||
/**
|
||
* 切换条目展开。
|
||
*/
|
||
function toggleExpand(id: number) {
|
||
expandedId.value = expandedId.value === id ? null : id;
|
||
}
|
||
|
||
/**
|
||
* 拉取指定页审计条目并合并。
|
||
*/
|
||
async function loadPage(nextPage: number, append = false) {
|
||
if (!props.caseNo || loading.value) return;
|
||
loading.value = true;
|
||
try {
|
||
const result = await listWorkspaceShippingOrderReviewAuditTimeline(props.caseNo, {
|
||
page: nextPage,
|
||
page_size: pageSize,
|
||
});
|
||
total.value = result.total;
|
||
page.value = result.page;
|
||
items.value = append ? [...items.value, ...result.items] : result.items;
|
||
} catch (error: unknown) {
|
||
const message = error instanceof BusinessError
|
||
? resolveLocalizedErrorMessage(error.code, error.message)
|
||
: (error instanceof Error ? error.message : t('chat.shippingOrderReview.auditTrail.loadFailed'));
|
||
msg.warning(message);
|
||
} finally {
|
||
loading.value = false;
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 打开抽屉时重置并加载第一页。
|
||
*/
|
||
watch(
|
||
() => [props.open, props.caseNo] as const,
|
||
([isOpen, caseNo]) => {
|
||
if (!isOpen || !caseNo) return;
|
||
items.value = [];
|
||
page.value = 1;
|
||
total.value = 0;
|
||
expandedId.value = null;
|
||
void loadPage(1, false);
|
||
},
|
||
);
|
||
|
||
/**
|
||
* 加载下一页。
|
||
*/
|
||
function loadMore() {
|
||
if (items.value.length >= total.value) return;
|
||
void loadPage(page.value + 1, true);
|
||
}
|
||
</script>
|
||
|
||
<template>
|
||
<div
|
||
v-if="open"
|
||
class="fixed inset-0 z-40 flex justify-end bg-slate-900/30"
|
||
data-testid="review-audit-trail-drawer"
|
||
@click.self="emit('close')"
|
||
>
|
||
<aside class="flex h-full w-full max-w-xl flex-col bg-surface shadow-xl">
|
||
<header class="flex items-center justify-between gap-2 border-b border-default px-4 py-3">
|
||
<div>
|
||
<h2 class="text-sm font-semibold">{{ t('chat.shippingOrderReview.auditTrail.title') }}</h2>
|
||
<p class="text-[11px] text-muted">{{ t('chat.shippingOrderReview.auditTrail.subtitle') }}</p>
|
||
</div>
|
||
<button
|
||
type="button"
|
||
class="rounded border border-default px-2 py-1 text-xs hover-surface-soft"
|
||
data-testid="review-audit-trail-close"
|
||
@click="emit('close')"
|
||
>
|
||
{{ t('chat.shippingOrderReview.workbench.close') }}
|
||
</button>
|
||
</header>
|
||
<div class="min-h-0 flex-1 overflow-y-auto p-4">
|
||
<p v-if="loading && !items.length" class="text-sm text-muted">
|
||
{{ t('chat.shippingOrderReview.auditTrail.loading') }}
|
||
</p>
|
||
<p v-else-if="!items.length" class="text-sm text-muted">
|
||
{{ t('chat.shippingOrderReview.auditTrail.empty') }}
|
||
</p>
|
||
<ol v-else class="space-y-3">
|
||
<li
|
||
v-for="item in items"
|
||
:key="item.id"
|
||
class="rounded-lg border border-default bg-surface-soft px-3 py-2 text-xs dark:bg-slate-950/40"
|
||
:data-testid="`review-audit-item-${item.id}`"
|
||
>
|
||
<button
|
||
type="button"
|
||
class="flex w-full flex-wrap items-center justify-between gap-2 text-left"
|
||
@click="toggleExpand(item.id)"
|
||
>
|
||
<span class="font-medium text-primary">{{ eventLabel(item) }}</span>
|
||
<time class="text-[10px] text-muted">{{ formatTime(item.created_at) }}</time>
|
||
</button>
|
||
<div class="mt-1 space-y-0.5 text-muted">
|
||
<p v-if="item.actor_display || item.actor_type">
|
||
{{ t('chat.shippingOrderReview.auditTrail.actor', {
|
||
actor: item.actor_display || item.actor_type || '—',
|
||
}) }}
|
||
</p>
|
||
<p v-if="item.task_no">
|
||
{{ t('chat.shippingOrderReview.auditTrail.taskNo', { taskNo: item.task_no }) }}
|
||
</p>
|
||
<p v-if="item.result">
|
||
{{ t('chat.shippingOrderReview.auditTrail.result', { result: item.result }) }}
|
||
</p>
|
||
<p v-if="item.error_code" class="text-rose-700 dark:text-rose-300">
|
||
{{ item.error_code }}
|
||
</p>
|
||
<p v-if="item.reason" class="text-secondary">{{ item.reason }}</p>
|
||
</div>
|
||
<div
|
||
v-if="expandedId === item.id"
|
||
class="mt-2 space-y-2 border-t border-default pt-2"
|
||
data-testid="review-audit-item-details"
|
||
>
|
||
<p v-if="item.sensitive_redacted" class="text-amber-800 dark:text-amber-200">
|
||
{{ t('chat.shippingOrderReview.auditTrail.sensitiveRedacted') }}
|
||
</p>
|
||
<template v-else>
|
||
<div v-if="normalizeDiffRows(item.diff).length" data-testid="review-audit-business-diff">
|
||
<p class="mb-1 font-medium text-secondary">
|
||
{{ t('chat.shippingOrderReview.auditTrail.diff') }}
|
||
</p>
|
||
<ul class="space-y-1.5">
|
||
<li
|
||
v-for="(row, idx) in normalizeDiffRows(item.diff)"
|
||
:key="idx"
|
||
class="rounded bg-surface px-2 py-1.5 text-[11px]"
|
||
>
|
||
<p class="font-medium text-primary">{{ diffFieldLabel(row) }}</p>
|
||
<p class="mt-0.5 text-muted">
|
||
{{ t('chat.shippingOrderReview.auditTrail.beforeValue') }}:
|
||
<span class="text-secondary">{{ formatScalar(row.before) }}</span>
|
||
</p>
|
||
<p class="text-muted">
|
||
{{ t('chat.shippingOrderReview.auditTrail.afterValue') }}:
|
||
<span class="text-secondary">{{ formatScalar(row.after) }}</span>
|
||
</p>
|
||
</li>
|
||
</ul>
|
||
</div>
|
||
<p v-if="item.reason" class="text-[11px] text-secondary">
|
||
{{ t('chat.shippingOrderReview.auditTrail.reason') }}:{{ item.reason }}
|
||
</p>
|
||
<details class="rounded border border-default">
|
||
<summary class="cursor-pointer px-2 py-1 text-[11px] text-muted">
|
||
{{ t('chat.shippingOrderReview.auditTrail.technicalDetails') }}
|
||
</summary>
|
||
<div class="space-y-2 border-t border-default p-2">
|
||
<div class="flex items-center justify-between gap-2">
|
||
<p class="font-medium text-secondary">
|
||
{{ t('chat.shippingOrderReview.auditTrail.before') }}
|
||
</p>
|
||
<button
|
||
type="button"
|
||
class="rounded border border-default px-1.5 py-0.5 text-[11px] hover-surface-soft"
|
||
data-testid="review-audit-copy-before"
|
||
@click="copyJson(item.before_snapshot)"
|
||
>
|
||
{{ t('chat.shippingOrderReview.auditTrail.copyJson') }}
|
||
</button>
|
||
</div>
|
||
<pre class="max-h-32 overflow-auto rounded bg-surface p-2 text-xs leading-relaxed">{{ formatJson(item.before_snapshot) }}</pre>
|
||
<div class="flex items-center justify-between gap-2">
|
||
<p class="font-medium text-secondary">
|
||
{{ t('chat.shippingOrderReview.auditTrail.after') }}
|
||
</p>
|
||
<button
|
||
type="button"
|
||
class="rounded border border-default px-1.5 py-0.5 text-[11px] hover-surface-soft"
|
||
data-testid="review-audit-copy-after"
|
||
@click="copyJson(item.after_snapshot)"
|
||
>
|
||
{{ t('chat.shippingOrderReview.auditTrail.copyJson') }}
|
||
</button>
|
||
</div>
|
||
<pre class="max-h-32 overflow-auto rounded bg-surface p-2 text-xs leading-relaxed">{{ formatJson(item.after_snapshot) }}</pre>
|
||
</div>
|
||
</details>
|
||
</template>
|
||
</div>
|
||
</li>
|
||
</ol>
|
||
<button
|
||
v-if="items.length < total"
|
||
type="button"
|
||
class="mt-3 w-full rounded border border-default px-2 py-1.5 text-xs hover-surface-soft disabled:opacity-50"
|
||
data-testid="review-audit-trail-load-more"
|
||
:disabled="loading"
|
||
@click="loadMore"
|
||
>
|
||
{{ loading ? t('chat.shippingOrderReview.auditTrail.loading') : t('chat.shippingOrderReview.auditTrail.loadMore') }}
|
||
</button>
|
||
</div>
|
||
</aside>
|
||
</div>
|
||
</template>
|