oms/backend/app/lib/externalPrintConfig.js
liufumingren eaf957ed14 feat(express): 外部打单快递接口、自定义快递编码与回 OMS 取号
- 外部打单平台快递接口:按账号同步平台模板、绑定快递编码、查看模板明细;打开列表只读库,仅「从平台同步数据」时拉取
- 系统管理新增自定义快递编码字典,订单改快递 / 物流下拉合并系统与自定义编码,推送奇门沿用订单上的编码
- 新增 OpenAPI deliveryorder.getwaybill:WMS 批量回取号(单次≤100、并发4、逐单结果),快递编码只取 WMS 报文,取消/作废/归档返回失败,已取号直接复用
- 新增 openapi_request_idempotency 请求级幂等:request_id 回放、报文摘要校验、30 分钟 stale 接管、7 天定时清理
- 外部打单平台订单按平台 fetch_waybill_no 下发 need_oms_waybill,推送时实时判定并回写单据
- 店铺、打单账号平台配置与「平台+编码→模板」绑定走 Redis 缓存,绑定/账号变更主动失效,避免批量推送与取号逐单查库

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-11-04 04:10:37 +08:00

71 lines
2.4 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

'use strict';
/**
* 外部打单(店铺 / 账号 / 平台)配置读取
*
* 推送与取号都是逐单处理,直接查库会放大成 N 倍数据库压力,这里统一走缓存服务:
* 命中返回缓存,未命中查库并回写;缓存服务不可用(如单元测试直接 new Service)时降级直查。
*/
/**
* 读取店铺基础资料(按主体隔离校验)
* @param {Object} ctx egg context
* @param {Number} shopId 店铺ID
* @param {Number} [principalId] 主体ID,传入时校验归属
* @return {Promise<Object|null>} 店铺基础资料(plain),不存在或跨主体为 null
*/
async function getShopBasic(ctx, shopId, principalId) {
if (!shopId) return null;
const cache = ctx.service && ctx.service.cache;
let shop = null;
if (cache && typeof cache.getShopBasic === 'function') {
shop = await cache.getShopBasic(shopId);
} else {
const row = await ctx.model.Shop.findByPk(shopId, {
attributes: [ 'id', 'principal_id', 'name', 'type', 'external_print_account_id' ],
});
shop = row ? row.toJSON() : null;
}
if (!shop) return null;
if (principalId != null && Number(shop.principal_id) !== Number(principalId)) return null;
return shop;
}
/**
* 读取外部打单账号及其平台配置(含 fetch_waybill_no)
* @param {Object} ctx egg context
* @param {Number} accountId 外部打单账号ID
* @param {Number} [principalId] 主体ID,传入时校验归属
* @return {Promise<Object|null>} 账号与平台配置(plain),不存在或跨主体为 null
*/
async function getAccountWithPlatform(ctx, accountId, principalId) {
if (!accountId) return null;
const cache = ctx.service && ctx.service.cache;
let account = null;
if (cache && typeof cache.getExternalPrintAccount === 'function') {
account = await cache.getExternalPrintAccount(accountId);
} else {
const row = await ctx.model.ExternalPrintAccount.findByPk(accountId, {
attributes: [ 'id', 'principal_id', 'platform_id' ],
include: [
{
model: ctx.model.ExternalPrintPlatform,
as: 'platform',
attributes: [ 'id', 'code', 'name', 'url', 'fetch_waybill_no' ],
},
],
});
account = row ? row.toJSON() : null;
}
if (!account) return null;
if (principalId != null && Number(account.principal_id) !== Number(principalId)) return null;
return account;
}
module.exports = {
getShopBasic,
getAccountWithPlatform,
};