liufumingren d725e7e457 feat(openapi): 出站回调命名与报文改奇门同层级
- 回调 method 由 snake_case 事件名改为 模块.动作:entryorder.confirm / entryorder.match / deliveryorder.confirm / stockout.confirm / deliveryorder.cancel(原 order.cancel 与入站方法同名,改名避免 callback_url 误指向 /openapi 时被路由到取消单据接口)。

- 时间戳统一为秒级(String(Math.floor(Date.now()/1000))),与 /openapi 入站 ±600 秒时效校验及回调调试页一致。

- 报文体由 { event, data, metadata } 改为奇门同层级 { request: {...} }(移除 metadata,事件标识只靠 query 的 method);入库类回调与奇门侧共用同一份报文组装。

- deliveryorder.confirm 改为逐单发送(仅同步成功的订单);stockout.confirm 直接复用 buildStockoutConfirmDataInternal 结果。

- 修复 entryorder.match 死代码:绑定成功且本次新建绑定时回传,重复触发不重复通知,回传失败不影响绑定。

- 调试页 /docs/api/openapi/callback-test 按真实报文预填;新增出站回调文档页 /docs/api/openapi_callback 及入口链接。

- script/update-api-doc.cjs 支持 spec.newDoc 新建文档与 docMeta.description(附单测)。

- 文档:methods/openapi_callback.json、AI_METHOD_ROUTING.md 5.6 节、docs/openapi-callback-events.md、docs/api-doc-update-workflow.md 等。
2026-09-13 02:24:37 +08:00

78 lines
2.2 KiB
JavaScript
Raw 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';
const MainService = require('../main');
/**
* OpenAPI 回调测试方法服务。
* 提供模拟发送回调通知的能力,用于测试回调链路是否正常。
*/
class OpenapiCallbackMethodService extends MainService {
/**
* 通过 app_key 解析应用信息。
* 查询 ApiApp 表校验 app_key 有效性。
* @param {string} appKey 开放接口 app_key。
* @return {Promise<Object>} 包含 appKey 的信息对象。
*/
async resolveAppInfo(appKey) {
const { ctx } = this;
const normalizedAppKey = String(appKey || '').trim();
if (!normalizedAppKey) {
throw new Error('app_key 缺失');
}
const appConfig = await ctx.model.ApiApp.findOne({
attributes: [ 'self_app_key' ],
where: {
self_app_key: normalizedAppKey,
is_enabled: 1,
},
raw: true,
});
if (!appConfig) {
throw new Error(`无法找到应用,请检查 app_key: ${normalizedAppKey}`);
}
return {
appKey: normalizedAppKey,
};
}
/**
* 模拟发送回调通知(测试用)。
* 通过 app_key 查找回调配置并发送一次回调请求。
* @param {Object} [payload={}] 调用参数。
* @param {Object} [payload.body={}] 请求体数据,可指定 event 与 request(奇门同层级业务报文,不含外层 request 包装)。
* @param {Object} [payload.query={}] 请求 query 数据,需包含 app_key。
* @return {Promise<Object>} 回调测试结果。
*/
async test(payload = {}) {
const { body = {}, query = {} } = payload;
const { event = 'deliveryorder.confirm', request = {} } = body;
try {
const info = await this.resolveAppInfo(query.app_key);
const result = await this.ctx.service.openapi.callback.sendByAppKey({
appKey: info.appKey,
event,
request,
});
if (result.success) {
return this.success('回调测试成功', { retryCount: result.retryCount });
}
return this.fail('回调测试失败', {
error: result.error,
retryCount: result.retryCount,
});
} catch (error) {
return this.fail(`回调测试异常: ${error.message}`);
}
}
}
module.exports = OpenapiCallbackMethodService;