refactor(shipping-ui): 收口订单复核与库存流程前端投影

This commit is contained in:
hugo committed 2026-09-04 21:01:59 +08:00
1 parent 265a6e179a
commit 4e8b2f8db2
27 files changed
+618 -407

No files matched your search

@@ -3,8 +3,18 @@
* 用途:将统一 ShippingProcessing 状态/阶段码映射到四语言 i18n 键。
* 依赖:vue-i18n Composer(由调用方传入 t / te)。
* 逻辑:统一读 chat.shippingProcessing.*;阶段展示优先消费后端 label_key
* (如 waiting_system_start),禁止把 stage code 直接当用户文案。
* (如 waiting_system_start),禁止把 stage code 直接当用户文案;
* v2 新增 Run 级 lifecycle_progress 的状态兜底映射与动态说明解析
* (设计:docs/superpowers/specs/2026-08-31-shipping-review-status-display-unification-design.md)。
*/
import type {
ShippingOrderExtractionLifecycleProgress,
ShippingUserProcessingStatus,
} from '@/features/chat/types/shipping.api.types';
import {
LIFECYCLE_STAGE_LABEL_KEYS,
mapProcessingStageToLifecycle,
} from '@/features/shipping-progress/stageMapping';
/** 会话 processing 旧 status_label → 统一 ShippingProcessingStatus。 */
const LEGACY_STATUS_TO_PROCESSING: Record<string, string> = {
@@ -137,7 +147,16 @@ export function resolveShippingProcessingStageLabel(
if (te(reviewKey)) return t(reviewKey);
}
const code = String(stage || '').trim();
if (!code) return '—';
if (!code) {
// [契约 §1.1「当前门禁 —」] 已知正式 OT 必有阶段,投影恒有 stage/lifecycle_stage;
// 投影未就绪由右卡 :loading 驱动骨架占位。此处禁止落「—」,改为统一的「获取中」占位文案,
// 避免用户看到无信息的破折号。仍在无 stage 且非 loading(连降级阶段都没有)时中性呈现。
const loadingKey = 'chat.shippingOrderReview.currentProcessing.stageLoading';
if (te(loadingKey)) return t(loadingKey);
const waitingKey = 'chat.shippingProcessing.stageLabel.waiting_system_start';
if (te(waitingKey)) return t(waitingKey);
return '正在获取最新状态…';
}
const stageKey = shippingProcessingStageKey(code);
if (te(stageKey)) return t(stageKey);
const reviewCodeKey = `chat.shippingOrderReview.currentProcessing.stages.${code}`;
@@ -148,6 +167,57 @@ export function resolveShippingProcessingStageLabel(
: '等待系统启动';
}
/**
* 解析「5 业务阶段」用户文案(批次中心与工作台共用词汇)。
*
* 三方统一约定:内联卡、右侧批次中心、工作台只消费后端 ui_state.lifecycle_stage,
* 映射 `chat.shippingNoticeUi.stage.<code>`(接收解析/订单复核/通知准备/货代上传/上传完成)。
* 入参可能是 5 业务阶段码或旧 15 技术阶段码:非业务码时按共享映射聚拢,确保永不显示
* “等待系统启动”这类技术阶段措辞。
*
* @param stage - ui_state.lifecycle_stage(业务码),或兼容的技术阶段码
* @param te - i18n te
* @param t - i18n t
* @returns 本地化文案;未知码回退 `—`
*/
export function resolveShippingLifecycleStageLabel(
stage: string | null | undefined,
te: (key: string) => boolean,
t: (key: string) => string,
): string {
const raw = String(stage || '').trim();
if (!raw) return '—';
const code = LIFECYCLE_STAGE_LABEL_KEYS[raw]
? raw
: mapProcessingStageToLifecycle(raw);
const labelKey = LIFECYCLE_STAGE_LABEL_KEYS[code];
const stageKey = `chat.shippingNoticeUi.stage.${labelKey}`;
if (labelKey && te(stageKey)) return t(stageKey);
return '—';
}
/**
* 判定阻塞原因是否为「通用兜底」(应在行内隐藏,避免与状态芯片重复)。
*
* 三方约定:仅当下发到具体、非兜底的阻塞原因(有真实 i18n 文案)才展示原因行;
* 进到 reasonCode.unknown / 无 i18n_key 的通用码一律视为重复信息。
*
* @param reason - 后端 blocking_reason(code / i18n_key)
* @param te - i18n te
* @returns true=通用兜底(应隐藏)
*/
export function isGenericBlockingReason(
reason: { code?: string | null; i18nKey?: string | null; i18n_key?: string | null } | null | undefined,
te: (key: string) => boolean,
): boolean {
if (!reason) return true;
const i18nKey = String(reason.i18nKey || reason.i18n_key || '').trim();
if (i18nKey && te(i18nKey)) return false;
const code = String(reason.code || '').trim();
if (code && code !== 'unknown' && te(`chat.shippingProcessing.reasonCode.${code}`)) return false;
return true;
}
/**
* 解析工作项类型文案键。
* @param workItemType - order_intake | order_task
@@ -171,3 +241,117 @@ export function shippingProcessingSourceTypeKey(sourceType: string): string {
? `chat.shippingProcessing.sourceType.${code}`
: 'chat.shippingProcessing.sourceType.eml';
}
/** ========================================================================
* v2(状态展示统一):Run 级 lifecycle_progress 的状态兜底与动态说明。
* 仅消费后端下发码;受控常量禁止运行时扩充。
* ======================================================================== */
/** lifecycle_status 白名单:白名单外(completed/未知)一律不产生说明与状态映射。 */
const EXTRACTION_ACTIVITY_STATUS_ALLOWLIST = new Set(['running', 'waiting', 'failed', 'cancelled']);
/**
* Run 级 lifecycle_status → 用户态兜底映射(spec §5.1)。
* 核心逻辑:waiting + 表内原因码才补充/细化用户态;表外原因码不映射(维持待机)。
* queue_backlog / processing_slow / processing_attention 属 health tip,可与 running 并存。
*/
const RUN_STATUS_FALLBACK_MAP: Readonly<Record<string, ShippingUserProcessingStatus>> = {
review_required: 'waiting_human',
queue_backlog: 'running',
processing_slow: 'running',
processing_attention: 'running',
};
/**
* 允许隐藏动态说明的原因码白名单(spec §5.2)。
* 核心逻辑:仅登记"纯重复状态语义"的原因;排队/偏慢/需关注属诊断信息,一律保留说明。
* review_required 若未来文案携带更具体原因,应移出本集合。
*/
const REDUNDANT_DETAIL_REASON_CODES: ReadonlySet<string> = new Set(['review_required']);
/**
* 从 lifecycle_progress 提取受控原因码。
* 核心逻辑:取 blocking_reason_i18n_key 末段(受控后缀解析),禁止模糊关键词匹配;
* 仅认 RUN_STATUS_FALLBACK_MAP 登记过的码,其余一律空串。
* @param progress - Run 级生命周期进度投影
* @returns 原因码(如 review_required);无则空串
*/
export function extractExtractionProgressReasonCode(
progress: ShippingOrderExtractionLifecycleProgress | null | undefined,
): string {
const key = String(progress?.blocking_reason_i18n_key || '').trim();
if (!key) return '';
const segment = key.split('.').pop() || '';
return segment in RUN_STATUS_FALLBACK_MAP ? segment : '';
}
/**
* Run 级 lifecycle_status → 用户态兜底(spec §5.1;仅供 currentTaskUserStatus 链尾使用)。
* 核心逻辑:running/waiting/failed 才补充用户态;原因码只能"补充/细化",
* 不能覆盖 failed/cancelled/completed;completed/cancelled/未知一律 null
* (避免误伤本页 next-step 与按钮逻辑)。
* @param progress - Run 级生命周期进度投影
* @returns 用户态 6 值之一;无可兜底值时 null
*/
export function resolveRunLifecycleUserStatus(
progress: ShippingOrderExtractionLifecycleProgress | null | undefined,
): ShippingUserProcessingStatus | null {
const status = String(progress?.lifecycle_status || '').trim().toLowerCase();
if (status === 'running') return 'running';
if (status === 'failed') return 'failed';
if (status === 'waiting') {
return RUN_STATUS_FALLBACK_MAP[extractExtractionProgressReasonCode(progress)] ?? null;
}
return null;
}
/**
* 解析 Run 级 lifecycle_progress 的一行动态说明(spec §5.2 detailMessage / §5.3 轻提示共用)。
* 核心逻辑:白名单外(completed/未知)返回 null,绝不回退成"等待启动";
* running 才可声称自动处理:诊断原因(health tip)优先于步骤句,缺省通用处理中;
* waiting 只用原因句、缺省中性等待;failed/cancelled 强制原因 key,禁止复用 running 的 step 文案。
* @param progress - Run 级生命周期进度投影
* @param i18n - 调用方传入的 te / t
* @returns 单行说明文本 + running 位;无可展示内容时 null
*/
export function resolveExtractionActivityMessage(
progress: ShippingOrderExtractionLifecycleProgress | null | undefined,
i18n: { te: (key: string) => boolean; t: (key: string) => string },
): { text: string; running: boolean } | null {
const status = String(progress?.lifecycle_status || '').trim().toLowerCase();
if (!progress || !EXTRACTION_ACTIVITY_STATUS_ALLOWLIST.has(status)) return null;
const { t, te } = i18n;
const localized = (key: string | null | undefined): string => {
const normalized = String(key || '').trim();
return normalized && te(normalized) ? t(normalized) : '';
};
const stepText = localized(progress.current_step_i18n_key);
const reasonText = localized(progress.blocking_reason_i18n_key);
if (status === 'running') {
return { text: reasonText || stepText || t('chat.shippingProcessing.progress.systemProcessingNeed'), running: true };
}
if (status === 'waiting') {
return { text: reasonText || t('chat.shippingProcessing.progress.lifecycleWaitingNeed'), running: false };
}
const terminalKey = status === 'failed'
? 'chat.shippingProcessing.progress.lifecycleReason.failed'
: 'chat.shippingProcessing.progress.lifecycleReason.cancelled';
return { text: te(terminalKey) ? t(terminalKey) : reasonText, running: false };
}
/**
* 判定动态说明是否属"纯重复"可隐藏(spec §5.2)。
* 核心逻辑:仅白名单内原因码且其状态兜底映射结果 == 当前展示 user_status 才隐藏;
* 未知原因默认保留;不做模糊匹配、不比较任何语言的渲染文本。
* @param reasonCode - extractExtractionProgressReasonCode 的输出
* @param userStatus - 当前展示的用户态
* @returns true=隐藏说明行
*/
export function isRedundantExtractionDetail(
reasonCode: string,
userStatus: string | null | undefined,
): boolean {
const code = String(reasonCode || '').trim();
if (!REDUNDANT_DETAIL_REASON_CODES.has(code)) return false;
return RUN_STATUS_FALLBACK_MAP[code] === String(userStatus || '').trim();
}