出站回调:
- 新增 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
186 lines
6.3 KiB
JavaScript
186 lines
6.3 KiB
JavaScript
'use strict';
|
||
|
||
const Service = require('egg').Service;
|
||
|
||
/** 消息体大小上限 64KB */
|
||
const MAX_BODY_SIZE = 64 * 1024;
|
||
|
||
/**
|
||
* 站内信接收服务。
|
||
* 统一处理消息入站:幂等去重、写库。
|
||
* 暂不包含出站转发逻辑,纯入库管理。
|
||
*/
|
||
class InboxReceiverService extends Service {
|
||
/**
|
||
* 统一接收入口:幂等去重、写库。
|
||
* @param {Object} data 消息数据。
|
||
* @param {string} data.ext_id 外部唯一消息主键。
|
||
* @param {number} data.warehouse_id 所属仓库ID(服务端上下文注入,禁止外部传入)。
|
||
* @param {string} [data.direction='inbound'] 消息方向。
|
||
* @param {number} [data.parent_id] 关联消息ID(回信时指向原消息)。
|
||
* @param {string} data.source_type 来源类型:im / api / system。
|
||
* @param {string} [data.source_extra] 来源附加信息。
|
||
* @param {string} data.target_type 目标类型:im / api / webhook。
|
||
* @param {string} [data.group_name] 群名称(外部传入)。
|
||
* @param {string} [data.user_name] 用户名称(外部传入)。
|
||
* @param {string} [data.group_id] 外部群ID。
|
||
* @param {string} [data.user_id] 外部用户ID。
|
||
* @param {string} [data.content] 消息正文/摘要。
|
||
* @param {Object} [data.ext_data] 透传数据。
|
||
* @param {Object} [data.flags] 处理标记。
|
||
* @return {Promise<{ok: boolean, id: number, ext_id: string}>} 接收结果。
|
||
*/
|
||
async receive(data = {}) {
|
||
const { ctx } = this;
|
||
const { InboxMessage } = ctx.model;
|
||
|
||
// 安全边界:校验 ext_id 不能为空
|
||
const extId = String(data.ext_id || '').trim();
|
||
if (!extId) {
|
||
throw new Error('ext_id 不能为空');
|
||
}
|
||
|
||
// 安全边界:warehouse_id 必须由服务端上下文提供
|
||
const warehouseId = Number(data.warehouse_id);
|
||
if (!Number.isFinite(warehouseId) || warehouseId <= 0) {
|
||
throw new Error('warehouse_id 无效');
|
||
}
|
||
|
||
// 枚举校验:direction
|
||
const VALID_DIRECTIONS = [ 'inbound', 'outbound' ];
|
||
const direction = VALID_DIRECTIONS.includes(data.direction) ? data.direction : 'inbound';
|
||
|
||
// 枚举校验:source_type
|
||
const VALID_SOURCE_TYPES = [ 'im', 'api', 'system' ];
|
||
const sourceType = VALID_SOURCE_TYPES.includes(data.source_type) ? data.source_type : (data.source_type || 'api');
|
||
|
||
// 枚举校验:target_type
|
||
const VALID_TARGET_TYPES = [ 'im', 'api', 'webhook' ];
|
||
const targetType = VALID_TARGET_TYPES.includes(data.target_type) ? data.target_type : (data.target_type || 'im');
|
||
|
||
// parent_id 存在性校验
|
||
const parentId = data.parent_id ? Number(data.parent_id) : null;
|
||
if (parentId) {
|
||
const parentExists = await InboxMessage.findOne({
|
||
where: { id: parentId, warehouse_id: warehouseId },
|
||
attributes: [ 'id' ],
|
||
});
|
||
if (!parentExists) {
|
||
throw new Error(`parent_id=${parentId} 对应的消息不存在`);
|
||
}
|
||
}
|
||
|
||
// 幂等检查:ext_id 已存在则直接返回
|
||
const exist = await InboxMessage.findOne({
|
||
where: { ext_id: extId },
|
||
attributes: [ 'id', 'ext_id' ],
|
||
paranoid: false,
|
||
});
|
||
|
||
if (exist) {
|
||
ctx.logger.info(`[inbox] 消息已存在,跳过入库 ext_id=${extId} id=${exist.id}`);
|
||
return {
|
||
ok: true,
|
||
id: exist.id,
|
||
ext_id: extId,
|
||
};
|
||
}
|
||
|
||
// 消息体大小限制
|
||
const checkBody = {
|
||
content: data.content || null,
|
||
ext_data: data.ext_data || null,
|
||
};
|
||
const bodySize = Buffer.byteLength(JSON.stringify(checkBody), 'utf8');
|
||
if (bodySize > MAX_BODY_SIZE) {
|
||
throw new Error(`消息体超过 64KB 限制(当前 ${Math.round(bodySize / 1024)}KB)`);
|
||
}
|
||
|
||
// 从群绑定查询货主ID(外部不再直接传入 store_id)
|
||
let storeId = null;
|
||
const extGroupId = String(data.group_id || '').trim();
|
||
if (extGroupId && warehouseId) {
|
||
try {
|
||
const group = await ctx.model.InboxGroup.findOne({
|
||
attributes: [ 'store_id' ],
|
||
where: { group_id: extGroupId, warehouse_id: warehouseId },
|
||
raw: true,
|
||
});
|
||
if (group && group.store_id) {
|
||
storeId = Number(group.store_id);
|
||
}
|
||
} catch (groupErr) {
|
||
ctx.logger.warn(`[inbox] 查找群货主失败: ${groupErr.message}`);
|
||
}
|
||
}
|
||
|
||
// 写库(暂不触发异步转发)
|
||
const record = await InboxMessage.create({
|
||
ext_id: extId,
|
||
warehouse_id: warehouseId,
|
||
store_id: storeId || null,
|
||
direction,
|
||
parent_id: parentId,
|
||
source_type: sourceType,
|
||
source_extra: data.source_extra || null,
|
||
target_type: targetType,
|
||
group_name: data.group_name || null,
|
||
user_name: data.user_name || null,
|
||
group_id: data.group_id || null,
|
||
user_id: data.user_id || null,
|
||
content: data.content || null,
|
||
ext_data: data.ext_data || null,
|
||
flags: data.flags || {},
|
||
});
|
||
|
||
ctx.logger.info(`[inbox] 消息入库成功 ext_id=${extId} id=${record.id}`);
|
||
|
||
// 自动登记群(幂等 upsert)
|
||
const groupName = String(data.group_name || '').trim();
|
||
if (groupName) {
|
||
try {
|
||
await ctx.model.InboxGroup.findOrCreate({
|
||
where: { group_name: groupName, warehouse_id: warehouseId },
|
||
defaults: {
|
||
group_id: extGroupId,
|
||
store_id: storeId || null,
|
||
source_type: sourceType,
|
||
source_extra: data.source_extra || null,
|
||
ai_enabled: 0,
|
||
},
|
||
});
|
||
} catch (groupErr) {
|
||
ctx.logger.warn(`[inbox] 群登记失败: ${groupErr.message}`);
|
||
}
|
||
}
|
||
|
||
// source_type 分流:API 来源异步回执
|
||
if (sourceType === 'api') {
|
||
setImmediate(() => {
|
||
ctx.service.inbox.processor.handleApiMessage({
|
||
warehouse_id: warehouseId,
|
||
store_id: storeId,
|
||
parent_id: record.id,
|
||
ext_data: data.ext_data || null,
|
||
ext_id: extId,
|
||
content: data.content || '',
|
||
group_name: data.group_name || null,
|
||
user_name: data.user_name || null,
|
||
group_id: data.group_id || null,
|
||
user_id: data.user_id || null,
|
||
}).catch(err => {
|
||
ctx.logger.error(`[inbox] API 异步回执异常 ext_id=${extId}: ${err.message}`);
|
||
});
|
||
});
|
||
}
|
||
|
||
return {
|
||
ok: true,
|
||
id: record.id,
|
||
ext_id: extId,
|
||
};
|
||
}
|
||
}
|
||
|
||
module.exports = InboxReceiverService;
|