R77-A1: 修复腾讯OCR的PDF识别链路

这个提交包含在:
2026-04-29 09:38:31 +08:00
父节点 80b1c31037
当前提交 b892f2f5bf
共修改 3 个文件,包含 106 行新增和 19 行删除
+81 -14
查看文件
@@ -1,4 +1,5 @@
const tencentcloud = require("tencentcloud-sdk-nodejs");
const PDF_MIME_TYPE = "application/pdf";
function normalizeDate(value) {
if (!value || typeof value !== "string") {
@@ -33,7 +34,7 @@ function toNumberString(value) {
return null;
}
const stripped = value.replace(/[,¥\s]/g, "").trim();
const stripped = value.replace(/[,¥¥\s]/g, "").trim();
if (!stripped) {
return null;
}
@@ -84,6 +85,35 @@ function normalizeString(value) {
return normalized || null;
}
function normalizeInvoiceInfoMap(result) {
if (!Array.isArray(result?.VatInvoiceInfos)) {
return null;
}
return result.VatInvoiceInfos.reduce((acc, item) => {
if (typeof item?.Name !== "string") {
return acc;
}
acc[item.Name.trim()] = item?.Value;
return acc;
}, {});
}
function resolveTransportMode(sourceMimeType) {
return sourceMimeType === PDF_MIME_TYPE ? "pdf_native" : "image_native";
}
function wrapTencentError(error, transportMode) {
const code = typeof error?.code === "string" ? error.code : "ocr_failed";
const message = error?.message ?? "ocr_failed";
const wrapped = new Error(message);
wrapped.code = code;
wrapped.transportMode = transportMode;
wrapped.cause = error;
return wrapped;
}
function createTencentOcrClient(config) {
if (!config.tencentSecretId || !config.tencentSecretKey) {
throw new Error("Tencent OCR credentials are missing");
@@ -105,32 +135,69 @@ function createTencentOcrClient(config) {
}
function normalizeVatInvoiceResult(result) {
const infoMap = normalizeInvoiceInfoMap(result);
const invoiceCodeFromInfo =
normalizeIdentifier(infoMap?.["发票代码"]) ?? normalizeIdentifier(infoMap?.["发票号码"]);
const invoiceNumberFromInfo =
normalizeIdentifier(infoMap?.["发票号码"]) ?? normalizeIdentifier(infoMap?.["发票代码"]);
const invoiceDateFromInfo = normalizeDate(infoMap?.["开票日期"]);
const amountFromInfo = toNumberString(infoMap?.["合计金额"] ?? infoMap?.["小计金额"]);
const taxAmountFromInfo = toNumberString(infoMap?.["合计税额"] ?? infoMap?.["小计税额"]);
const totalAmountFromInfo = toNumberString(infoMap?.["价税合计(小写)"]);
const sellerNameFromInfo = normalizeString(infoMap?.["销售方名称"]);
const buyerNameFromInfo = normalizeString(infoMap?.["购买方名称"]);
const amount = toNumberString(result?.AmountWithoutTax);
const taxAmount = toNumberString(result?.TaxAmount);
const totalAmountFromSource = toNumberString(result?.TotalAmount ?? result?.AmountInFiguers);
const derivedAmount = amount ?? subtractNumberStrings(totalAmountFromSource, taxAmount);
const derivedTotalAmount = totalAmountFromSource ?? addNumberStrings(derivedAmount, taxAmount);
const resolvedAmount = amount ?? amountFromInfo;
const resolvedTaxAmount = taxAmount ?? taxAmountFromInfo;
const resolvedTotalAmountFromSource = totalAmountFromSource ?? totalAmountFromInfo;
const derivedAmount =
resolvedAmount ?? subtractNumberStrings(resolvedTotalAmountFromSource, resolvedTaxAmount);
const derivedTotalAmount =
resolvedTotalAmountFromSource ?? addNumberStrings(derivedAmount, resolvedTaxAmount);
return {
invoiceCode: normalizeIdentifier(result?.InvoiceCode),
invoiceNumber: normalizeIdentifier(result?.InvoiceNum),
invoiceDate: normalizeDate(result?.InvoiceDate),
invoiceCode: normalizeIdentifier(result?.InvoiceCode) ?? invoiceCodeFromInfo,
invoiceNumber: normalizeIdentifier(result?.InvoiceNum) ?? invoiceNumberFromInfo,
invoiceDate: normalizeDate(result?.InvoiceDate) ?? invoiceDateFromInfo,
amount: derivedAmount,
taxAmount,
taxAmount: resolvedTaxAmount,
totalAmount: derivedTotalAmount,
sellerName: normalizeString(result?.SellerName),
buyerName: normalizeString(result?.BuyerName),
sellerName: normalizeString(result?.SellerName) ?? sellerNameFromInfo,
buyerName: normalizeString(result?.BuyerName) ?? buyerNameFromInfo,
raw: result ?? {},
};
}
async function runTencentVatOcr(config, imageBase64) {
async function runTencentVatOcr(config, { imageBase64, sourceMimeType }) {
const client = createTencentOcrClient(config);
const result = await client.VatInvoiceOCR({
ImageBase64: imageBase64,
});
const transportMode = resolveTransportMode(sourceMimeType);
return normalizeVatInvoiceResult(result);
try {
const request = {
ImageBase64: imageBase64,
};
if (transportMode === "pdf_native") {
request.IsPdf = true;
request.PdfPageNumber = 1;
}
const result = await client.VatInvoiceOCR(request);
const normalized = normalizeVatInvoiceResult(result);
return {
...normalized,
transportMode,
raw: {
...(normalized.raw ?? {}),
transportMode,
},
};
} catch (error) {
throw wrapTencentError(error, transportMode);
}
}
module.exports = {