'use strict'; const { Controller } = require('egg'); const { parsePagination, buildPageResult } = require('../lib/pagination'); /** * @controller ApiOutboundLogController 对外 API 调用日志 * 记录 OMS → 外部 WMS 的调用(index.ping / singleitem.synchronize 等),数据按主体隔离 */ class ApiOutboundLogController extends Controller { /** * @summary 对外 API 调用日志列表 * @description 分页查询当前主体下的对外调用记录,支持按仓库、接口配置、方法、关键词过滤 * @router get /api/outbound-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 WMS 方法名,如 index.ping / singleitem.synchronize * @request query string keyword 关键词,匹配请求地址/业务提示/异常信息 * @response 200 apiOutboundLogListResponse 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, keyword } = 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 (keyword) { where[Op.or] = [ { url: { [Op.like]: `%${keyword}%` } }, { business_message: { [Op.like]: `%${keyword}%` } }, { error: { [Op.like]: `%${keyword}%` } }, ]; } const { count, rows } = await ctx.model.ApiOutboundLog.findAndCountAll({ where, offset, limit, order: [[ 'id', 'DESC' ]], }); ctx.body = { code: 0, message: 'success', data: buildPageResult(rows, count, { page, pageSize }) }; } } module.exports = ApiOutboundLogController;