oms/backend/app/controller/callbackLog.js
liufumingren 1d29e03ba2 feat(log): 对外回调日志可查询
外部系统调用 OMS /openapi 的请求与返回一直有写 callback_logs,
但没有任何查询入口,WMS 侧排查「回调有没有到、报了什么」只能翻服务器日志。

- 新增 GET /api/callback-logs:按主体隔离,支持仓库/接口配置/方法/app_key/
  结果/IP/关键词/时间范围过滤 + 分页
- 补 contract schema(callbackLogItem / callbackLogPage / callbackLogListResponse)
- 外部接口页新增「回调日志」tab:展开可看请求参数、请求报文、返回报文、
  异常信息、来源IP 与 app_key,并支持按当前仓库导出

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-11-07 08:43:14 +08:00

78 lines
2.9 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';
const { Controller } = require('egg');
const { parsePagination, buildPageResult } = require('../lib/pagination');
/**
* @controller CallbackLogController 对外回调日志
* 记录外部系统(WMS/第三方)调用 OMS `POST /openapi` 的请求与返回,
* 含未通过验签/未注册方法的请求,便于排查「回调有没有到、报了什么」。
*/
class CallbackLogController extends Controller {
/**
* @summary 对外回调日志列表
* @description 分页查询当前主体的回调记录,支持按仓库、接口配置、方法、app_key、结果、IP、关键词、时间段过滤
* @router get /api/callback-logs
* @apikey
* @request query integer page 页码,默认 1
* @request query integer pageSize 每页条数,默认 20
* @request query integer warehouse_id 仓库ID,不传则返回该主体下全部
* @request query integer warehouse_api_id 接口配置ID,不传则不过滤
* @request query string method 回调方法名,如 entryorder.confirm / deliveryorder.getwaybill
* @request query string app_key 请求携带的 app_key
* @request query integer is_success 业务是否成功:1 是 / 0 否
* @request query string ip 调用方 IP
* @request query string keyword 关键词,匹配提示信息 / 失败原因
* @request query string start_time 起始时间(YYYY-MM-DD HH:mm:ss 或 YYYY-MM-DD)
* @request query string end_time 截止时间
* @response 200 callbackLogListResponse success
*/
async index() {
const { ctx } = this;
const Op = this.app.Sequelize.Op;
const { page, pageSize, offset, limit } = parsePagination(ctx.query);
const {
warehouse_id: warehouseId,
warehouse_api_id: apiId,
method,
app_key: appKey,
is_success: isSuccess,
ip,
keyword,
start_time: startTime,
end_time: endTime,
} = ctx.query;
const where = { principal_id: ctx.state.principalId };
if (warehouseId) where.warehouse_id = Number(warehouseId);
if (apiId) where.warehouse_api_id = Number(apiId);
if (method) where.method = method;
if (appKey) where.app_key = appKey;
if (ip) where.ip = ip;
if (isSuccess !== undefined && isSuccess !== '') where.is_success = Number(isSuccess) === 1 ? 1 : 0;
if (startTime || endTime) {
const range = {};
if (startTime) range[Op.gte] = new Date(String(startTime));
if (endTime) range[Op.lte] = new Date(String(endTime));
where.created_at = range;
}
if (keyword) {
where[Op.or] = [
{ message: { [Op.like]: `%${keyword}%` } },
{ error: { [Op.like]: `%${keyword}%` } },
];
}
const { count, rows } = await ctx.model.CallbackLog.findAndCountAll({
where,
offset,
limit,
order: [[ 'id', 'DESC' ]],
});
ctx.body = { code: 0, message: 'success', data: buildPageResult(rows, count, { page, pageSize }) };
}
}
module.exports = CallbackLogController;