liufumingren e61efbb66a feat(inbox): 站内信模块完整实现 - AI智能助手、群管理、消息列表增强、灰度开关
新增功能:
- AI智能助手: DeepSeek多轮推理、函数调用(searchStock/createOrder等)
- silent动作: AI识别无需回复的消息(好的/谢谢/再见)自动跳过回执
- ai_enabled灰度开关: 群级别控制AI启停,支持逐群放量
- 消息列表增强: storeId/groupId/groupName/userName/content/sourceType多条件筛选,时间格式化

代码优化:
- processWithAI: 多轮推理循环(MAX_ROUNDS=5)、对话历史不含[#N]前缀防AI污染
- 回执记录补齐group_id/user_id/group_name/user_name上下文
- 移除未使用的target_channel字段及迁移
- bindCode文档修正: store_warehouse → store

接口文档:
- system.json v0.3.18→0.3.19: 新增aiEnabled参数、消息列表响应扩充
- changes/system.json: 新增bindCode变更记录

杂项:
- store_warehouse.js JSDoc补全
- wave.js waveBatchSource修复
2026-08-06 17:40:36 +08:00

190 lines
6.1 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 MainService = require('../main');
/**
* 站内信系统方法层。
* 提供需要用户权限的内部管理接口。
*/
class InboxSystemService extends MainService {
/**
* 群管理子入口,供 /system dispatcher 按 inbox.group.* 路径访问。
* @return {{list: Function, disable: Function, update: Function, messages: Function, checkAuth: Function}}
*/
get group() {
return {
list: data => this.groupList(data),
disable: data => this.groupDisable(data),
update: data => this.groupUpdate(data),
messages: data => this.groupMessages(data),
checkAuth: (...args) => this.checkAuth(...args),
};
}
/**
* 消息管理子入口,供 /system dispatcher 按 inbox.message.* 路径访问。
* @return {{list: Function, detail: Function, thread: Function, reply: Function, stats: Function, checkAuth: Function}}
*/
get message() {
const { ctx } = this;
return {
list: data => {
const warehouseId = Number(ctx.user?.store_id || 0);
return ctx.service.inbox.message.list({ warehouseId, ...data });
},
detail: data => {
const warehouseId = Number(ctx.user?.store_id || 0);
return ctx.service.inbox.message.detail({ warehouseId, id: data.id });
},
thread: data => {
const warehouseId = Number(ctx.user?.store_id || 0);
return ctx.service.inbox.message.thread({ warehouseId, id: data.id });
},
stats: data => {
const warehouseId = Number(ctx.user?.store_id || 0);
return ctx.service.inbox.message.stats({ warehouseId, direction: data.direction });
},
checkAuth: (...args) => this.checkAuth(...args),
};
}
/**
* 生成货主绑群码。
* 委托给 systems.store.bindCode 执行:校验 StoreWarehouse 合约 → 生成12位随机码 → 缓存300秒。
* warehouseId 从 ctx.user.store_id 自动注入,绑群码缓存 key 为 chat:bind:{warehouseId}:{code}。
*
* @param {Object} data 请求数据。
* @param {number} data.storeId 货主ID。
* @param {string} [data.subType] 群子类型编码:after_sales(售后) / finance(财务) / receiving(收货)。
* @return {Promise<Object>} { code: 绑群码 }。
*/
async bindCode(data = {}) {
const { ctx } = this;
const { storeId, subType } = data;
const warehouseId = Number(ctx.user?.store_id || 0);
return await ctx.service.systems.store.bindCode({
storeId,
warehouseId,
subType,
});
}
/**
* 拉取当前仓库的群列表。
* warehouseId 从 ctx.user.store_id 自动注入。
*
* @param {Object} data 请求数据。
* @param {string} [data.groupType] 按群类型筛选:store / warehouse / express。
* @param {string} [data.keyword] 群名称模糊搜索。
* @param {number} [data.page] 页码。
* @param {number} [data.pageSize] 每页条数。
* @return {Promise<Object>} { total, list }。
*/
async groupList(data = {}) {
const { ctx } = this;
const { groupType, keyword, page, pageSize } = data;
const warehouseId = Number(ctx.user?.store_id || 0);
return await ctx.service.systems.inbox.group.list({
warehouseId,
groupType,
keyword,
page,
pageSize,
});
}
/**
* 禁用 / 解禁群。
* warehouseId 从 ctx.user.store_id 自动注入。
*
* @param {Object} data 请求数据。
* @param {number} data.id 群记录主键ID(必填)。
* @param {number} [data.isBlocked=1] 是否禁用 1=是 0=否。
* @return {Promise<Object>} { message: 操作结果 }。
*/
async groupDisable(data = {}) {
const { ctx } = this;
const { id, isBlocked } = data;
const warehouseId = Number(ctx.user?.store_id || 0);
await ctx.service.systems.inbox.group.disable({
id,
warehouseId,
isBlocked: isBlocked === 0 ? 0 : 1,
});
return { message: isBlocked === 0 ? '群已启用' : '群已禁用' };
}
/**
* 修改群信息。
* 支持修改 group_type / sub_type / store_id / express_interfaces / system_express_code。
* warehouseId 从 ctx.user.store_id 自动注入。
*
* @param {Object} data 请求数据。
* @param {number} data.id 群记录主键ID(必填)。
* @param {string} [data.groupType] 群类型:store / warehouse / express。
* @param {string} [data.subType] 群子类型。
* @param {number} [data.storeId] 关联货主ID。
* @param {number[]} [data.expressInterfaces] 快递接口ID数组(仅 express 生效)。
* @param {string} [data.systemExpressCode] 系统快递编码(express 时必填)。
* @return {Promise<Object>} { message: 操作结果 }。
*/
async groupUpdate(data = {}) {
const { ctx } = this;
const {
id, groupType, subType, storeId,
expressInterfaces, systemExpressCode, aiEnabled,
} = data;
const warehouseId = Number(ctx.user?.store_id || 0);
await ctx.service.systems.inbox.group.update({
id,
warehouseId,
groupType,
subType,
storeId,
expressInterfaces,
systemExpressCode,
aiEnabled,
});
return { message: '群信息修改成功' };
}
/**
* 拉取指定群的消息列表。
* warehouseId 从 ctx.user.store_id 自动注入。
*
* @param {Object} data 请求数据。
* @param {string} data.groupId 群ID(必填)。
* @param {string} [data.direction] 消息方向筛选:inbound / outbound。
* @param {string} [data.keyword] 关键词搜索(ext_id / content)。
* @param {number} [data.page] 页码。
* @param {number} [data.pageSize] 每页条数。
* @return {Promise<Object>} { list, pagination }。
*/
async groupMessages(data = {}) {
const { ctx } = this;
const { groupId, direction, keyword, page, pageSize } = data;
const warehouseId = Number(ctx.user?.store_id || 0);
if (!groupId || typeof groupId !== 'string' || !groupId.trim()) {
throw new Error('groupId 不能为空');
}
return await ctx.service.inbox.message.list({
warehouseId,
groupId: groupId.trim(),
direction,
keyword,
page,
pageSize,
});
}
}
module.exports = InboxSystemService;