diff --git a/backend/app/contract/index.js b/backend/app/contract/index.js index b244889..be91a75 100644 --- a/backend/app/contract/index.js +++ b/backend/app/contract/index.js @@ -856,6 +856,28 @@ module.exports = { }, apiOutboundLogPage: buildPage('apiOutboundLogItem', '对外API请求记录列表'), apiOutboundLogListResponse: buildListResponse('apiOutboundLogPage'), + callbackLogItem: { + id: { type: 'integer', description: '主键' }, + principal_id: { type: 'integer', description: '所属主体ID' }, + warehouse_id: { type: 'integer', description: '关联仓库ID' }, + warehouse_api_id: { type: 'integer', description: '命中的接口配置ID' }, + method: { type: 'string', description: '回调方法名,如 entryorder.confirm' }, + app_key: { type: 'string', description: '请求携带的 app_key' }, + ip: { type: 'string', description: '调用方 IP' }, + http_status: { type: 'integer', description: '响应 HTTP 状态码' }, + flag: { type: 'string', description: '结果标记:success / failure' }, + business_code: { type: 'string', description: '业务码' }, + message: { type: 'string', description: '提示信息 / 失败原因' }, + request_query: { type: 'string', description: '请求 query JSON(sign 已脱敏)' }, + request_body: { type: 'string', description: '请求报文 JSON(敏感字段已脱敏,超长截断)' }, + response_body: { type: 'string', description: '返回报文 JSON(超长截断)' }, + is_success: { type: 'integer', description: '业务是否成功:1是 0否' }, + error: { type: 'string', description: '失败信息(验签失败/未注册方法/单据处理失败等)' }, + duration: { type: 'integer', description: '耗时(毫秒)' }, + created_at: { type: 'string', description: '创建时间' }, + }, + callbackLogPage: buildPage('callbackLogItem', '对外回调日志列表'), + callbackLogListResponse: buildListResponse('callbackLogPage'), createWarehouseApiRequest: { principal_id: { type: 'integer', required: true, description: '所属主体ID,由请求拦截器注入' }, warehouse_id: { type: 'integer', required: true, description: '所属仓库ID' }, diff --git a/backend/app/controller/callbackLog.js b/backend/app/controller/callbackLog.js new file mode 100644 index 0000000..79097df --- /dev/null +++ b/backend/app/controller/callbackLog.js @@ -0,0 +1,77 @@ +'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; diff --git a/backend/app/router.js b/backend/app/router.js index 7a86935..dae161e 100644 --- a/backend/app/router.js +++ b/backend/app/router.js @@ -143,6 +143,8 @@ module.exports = app => { router.resources('warehouseApis', '/api/warehouse-apis', jwtAuth, principalCheck({ model: 'WarehouseApi' }), groupScope(), controller.warehouseApi); // 对外 API 调用日志(OMS → 外部 WMS 的请求记录),按主体隔离(principal_id 由前端拦截器注入 query) router.get('/api/outbound-logs', jwtAuth, principalCheck(), groupScope(), controller.apiOutboundLog.index); + // 对外回调日志(外部系统 → OMS /openapi 的请求与返回),排查「回调有没有到、报了什么」 + router.get('/api/callback-logs', jwtAuth, principalCheck(), groupScope(), controller.callbackLog.index); // 订单整单解析需在 resources('orders') 之前注册,避免被 /api/orders/:id 误匹配 router.post('/api/orders/:id/parse-items', jwtAuth, principalCheck({ model: 'Order' }), controller.order.parseItems); router.resources('orders', '/api/orders', jwtAuth, principalCheck({ model: 'Order' }), controller.order); diff --git a/frontend/src/api/callbackLog.ts b/frontend/src/api/callbackLog.ts new file mode 100644 index 0000000..4caa8bc --- /dev/null +++ b/frontend/src/api/callbackLog.ts @@ -0,0 +1,31 @@ +import { get } from '@/utils/request' + +/** 对外回调请求记录(外部系统 → OMS `POST /openapi`) */ +export interface CallbackLogItem { + id: number + principal_id: number | null + warehouse_id: number | null + warehouse_api_id: number | null + method: string + app_key: string | null + ip: string | null + http_status: number | null + flag: string | null + business_code: string | null + message: string | null + request_query: string | null + request_body: string | null + response_body: string | null + is_success: number + error: string | null + duration: number | null + created_at: string +} + +/** 对外回调日志列表(按当前主体、可选仓库过滤) */ +export function getCallbackLogs(params?: object) { + return get<{ list: CallbackLogItem[]; total: number; page: number; pageSize: number }>( + '/callback-logs', + params, + ) +} diff --git a/frontend/src/views/ExternalApi/Index.vue b/frontend/src/views/ExternalApi/Index.vue index 82fb3b4..110f37f 100644 --- a/frontend/src/views/ExternalApi/Index.vue +++ b/frontend/src/views/ExternalApi/Index.vue @@ -30,6 +30,7 @@ import { type WarehouseApiFieldGroup, } from '@/api/warehouseApi' import { getOutboundLogs, type OutboundLogItem } from '@/api/outboundLog' +import { getCallbackLogs, type CallbackLogItem } from '@/api/callbackLog' import { getWarehouseExpressMappings, createWarehouseExpressMapping, @@ -236,6 +237,7 @@ function handleDetailTabChange(name: string | number) { if (name === 'products' && productLoadedId.value !== currentWarehouse.value?.id) fetchProducts() if (name === 'inventory' && inventoryLoadedId.value !== currentWarehouse.value?.id) fetchInventory() if (name === 'logs' && outboundLoadedId.value !== currentWarehouse.value?.id) fetchOutboundLogs() + if (name === 'callback_logs' && callbackLoadedId.value !== currentWarehouse.value?.id) fetchCallbackLogs() } /* -------------------- 快递映射(按仓库:外部自定义编码 → 默认快递编码) -------------------- */ @@ -824,12 +826,51 @@ async function fetchOutboundLogs() { outboundLoadedId.value = warehouseId } -/** 切换仓库时清空调用日志缓存 */ +/** 切换仓库时清空调用日志/回调日志缓存 */ function resetOutboundState() { outboundLoadedId.value = null outboundQuery.page = 1 outboundLogs.value = [] outboundTotal.value = 0 + resetCallbackState() +} + +/* -------------------- 对外回调日志(外部系统 → OMS /openapi) -------------------- */ +const callbackQuery = reactive({ page: 1, pageSize: DEFAULT_PAGE_SIZE }) +/** 已加载回调日志的仓库ID,避免切换标签页时重复请求 */ +const callbackLoadedId = ref(null) + +const { + loading: callbackLoading, + list: callbackLogs, + total: callbackTotal, + elapsed: callbackElapsed, + fetchList: fetchCallbackList, + handlePageChange: handleCallbackPageChange, + handleSizeChange: handleCallbackSizeChange, +} = useListPage({ + query: callbackQuery, + fetcher: () => { + const warehouseId = currentWarehouse.value!.id + return getCallbackLogs({ warehouse_id: warehouseId, page: callbackQuery.page, pageSize: callbackQuery.pageSize }) + }, +}) + +/** 拉取当前仓库的回调日志并记录已加载的仓库 */ +async function fetchCallbackLogs() { + const warehouseId = currentWarehouse.value?.id + if (!warehouseId) return + await fetchCallbackList() + if (currentWarehouse.value?.id !== warehouseId) return + callbackLoadedId.value = warehouseId +} + +/** 切换仓库时清空回调日志缓存 */ +function resetCallbackState() { + callbackLoadedId.value = null + callbackQuery.page = 1 + callbackLogs.value = [] + callbackTotal.value = 0 } /** 商品标签页:已勾选的商品行 */ const selectedProducts = ref([]) @@ -1400,6 +1441,58 @@ onMounted(async () => { + + +
+ + +
+