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>
This commit is contained in:
liufumingren 2026-11-07 08:43:14 +08:00
parent 8b4fe94bbf
commit 1d29e03ba2
5 changed files with 226 additions and 1 deletions

View File

@ -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' },

View File

@ -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;

View File

@ -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);

View File

@ -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,
)
}

View File

@ -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<number | null>(null)
const {
loading: callbackLoading,
list: callbackLogs,
total: callbackTotal,
elapsed: callbackElapsed,
fetchList: fetchCallbackList,
handlePageChange: handleCallbackPageChange,
handleSizeChange: handleCallbackSizeChange,
} = useListPage<CallbackLogItem, typeof callbackQuery>({
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<WarehouseProductItem[]>([])
@ -1400,6 +1441,58 @@ onMounted(async () => {
<el-empty v-else description="请点击上方仓库查看调用日志" />
</div>
</el-tab-pane>
<el-tab-pane v-if="!isCompanyScope" label="回调日志" name="callback_logs">
<div class="detail-body is-config">
<template v-if="currentWarehouse">
<div class="items-toolbar">
<span class="items-title">回调日志 · {{ currentWarehouse.name }}</span>
<el-button size="small" :loading="callbackLoading" @click="fetchCallbackLogs">刷新</el-button>
<ExportButton type="callback_log" :filters="detailExportFilters" size="small" plain />
</div>
<div class="bottom-table-wrap">
<DataTable :loading="callbackLoading" :data="callbackLogs" height="100%">
<el-table-column type="expand">
<template #default="{ row }">
<div class="log-detail">
<div><b>请求参数:</b><pre class="log-pre">{{ row.request_query || '-' }}</pre></div>
<div><b>请求报文:</b><pre class="log-pre">{{ row.request_body || '-' }}</pre></div>
<div><b>返回报文:</b><pre class="log-pre">{{ row.response_body || '-' }}</pre></div>
<div v-if="row.error"><b>异常信息:</b><span class="break-all">{{ row.error }}</span></div>
<div><b>来源IP:</b>{{ row.ip || '-' }} <b>app_key:</b>{{ row.app_key || '-' }}</div>
</div>
</template>
</el-table-column>
<el-table-column prop="method" label="回调方法" min-width="200" show-overflow-tooltip />
<el-table-column label="结果" width="150">
<template #default="{ row }">
<el-tag :type="row.is_success ? 'success' : 'danger'" size="small">
{{ row.is_success ? '成功' : '失败' }}
</el-tag>
<span v-if="row.http_status" class="muted"> · HTTP {{ row.http_status }}</span>
</template>
</el-table-column>
<el-table-column prop="message" label="提示 / 失败原因" min-width="200" show-overflow-tooltip />
<el-table-column prop="duration" label="耗时" width="90">
<template #default="{ row }">{{ row.duration == null ? '-' : `${row.duration} ms` }}</template>
</el-table-column>
<el-table-column prop="created_at" label="时间" width="180">
<template #default="{ row }">{{ formatDateTime(row.created_at) }}</template>
</el-table-column>
</DataTable>
</div>
<ListPager
:total="callbackTotal"
:elapsed="callbackElapsed"
:page="callbackQuery.page"
:page-size="callbackQuery.pageSize"
@page-change="handleCallbackPageChange"
@size-change="handleCallbackSizeChange"
/>
</template>
<el-empty v-else description="请点击上方仓库查看回调日志" />
</div>
</el-tab-pane>
</el-tabs>
</template>
</SplitLayout>