- POST /openapi 入口(controller/openapi.js),协议与 WMS openapi 对齐(method/app_key/sign) - 方法注册表 lib/openapiMethods.js:index.ping、entryorder.confirm/match、deliveryorder.confirm、stockout.confirm、order.cancel - 单据处理公共逻辑 lib/orderCallback.js;请求/响应封装 lib/apiResponse.js、lib/openapiCallback.js - WMS 回调入口 controller/wmsNotify.js 改为复用公共模块 - 回调日志:callback_logs 表 + lib/callbackLog.js(脱敏、fire-and-forget)
84 lines
2.9 KiB
JavaScript
84 lines
2.9 KiB
JavaScript
'use strict';
|
||
|
||
/**
|
||
* 对外通用回调接口(POST /openapi)的公共辅助方法。
|
||
*
|
||
* 接口协议与 WMS /openapi 对齐(见 D:\wms\app\service\openapiEntry.js):
|
||
* - 系统参数走 query:method / app_key / v / timestamp / sign
|
||
* - 业务参数走 JSON body
|
||
* - 签名:md5(canonicalQuery + app_secret + md5(stableStringify(body))),见 lib/wms/sign.js
|
||
*/
|
||
|
||
/** 时间戳时效阈值:±10 分钟(秒) */
|
||
const TIME_TOLERANCE_SEC = 600;
|
||
|
||
/** 对外回调接口路径(与 router.js 注册的路由保持一致) */
|
||
const CALLBACK_PATH = '/openapi';
|
||
|
||
/**
|
||
* 归一化时间戳为 Unix 秒,兼容两种传法:
|
||
* - 秒(10 位):OMS/WMS openapi 入参使用
|
||
* - 毫秒(13 位):WMS 出站回调(openapi/callback.js)使用
|
||
* @param {*} value query 中的 timestamp
|
||
* @return {Number|null} 秒级时间戳;非法值返回 null
|
||
*/
|
||
function normalizeTimestamp(value) {
|
||
const num = Number(value);
|
||
if (!Number.isFinite(num) || num <= 0) return null;
|
||
// 大于 1e11 视为毫秒(1973 年以后的毫秒时间戳均满足该条件)
|
||
return num > 1e11 ? Math.floor(num / 1000) : Math.floor(num);
|
||
}
|
||
|
||
/**
|
||
* 判断时间戳是否在 ±TIME_TOLERANCE_SEC 时效内
|
||
* @param {Number} timestampSec 秒级时间戳
|
||
* @param {Number} [nowSec] 当前秒级时间戳
|
||
* @return {Boolean} 是否有效
|
||
*/
|
||
function isTimestampValid(timestampSec, nowSec = Math.floor(Date.now() / 1000)) {
|
||
return Math.abs(nowSec - timestampSec) <= TIME_TOLERANCE_SEC;
|
||
}
|
||
|
||
/**
|
||
* 解析请求体为对象(验签需使用「解析后的对象」,与签名方的 stableStringify 对齐)
|
||
* @param {Object} ctx egg context
|
||
* @return {Object|String} 解析后的对象;无法解析时返回原始字符串
|
||
*/
|
||
function parseBody(ctx) {
|
||
const parsed = ctx.request.body;
|
||
if (parsed && typeof parsed === 'object') return parsed;
|
||
const raw = ctx.request.rawBody;
|
||
if (!raw) return {};
|
||
if (typeof raw === 'string') {
|
||
try {
|
||
return JSON.parse(raw);
|
||
} catch {
|
||
return {};
|
||
}
|
||
}
|
||
return parsed || {};
|
||
}
|
||
|
||
/**
|
||
* 组装对外回调地址(在接口配置处展示,供外部系统配置)
|
||
* 形如 http://localhost:6090/openapi(系统参数 method/app_key/timestamp/sign 由调用方自行拼接)
|
||
* 优先使用 config.omsCallback.baseUrl(部署在网关/Nginx 后时配置),
|
||
* 未配置时按当前请求的访问地址动态生成(开发环境即为后端实际地址)。
|
||
* @param {Object} ctx egg context
|
||
* @return {String} 形如 http(s)://host/openapi
|
||
*/
|
||
function buildCallbackUrl(ctx) {
|
||
const configured = (ctx.app.config.omsCallback && ctx.app.config.omsCallback.baseUrl) || '';
|
||
const base = String(configured).replace(/\/+$/, '') || `${ctx.protocol}://${ctx.host}`;
|
||
return `${base}${CALLBACK_PATH}`;
|
||
}
|
||
|
||
module.exports = {
|
||
TIME_TOLERANCE_SEC,
|
||
CALLBACK_PATH,
|
||
normalizeTimestamp,
|
||
isTimestampValid,
|
||
parseBody,
|
||
buildCallbackUrl,
|
||
};
|