WMS_MAIN/app/service/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

295 lines
7.6 KiB
JavaScript
Raw 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 Service = require('egg').Service;
const WAREHOUSE_INTERFACE_TYPE_ID = 3;
class WarehouseService extends Service {
/**
* 解析 warehouse method,拆分为服务路径与动作名。
* @param {string} methodName method 名称。
* @return {{ servicePath: string, actionName: string }} 解析结果。
*/
resolveWarehouseMethod(methodName = '') {
const method = String(methodName || '').trim();
const parts = method.split('.').filter(Boolean);
const actionName = parts.pop() || '';
const servicePath = parts.join('.');
return {
servicePath,
actionName,
};
}
/**
* 根据服务路径查找 warehouseMethods 下的目标服务。
* @param {string} servicePath 服务路径。
* @return {Object|null} 目标服务实例。
*/
getWarehouseMethodService(servicePath = '') {
const { ctx } = this;
const pathSegments = String(servicePath || '').split('.').filter(Boolean);
let methodService = ctx.service.warehouseMethods;
if (pathSegments.length === 0) {
return methodService;
}
for (const segment of pathSegments) {
methodService = methodService && methodService[segment];
}
return methodService || null;
}
/**
* 分发 warehouse method 到 warehouseMethods 目录。
* 分发前统一注入仓库上下文(warehouseId/appKey)。
* @param {string} methodName method 名称。
* @param {Object} body 请求体。
* @param {Object} query 请求 query。
* @param {Object} warehouseInfo 仓库上下文信息。
* @return {Promise<Object>} method 执行结果。
*/
async dispatchWarehouseMethod(methodName, body, query, warehouseInfo) {
const { servicePath, actionName } = this.resolveWarehouseMethod(methodName);
const methodService = this.getWarehouseMethodService(servicePath);
if (!methodService || typeof methodService[actionName] !== 'function') {
return {
ok: false,
httpStatus: 400,
code: 400,
message: `warehouse method 不存在: ${methodName}`,
data: null,
};
}
// 统一注入仓库上下文到方法服务实例
if (warehouseInfo && typeof methodService.setInfo === 'function') {
methodService.setInfo(
null, // storeId
warehouseInfo.warehouseId,
warehouseInfo.appKey,
null, // customerId
null // warehouseCode
);
}
const methodResult = await methodService[actionName]({
body,
query,
});
return {
ok: true,
httpStatus: 200,
code: 1,
message: 'ok',
data: methodResult,
};
}
/**
* 校验 warehouse 基础 query 参数。
* @param {Object} query 请求 query 参数。
* @return {boolean} 是否通过基础参数校验。
*/
validateWarehouseParams(query) {
const requiredFields = [ 'method', 'v', 'timestamp' ];
for (const field of requiredFields) {
if (!query || !query[field]) {
return false;
}
}
if (typeof query.method !== 'string' || !query.method.includes('.')) {
return false;
}
return true;
}
/**
* 读取并解析 JSON 请求体。
* @return {Promise<Object>} 解析后的 JSON 对象。
*/
async parseJsonBody() {
const { ctx } = this;
let jsonData = '';
if (ctx.request.rawBody) {
jsonData = ctx.request.rawBody;
} else if (ctx.request.body !== undefined) {
jsonData = typeof ctx.request.body === 'string'
? ctx.request.body
: JSON.stringify(ctx.request.body);
} else {
jsonData = await new Promise((resolve, reject) => {
let data = '';
ctx.req.on('data', chunk => {
data += chunk;
});
ctx.req.on('end', () => {
resolve(data);
});
ctx.req.on('error', reject);
});
}
jsonData = jsonData ? String(jsonData) : '';
if (!jsonData || jsonData.trim() === '') {
return {
ok: false,
httpStatus: 400,
code: 400,
message: 'json数据为空',
data: null,
};
}
try {
return JSON.parse(jsonData);
} catch (error) {
return {
ok: false,
httpStatus: 400,
code: 400,
message: 'json数据解析错误',
data: null,
};
}
}
/**
* 提取并归一化 warehouse 所需的 appKey 与 sign。
* @param {Object} query 请求 query 参数。
* @return {{ appKey: string, providedSign: string }} 归一化后的关键信息。
*/
normalizeSignFields(query = {}) {
const appKey = String(query.app_key || query.appkey || query.self_app_key || '').trim();
const providedSign = String(query.sign || query.signature || '').trim();
return {
appKey,
providedSign,
};
}
/**
* 处理 warehouse 请求并返回统一结果。
* 核心差异:使用 interface_type_id=3(仓库级),无需合约关联,直接取 warehouse_id。
* @return {Promise<Object>} 统一结果对象。
*/
async processRequest() {
const { ctx } = this;
const query = ctx.query || {};
if (!this.validateWarehouseParams(query)) {
return {
ok: false,
httpStatus: 400,
code: 400,
message: '参数缺失或错误: method/v/timestamp',
data: null,
warehouseId: null,
appKey: String(query.app_key || '').trim() || null,
};
}
// 时间戳时效校验(±10分钟)
const nowTs = Math.floor(Date.now() / 1000);
const reqTs = Number(query.timestamp);
if (!Number.isFinite(reqTs) || Math.abs(nowTs - reqTs) > 600) {
return {
ok: false,
httpStatus: 400,
code: 400,
message: `时间戳已过期或无效,服务器时间: ${nowTs}`,
data: null,
warehouseId: null,
appKey: String(query.app_key || '').trim() || null,
};
}
const body = await this.parseJsonBody();
if (body && body.ok === false) {
return {
...body,
warehouseId: null,
appKey: String(query.app_key || '').trim() || null,
};
}
const { appKey, providedSign } = this.normalizeSignFields(query);
if (!appKey) {
return {
ok: false,
httpStatus: 400,
code: 400,
message: '参数缺失或错误: app_key',
data: null,
warehouseId: null,
appKey: null,
};
}
const { systems: systemsService } = ctx.service;
const apiSignService = systemsService.apiSign;
// 仓库级验签:interface_type_id = 3
const verifyResult = await apiSignService.verifyRequestSignByAppKey({
appKey,
query,
body,
providedSign,
interfaceTypeId: WAREHOUSE_INTERFACE_TYPE_ID,
});
if (!verifyResult.ok) {
return {
ok: false,
httpStatus: 401,
code: 401,
message: `签名校验失败: ${verifyResult.message}`,
data: null,
warehouseId: null,
appKey,
};
}
// 仓库级无需合约关联,直接使用 api_app.warehouse_id
const warehouseId = verifyResult.warehouseId;
if (!warehouseId) {
return {
ok: false,
httpStatus: 403,
code: 403,
message: 'API 应用未绑定仓库',
data: null,
warehouseId: null,
appKey: verifyResult.appKey,
};
}
const queryWithAppKey = {
...query,
app_key: verifyResult.appKey,
};
const dispatchResult = await this.dispatchWarehouseMethod(query.method, body, queryWithAppKey, {
warehouseId,
appKey: verifyResult.appKey,
});
return {
...dispatchResult,
warehouseId,
appKey: verifyResult.appKey,
};
}
}
module.exports = WarehouseService;