const OCR_FIELD_LABELS: Record = { invoiceCode: "发票代码", invoiceNumber: "发票号码", invoiceDate: "开票日期", totalAmount: "总金额", sellerName: "销售方", }; export function parseMissingInvoiceOcrFields(rawError: string | null | undefined) { if (typeof rawError !== "string") { return []; } const matched = rawError.match(/^missing_required_fields:(.+)$/); if (!matched?.[1]) { return []; } return matched[1] .split(",") .map((field) => field.trim()) .filter(Boolean); } export function formatInvoiceOcrFieldLabels(fields: string[]) { return fields.map((field) => OCR_FIELD_LABELS[field] ?? field); } export function formatInvoiceOcrErrorMessage(rawError: string | null | undefined) { const missingFields = parseMissingInvoiceOcrFields(rawError); if (missingFields.length) { return `票据识别缺少关键字段:${formatInvoiceOcrFieldLabels(missingFields).join("、")}。`; } if (rawError === "duplicate_invoice_code_number") { return "已识别出相同发票,不能重复添加。"; } if (rawError === "mock_ocr_failed") { return "票据识别失败,请重试或人工补齐关键信息。"; } if (!rawError) { return "票据识别失败,请稍后重试。"; } return rawError; }