'use strict'; /** * 对外回调日志(callback_logs)写入工具。 * * 记录外部系统调用 POST /openapi 的请求(query / 报文)与返回(业务码 / 报文), * 用于排查「回调是否到达、method 是否注册、验签是否通过、单据是否处理成功」。 * 与 api_outbound_logs(OMS → 外部)方向相反,两者共同覆盖双向链路。 */ /** 单条文本落库前的最大字符数(超长截断,避免撑爆 TEXT) */ const MAX_LOG_TEXT = 4000; /** 落库前需脱敏的字段(大小写不敏感) */ const SENSITIVE_KEYS = [ 'sign', 'signature', 'app_secret', 'appsecret', 'password', 'token', 'salt' ]; /** * 递归脱敏敏感字段 * @param {*} value 任意值 * @return {*} 脱敏后的副本 */ function maskDeep(value) { if (!value || typeof value !== 'object') return value; const result = Array.isArray(value) ? [ ...value ] : { ...value }; for (const key of Object.keys(result)) { if (SENSITIVE_KEYS.includes(String(key).toLowerCase())) { result[key] = '***'; } else if (result[key] && typeof result[key] === 'object') { result[key] = maskDeep(result[key]); } } return result; } /** * 序列化为字符串并按上限截断 * @param {*} value 任意值 * @param {Number} [max] 最大字符数 * @return {String|null} 序列化结果;空值返回 null */ function safeStringify(value, max = MAX_LOG_TEXT) { if (value === undefined || value === null) return null; let text; try { text = typeof value === 'string' ? value : JSON.stringify(value); } catch { text = '[unserializable]'; } if (!text) return null; return text.length > max ? `${text.slice(0, max)}...[truncated]` : text; } /** * 记录一条对外回调日志 * 异步写库(不阻塞回调返回),写入失败只记错误日志,不影响业务结果。 * @param {Egg.Application} app egg application * @param {Object} entry 日志内容 * @param {String} entry.method 回调方法名(未传时存空串) * @param {String} [entry.appKey] 请求携带的 app_key * @param {Object} [entry.query] 请求 query(sign 会脱敏) * @param {*} [entry.requestBody] 请求报文(业务参数) * @param {*} [entry.responseBody] 返回报文(完整响应体) * @param {Object} [entry.api] 按 app_key 定位到的接口配置(可能为空) * @param {Object} [entry.warehouse] 接口配置关联的仓库(可能为空) * @param {Number} [entry.httpStatus] 响应 HTTP 状态码 * @param {Boolean} [entry.ok] 业务是否成功 * @param {String} [entry.message] 提示信息 / 错误信息 * @param {Number} [entry.duration] 耗时(毫秒) * @param {String} [entry.ip] 调用方 IP * @return {void} */ function recordCallbackLog(app, entry = {}) { if (!app || !app.model || !app.model.CallbackLog) return; const payload = { principal_id: entry.api ? entry.api.principal_id : null, warehouse_id: (entry.warehouse && entry.warehouse.id) || (entry.api && entry.api.warehouse_id) || null, warehouse_api_id: entry.api ? entry.api.id : null, method: String(entry.method || '').slice(0, 100), app_key: entry.appKey ? String(entry.appKey).slice(0, 100) : null, ip: entry.ip ? String(entry.ip).slice(0, 64) : null, http_status: entry.httpStatus || null, flag: entry.ok ? 'success' : 'failure', business_code: entry.responseBody && entry.responseBody.code != null ? String(entry.responseBody.code).slice(0, 32) : null, message: entry.message ? String(entry.message).slice(0, 500) : null, request_query: safeStringify(maskDeep(entry.query)), request_body: safeStringify(maskDeep(entry.requestBody)), response_body: safeStringify(entry.responseBody), is_success: entry.ok ? 1 : 0, error: entry.ok ? null : safeStringify(entry.message, 2000), duration: entry.duration == null ? null : Number(entry.duration), }; app.model.CallbackLog.create(payload).catch(err => { app.logger.error(`[callback-log] 写入失败:${err.message}`); }); } module.exports = { recordCallbackLog, maskDeep, safeStringify, MAX_LOG_TEXT };