WMS_MAIN/app/controller/warehouse.js
liufumingren 948f600689 feat(inbox): 回调推送、出站文档、搜索增强、日志与推送状态完善
出站回调:
- 新增 warehouse_notify.json 文档(notify.ping/notify.reply),与 warehouse.json 互跳
- 回调体统一 { method, code, data },method 放 query,body 为业务数据
- MD5 签名: sign=MD5(appSecret+timestamp+bodyJson)
- 回调响应: {code:200} 成功,非200/非code:200 视为失败
- docs 页面支持模拟推送调试

搜索增强(searchStock/searchWarehouseStock):
- 直接查 stock_warehouse,不再依赖 stock_store
- 新增字段: gross_weight/box_spec/packing_requirements/prohibited_type
- 管理标识: is_sn_mgmt/is_shelf_life_mgmt/is_batch_mgmt
- 条码关联: wms_stock_barcodes
- 图片: wms_file_reference(business_type=stock)+wms_file,返回数组
- id 修正为 stock_warehouse.id

推送状态完善:
- handleApiMessage 增加 push_status 更新(2成功/3失败)
- 未配回调地址 → push_status=3 + error 原因
- 回调异常 catch 也更新 push_status=3 + 写日志
- 出站日志写入 wms_warehouse_log 表(WN-前缀)

消息列表:
- keyword 兼容 content/keyword,支持数组
- keyword 搜索 content/group_name/user_name 三列
- 支持 where+operator 结构化查询(parseWhereConditions)
- storeId=-1 查询无货主消息; storeId=0→IS NULL
- 日期范围: startDate/endDate

其他:
- silent 动作: AI 识别无需回复时跳过回执
- ai_enabled 灰度开关(群级)
- bindCode 文档修正
- store_name 改用 StoreWarehouse.store_remask
2026-08-08 00:17:38 +08:00

110 lines
3.5 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 crypto = require('crypto');
const { toSuccess, toFailure } = require('../lib/api_response');
/** 日志内容长度上限 */
const LOG_BODY_MAX = 65535;
class WarehouseController extends Controller {
/**
* 输出仓库 API 统一失败响应。
* @param {number} httpStatus HTTP 状态码。
* @param {string} message 错误消息。
* @param {number} [code=httpStatus] 业务错误码。
* @return {void} 无返回值。
*/
writeFailureResponse(httpStatus, message, code = httpStatus) {
const { ctx } = this;
ctx.status = httpStatus;
ctx.body = toFailure({
message,
httpStatus,
code,
});
ctx.type = 'application/json';
}
/**
* 截断日志内容到上限长度。
* @param {string} value 原始字符串。
* @return {string} 截断后的字符串。
*/
truncateLogBody(value) {
const str = typeof value === 'string' ? value : JSON.stringify(value);
if (str.length > LOG_BODY_MAX) {
return str.substring(0, LOG_BODY_MAX);
}
return str;
}
/**
* 仓库 API 统一入口,记录请求日志并转发到 warehouse service。
* @return {Promise<void>} 无返回值,结果写入 ctx.body。
*/
async index() {
const { ctx, app } = this;
const startTime = Date.now();
const logId = 'WL-' + Date.now() + '-' + crypto.randomBytes(3).toString('hex');
const query = ctx.query || {};
let result;
try {
result = await ctx.service.warehouse.processRequest();
if (result.ok) {
ctx.status = Number(result.httpStatus) || 200;
ctx.body = toSuccess(result.data, result.message || 'ok');
ctx.type = 'application/json';
return;
}
this.writeFailureResponse(
Number(result.httpStatus) || 500,
String(result.message || '请求失败'),
Number(result.code) || Number(result.httpStatus) || 500
);
ctx.type = 'application/json';
return;
} catch (error) {
const message = String(error && error.message || '请求失败');
this.writeFailureResponse(500, `接口错误: ${message}`, 500);
ctx.type = 'application/json';
// 异常情况下 result 为 undefined,构造一个 fallback
result = {
ok: false,
httpStatus: 500,
message: error.message || '未知错误',
warehouseId: null,
appKey: String(query.app_key || '').trim() || null,
};
} finally {
// 异步写日志,不阻塞响应
try {
const logData = {
log_id: logId,
app_key: (result && result.appKey) || String(query.app_key || '').trim() || null,
method: String(query.method || '').trim() || null,
warehouse_id: (result && result.warehouseId) || null,
url: ctx.request.href || null,
http_status: ctx.status || (result && result.httpStatus) || null,
body: this.truncateLogBody(ctx.request.rawBody || JSON.stringify(ctx.request.body || {})),
result: this.truncateLogBody(typeof ctx.body === 'string' ? ctx.body : JSON.stringify(ctx.body || {})),
use_time: Date.now() - startTime,
created_at: new Date(),
};
app.fileWriter.writeRecord('warehouse-logs', logData).catch(writeErr => {
ctx.logger.error('[warehouse-log-sink] 写入文件失败: ' + writeErr.message);
});
} catch (logErr) {
ctx.logger.error('[warehouse-log] 日志记录异常: ' + logErr.message);
}
}
}
}
module.exports = WarehouseController;