- 返回精简:每单只给定位字段 + params(contents[].params,形如
access_token=…&app_key=…&method=logistics.getShopKey&…&sign=…)与 template_url;
不再回整包 print_data / printer_config / document
- 限流:平台该接口有限流,改为一次只带一个 printHistoryId,逐张串行并间隔 300ms;
单张失败不影响其它张,失败原因逐单返回
- params 为空时直接判失败(平台的打印数据里没有 params)
实测:{ printHistoryId: "10000078656212" } → params 为 access_token=uo7…
&method=logistics.getShopKey&…&sign=8849…,template_url 为申通 76x130 模板。
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
370 lines
14 KiB
JavaScript
370 lines
14 KiB
JavaScript
'use strict';
|
||
|
||
/**
|
||
* 打印数据(params)有效期:店管家面团单打印数据只有 10 分钟有效,
|
||
* 这里按保守口径回给调用方,便于 WMS 判断是否需要重新拉取。
|
||
*/
|
||
const PRINT_DATA_TTL_SECONDS = 600;
|
||
|
||
/** 单次最多换取的运单号数量(一次打印任务的面单张数不会太多) */
|
||
const MAX_BATCH = 200;
|
||
|
||
/** 平台接口限流:同一个店铺两次请求之间至少间隔(毫秒),且一次只带一个 printHistoryId */
|
||
const THROTTLE_MS = 300;
|
||
|
||
/**
|
||
* 简单等待
|
||
* @param {number} ms 毫秒
|
||
* @return {Promise<void>} 无返回值
|
||
*/
|
||
function sleep(ms) {
|
||
return new Promise(resolve => setTimeout(resolve, ms));
|
||
}
|
||
|
||
/**
|
||
* 拆掉报文外层的 request / deliveryOrders 包装,取真正的入参对象
|
||
* @param {Object} data 原始报文
|
||
* @return {Object} 入参对象
|
||
*/
|
||
function unwrapBody(data) {
|
||
if (!data || typeof data !== 'object') return {};
|
||
if (data.request && typeof data.request === 'object' && !Array.isArray(data.request)) return data.request;
|
||
return data;
|
||
}
|
||
|
||
/**
|
||
* 归一化成非空字符串数组(兼容单值、逗号分隔、数组)
|
||
* @param {*} value 原始值
|
||
* @return {string[]} 去重后的字符串数组
|
||
*/
|
||
function toStringList(value) {
|
||
const raw = Array.isArray(value) ? value : [ value ];
|
||
const out = [];
|
||
for (const item of raw) {
|
||
if (item == null) continue;
|
||
for (const part of String(item).split(',')) {
|
||
const text = part.trim();
|
||
if (text) out.push(text);
|
||
}
|
||
}
|
||
return [ ...new Set(out) ];
|
||
}
|
||
|
||
/**
|
||
* 解析取号记录里的打印记录(`print_history_ids` 存的是 JSON 字符串)
|
||
* @param {string|Array} value 打印记录
|
||
* @return {Array<Object>} 打印记录数组
|
||
*/
|
||
function parsePrintHistoryIds(value) {
|
||
if (Array.isArray(value)) return value;
|
||
if (typeof value !== 'string' || !value.trim()) return [];
|
||
try {
|
||
const parsed = JSON.parse(value);
|
||
return Array.isArray(parsed) ? parsed : [];
|
||
} catch {
|
||
return [];
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 从一条取号记录里挑出与运单号对应的打印记录
|
||
*
|
||
* 一个取号请求可能取到多张面单(多包裹),`print_history_ids` 与 `waybill_codes` 按顺序对应;
|
||
* 优先按 `waybill_code` 精确匹配,匹配不到时(历史数据缺该字段)退回按位取。
|
||
* @param {Object} record 取号记录(Sequelize 实例或 plain object)
|
||
* @param {string} waybillCode 运单号
|
||
* @param {string[]} [preferredIds] 调用方直传的打印记录ID(命中则优先用)
|
||
* @return {Object|null} 打印记录 { id, pid, waybill_code, batch_index }
|
||
*/
|
||
function pickPrintHistory(record, waybillCode, preferredIds = []) {
|
||
const list = parsePrintHistoryIds(record && record.print_history_ids);
|
||
if (!list.length) return null;
|
||
if (preferredIds.length) {
|
||
const hit = list.find(item => item && preferredIds.includes(String(item.id)));
|
||
if (hit) return hit;
|
||
}
|
||
const matched = list.find(item => item && String(item.waybill_code || '') === waybillCode);
|
||
if (matched) return matched;
|
||
|
||
const codes = parsePrintHistoryIds(record && record.waybill_codes);
|
||
const index = codes.findIndex(code => String(code) === waybillCode);
|
||
return index >= 0 ? list[index] || null : null;
|
||
}
|
||
|
||
/**
|
||
* 解析平台返回的打印数据(params),取出打印机配置与面单 document
|
||
*
|
||
* `Data[].PrintDataJson` 是字符串,里面还有一层 `printDataJson`,形如
|
||
* `{ PrinterConfig, PrintDataList: { task: { documents: [ { documentID, contents } ] } } }`。
|
||
* 同店铺的 PrinterConfig / templateURL 相同,多张面单只需把各自 documents 拼成一个 task。
|
||
* @param {string} printData 平台返回的 PrintDataJson
|
||
* @return {{printer_config: Object|null, document: Object|null, documents: Array}} 解析结果
|
||
*/
|
||
function parsePrintData(printData) {
|
||
const empty = { printer_config: null, document: null, documents: [], params: '', template_url: '' };
|
||
if (typeof printData !== 'string' || !printData.trim()) return empty;
|
||
try {
|
||
const outer = JSON.parse(printData);
|
||
const inner = outer && outer.printDataJson ? JSON.parse(outer.printDataJson) : outer;
|
||
const documents = inner && inner.PrintDataList && inner.PrintDataList.task && inner.PrintDataList.task.documents;
|
||
const list = Array.isArray(documents) ? documents : [];
|
||
const document = list[0] || null;
|
||
const contents = document && Array.isArray(document.contents) ? document.contents : [];
|
||
// 打印端真正要的是 contents[].params(access_token=...&method=logistics.getShopKey&sign=...)
|
||
const first = contents.find(item => item && String(item.params || '').trim()) || contents[0] || {};
|
||
return {
|
||
printer_config: (inner && inner.PrinterConfig) || null,
|
||
document,
|
||
documents: list,
|
||
params: String(first.params || ''),
|
||
template_url: String(first.templateURL || ''),
|
||
};
|
||
} catch {
|
||
return empty;
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 取(并缓存)打单账号的完整实例:一次批量请求通常只涉及 1~2 个账号
|
||
* @param {Object} ctx egg context
|
||
* @param {Map} memo 批内缓存
|
||
* @param {Object} params 参数
|
||
* @param {number} params.accountId 打单账号ID
|
||
* @param {number} params.principalId 主体ID
|
||
* @return {Promise<Object|null>} { account, service }
|
||
*/
|
||
async function loadAccount(ctx, memo, { accountId, principalId }) {
|
||
const key = `${principalId}:${accountId}`;
|
||
if (memo.has(key)) return memo.get(key);
|
||
const account = await ctx.model.ExternalPrintAccount.findByPk(accountId, {
|
||
include: [{ model: ctx.model.ExternalPrintPlatform, as: 'platform' }],
|
||
});
|
||
if (!account || !account.platform || (account.principal_id && Number(account.principal_id) !== Number(principalId))) {
|
||
memo.set(key, null);
|
||
return null;
|
||
}
|
||
const service = ctx.service.externalPrintOrder.createPlatformService(account);
|
||
const entry = { account, service };
|
||
memo.set(key, entry);
|
||
return entry;
|
||
}
|
||
|
||
/**
|
||
* 按运单号(或打印记录ID)重新拉取面单打印数据
|
||
*
|
||
* 背景:取号返回的打印数据只有 10 分钟有效期,WMS 真正打印前必须换新的。
|
||
* 定位口径:WMS 传「店铺标识(CaiNiaoAuthInfoId)+ 运单号」,OMS 查取号记录得到该运单号的
|
||
* 打印记录ID,再回平台 `PrintHistory/LoadPrintData` 换新的 params。
|
||
* 也支持直传 `printHistoryIds`(命中即跳过按运单号的匹配)。
|
||
* @param {Object} ctx egg context
|
||
* @param {Object} params 参数
|
||
* @param {number} params.principalId 主体ID(来自 app_key)
|
||
* @param {number} [params.warehouseId] 仓库ID(来自 app_key,仅用于日志)
|
||
* @param {Object} params.data 报文
|
||
* @param {string} [params.requestId] 调用方 request_id(日志用)
|
||
* @return {Promise<Object>} { ok, message, data }
|
||
*/
|
||
async function getPrintData(ctx, { principalId, warehouseId, data, requestId }) {
|
||
const Op = ctx.app.Sequelize.Op;
|
||
const body = unwrapBody(data);
|
||
ctx.logger.info('[waybill.printdata] 换取打印数据 principal=%s warehouse=%s requestId=%s', principalId, warehouseId || '-', requestId || '-');
|
||
const waybillCodes = toStringList(
|
||
body.waybillCodes || body.waybillCode || body.mailNos || body.mailNo || body.expressCodes
|
||
);
|
||
const historyIds = toStringList(body.printHistoryIds || body.printHistoryId);
|
||
const shopIds = toStringList(body.caiNiaoAuthInfoIds || body.caiNiaoAuthInfoId || body.authInfoIds)
|
||
.map(value => Number(value))
|
||
.filter(value => Number.isFinite(value) && value > 0);
|
||
|
||
if (!waybillCodes.length && !historyIds.length) {
|
||
return { ok: false, message: '缺少运单号(waybillCodes)或打印记录ID(printHistoryIds)' };
|
||
}
|
||
if (waybillCodes.length > MAX_BATCH) {
|
||
return { ok: false, message: `单次最多换取 ${MAX_BATCH} 个运单号的打印数据` };
|
||
}
|
||
|
||
// 定位取号记录:运单号走索引;直传打印记录ID时按 JSON 里的 id 匹配(避免全表扫描)
|
||
const where = { principal_id: principalId, status: 1 };
|
||
if (shopIds.length) where.cai_niao_auth_info_id = { [Op.in]: shopIds };
|
||
const conditions = [];
|
||
if (waybillCodes.length) conditions.push({ waybill_code: { [Op.in]: waybillCodes } });
|
||
if (historyIds.length) {
|
||
conditions.push({
|
||
[Op.or]: historyIds.map(id => ({ print_history_ids: { [Op.like]: `%"id":"${id}"%` } })),
|
||
});
|
||
}
|
||
where[Op.or] = conditions;
|
||
const records = await ctx.model.WaybillFetchRecord.findAll({
|
||
where,
|
||
order: [[ 'id', 'DESC' ]],
|
||
});
|
||
|
||
// 运单号 → 取号记录(同号可能被多次取号,取最新一条)
|
||
const byWaybill = new Map();
|
||
for (const record of records) {
|
||
const code = String(record.waybill_code || '');
|
||
if (!code || byWaybill.has(code)) continue;
|
||
if (shopIds.length && !shopIds.includes(Number(record.cai_niao_auth_info_id))) continue;
|
||
byWaybill.set(code, record);
|
||
}
|
||
|
||
const results = [];
|
||
const memo = new Map();
|
||
const groups = new Map(); // `${accountId}|${host}|${dbname}` → { accountId, host, dbname, items: [] }
|
||
for (const code of waybillCodes) {
|
||
const record = byWaybill.get(code);
|
||
if (!record) {
|
||
results.push({ waybill_code: code, success: false, message: '未找到该运单号的取号记录(可能不是通过 OMS 取的号)' });
|
||
continue;
|
||
}
|
||
const history = pickPrintHistory(record, code, historyIds);
|
||
if (!history) {
|
||
results.push({
|
||
waybill_code: code,
|
||
success: false,
|
||
message: '取号记录里没有该运单号的打印记录ID(PrintHistoryIds),无法换新的打印数据',
|
||
});
|
||
continue;
|
||
}
|
||
const key = `${record.account_id}|${record.platform_host || ''}|${record.dbname || ''}`;
|
||
if (!groups.has(key)) {
|
||
groups.set(key, {
|
||
accountId: Number(record.account_id),
|
||
host: String(record.platform_host || ''),
|
||
dbname: String(record.dbname || ''),
|
||
items: [],
|
||
});
|
||
}
|
||
groups.get(key).items.push({ waybillCode: code, record, history });
|
||
}
|
||
|
||
// 直传 printHistoryIds 但没有对应运单号的情况:按记录里的运单号补齐
|
||
if (historyIds.length) {
|
||
for (const record of records) {
|
||
const list = parsePrintHistoryIds(record.print_history_ids)
|
||
.filter(item => item && historyIds.includes(String(item.id)));
|
||
for (const history of list) {
|
||
const code = String(history.waybill_code || record.waybill_code || '');
|
||
if (!code || results.some(row => row.waybill_code === code)) continue;
|
||
if (waybillCodes.length && !waybillCodes.includes(code)) continue;
|
||
const key = `${record.account_id}|${record.platform_host || ''}|${record.dbname || ''}`;
|
||
if (!groups.has(key)) {
|
||
groups.set(key, {
|
||
accountId: Number(record.account_id),
|
||
host: String(record.platform_host || ''),
|
||
dbname: String(record.dbname || ''),
|
||
items: [],
|
||
});
|
||
}
|
||
groups.get(key).items.push({ waybillCode: code, record, history });
|
||
}
|
||
}
|
||
}
|
||
|
||
for (const group of groups.values()) {
|
||
const found = await fetchGroup(ctx, { principalId, memo, group });
|
||
results.push(...found);
|
||
}
|
||
|
||
const success = results.filter(row => row.success).length;
|
||
return {
|
||
ok: true,
|
||
message: 'ok',
|
||
data: {
|
||
total: results.length,
|
||
success,
|
||
failed: results.length - success,
|
||
// params 只有 10 分钟有效期,调用方需要在这个时间内打印
|
||
expires_in: PRINT_DATA_TTL_SECONDS,
|
||
results,
|
||
},
|
||
};
|
||
}
|
||
|
||
/**
|
||
* 按「账号 + 域名 + 分区」分组拉取打印数据
|
||
*
|
||
* 平台侧对这个接口有限流,**一次只带一个 printHistoryId**,逐个串行请求并在两次之间留间隔,
|
||
* 避免同店铺多张面单一起打过去被限流/超时。
|
||
* @param {Object} ctx egg context
|
||
* @param {Object} params 参数
|
||
* @param {number} params.principalId 主体ID
|
||
* @param {Map} params.memo 批内账号缓存
|
||
* @param {Object} params.group 分组 { accountId, host, dbname, items }
|
||
* @return {Promise<Array<Object>>} 逐运单号结果
|
||
*/
|
||
async function fetchGroup(ctx, { principalId, memo, group }) {
|
||
const { accountId, host, dbname, items } = group;
|
||
const fallback = message => items.map(item => ({
|
||
waybill_code: item.waybillCode,
|
||
success: false,
|
||
message,
|
||
}));
|
||
|
||
const entry = await loadAccount(ctx, memo, { accountId, principalId });
|
||
if (!entry) return fallback('打单账号不存在或不属于该主体');
|
||
const { service } = entry;
|
||
if (typeof service.loadPrintData !== 'function') return fallback('当前打单平台不支持重新获取打印数据');
|
||
|
||
let traceId = '';
|
||
try {
|
||
const info = await service.getUserInfo();
|
||
traceId = (info.data && info.data.TraceId) || '';
|
||
} catch {
|
||
traceId = '';
|
||
}
|
||
|
||
const results = [];
|
||
for (let index = 0; index < items.length; index += 1) {
|
||
const item = items[index];
|
||
if (index > 0) await sleep(THROTTLE_MS);
|
||
let row = null;
|
||
try {
|
||
const list = await service.loadPrintData(host, {
|
||
traceId,
|
||
dbname,
|
||
printHistoryIds: [ item.history.id ],
|
||
});
|
||
row = list[0] || null;
|
||
} catch (err) {
|
||
ctx.logger.warn('[waybill.printdata] 换打印数据失败 host=%s dbname=%s id=%s:%s',
|
||
host, dbname, item.history.id, err.message);
|
||
results.push({ waybill_code: item.waybillCode, success: false, message: `读取打印数据失败:${err.message}` });
|
||
continue;
|
||
}
|
||
if (!row) {
|
||
results.push({ waybill_code: item.waybillCode, success: false, message: '平台未返回该运单号的打印数据' });
|
||
continue;
|
||
}
|
||
const parsed = parsePrintData(row.print_data);
|
||
if (!parsed.params) {
|
||
results.push({ waybill_code: item.waybillCode, success: false, message: '平台的打印数据里没有 params(access_token 串)' });
|
||
continue;
|
||
}
|
||
results.push({
|
||
waybill_code: item.waybillCode,
|
||
success: true,
|
||
message: 'ok',
|
||
print_history_id: row.print_history_id,
|
||
template_id: row.template_id,
|
||
cai_niao_auth_info_id: item.record.cai_niao_auth_info_id == null ? null : Number(item.record.cai_niao_auth_info_id),
|
||
dbname: item.record.dbname || '',
|
||
expires_in: PRINT_DATA_TTL_SECONDS,
|
||
// 打印端要的就是这串:access_token=...&app_key=...&method=logistics.getShopKey&...&sign=...
|
||
params: parsed.params,
|
||
template_url: parsed.template_url,
|
||
});
|
||
}
|
||
return results;
|
||
}
|
||
|
||
module.exports = {
|
||
getPrintData,
|
||
// 供单测/脚本复用
|
||
unwrapBody,
|
||
toStringList,
|
||
parsePrintHistoryIds,
|
||
pickPrintHistory,
|
||
parsePrintData,
|
||
};
|