liufumingren 806e739267 feat(inbox): v2 规则引擎完善(help 指令/建单开关/系统回复)并修复拦截日志误写仓库 ID
- functions.js: 拦截/取消拦截波次感知写订单日志 businessId 误用仓库 ID,改为订单 ID
  (生产订单 JY2026091813795 已发起拦截但订单详情查不到拦截日志的根因)
- rules/v2/processor/config/functionSync: 新增 help 指令、create_work_order=0 时跳过
  工单登记、系统级回复与无单号提示、白名单范围修正
- after_sale_reply 模型 + 6 个迁移: create_work_order 字段与相关配置种子
- return_unpack: 列表 count 统计修复、分页排序追加主键防重漏行、视频附加 camera_name
- methods/system.json + changes + sync 脚本: 接口文档与权限同步
2026-09-18 22:38:57 +08:00

332 lines
12 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 MainService = require('../main');
/**
* 站内信系统方法层。
* 提供需要用户权限的内部管理接口。
*/
class InboxSystemService extends MainService {
/**
* 群管理子入口,供 /system dispatcher 按 inbox.group.* 路径访问。
* @return {{list: Function, quickList: Function, members: Function, disable: Function, update: Function, reset: Function, messages: Function, checkAuth: Function}} 群管理子入口方法集合。
*/
get group() {
return {
list: data => this.groupList(data),
quickList: data => this.groupQuickList(data),
members: data => this.groupMembers(data),
disable: data => this.groupDisable(data),
update: data => this.groupUpdate(data),
reset: data => this.groupReset(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 });
},
reply: data => this.reply(data),
retry: data => this.retry(data),
checkAuth: (...args) => this.checkAuth(...args),
};
}
/**
* v2 可配置回信引擎配置管理子入口,供 /system dispatcher 按 inbox.v2.* 路径访问。
* 全部委托给 systems/inbox/v2.js(配置管理系统服务层),本层只做 checkAuth 代理。
* @return {Object} v2 配置管理方法集合(type/subType/keyword/numberRule/function/reply/relation)。
*/
get v2() {
const { ctx } = this;
const svc = () => ctx.service.systems.inbox.v2;
return {
type: { ...svc().type, checkAuth: (...args) => this.checkAuth(...args) },
subType: { ...svc().subType, checkAuth: (...args) => this.checkAuth(...args) },
keyword: { ...svc().keyword, checkAuth: (...args) => this.checkAuth(...args) },
numberRule: { ...svc().numberRule, checkAuth: (...args) => this.checkAuth(...args) },
function: { ...svc().function, checkAuth: (...args) => this.checkAuth(...args) },
reply: { ...svc().reply, checkAuth: (...args) => this.checkAuth(...args) },
relation: {
function: { ...svc().relation.function, checkAuth: (...args) => this.checkAuth(...args) },
rule: { ...svc().relation.rule, checkAuth: (...args) => this.checkAuth(...args) },
group: { ...svc().relation.group, 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, pagination, where } = data;
const warehouseId = Number(ctx.user?.store_id || 0);
return await ctx.service.systems.inbox.group.list({
warehouseId,
groupType,
keyword,
page,
pageSize,
pagination,
where,
});
}
/**
* 群快速查询(下拉选择用,不分页)。
* 双维度可选过滤,均不传时返回所有仓库/货主的群:
* - store_id:货主维度,透传为底层参数 storeId;
* - warehouse_id:仓库维度,透传为底层参数 warehouseId;
* - 两者可同时传(AND 关系),也可都不传(不加对应过滤条件)。
*
* @param {Object} data 请求数据。
* @param {string} [data.keyword] 关键词,模糊匹配群名称与外部群ID。
* @param {string} [data.groupType] 按群类型筛选:store / warehouse / express。
* @param {number} [data.limit=20] 返回条数上限,最大 100。
* @param {number|string} [data.store_id] 货主ID(可选,对应 wms_inbox_group.store_id 列)。
* @param {number|string} [data.warehouse_id] 仓库ID(可选,对应 wms_inbox_group.warehouse_id 列)。
* @return {Promise<Object>} { list: 群快速列表 }。
*/
async groupQuickList(data = {}) {
const { ctx } = this;
const {
keyword, groupType, limit,
store_id: storeId,
warehouse_id: warehouseId,
} = data;
return await ctx.service.systems.inbox.group.quickList({
storeId,
warehouseId,
keyword,
groupType,
limit,
});
}
/**
* 查询一个或多个群的成员列表(下拉选择用,多群聚合)。
* warehouseId 从 ctx.user.store_id 自动注入,不信任前端传值。
*
* @param {Object} data 请求数据。
* @param {Array<number|string>} data.groupIds 群内部主键ID数组(必填,非空且每个元素为正整数)。
* @param {string} [data.keyword] 关键词,模糊匹配 user_name / user_id。
* @param {number} [data.limit=20] 返回条数上限,最大 100。
* @return {Promise<Object>} { list: 群成员列表 }。
*/
async groupMembers(data = {}) {
const { ctx } = this;
const { groupIds, keyword, limit } = data;
const warehouseId = Number(ctx.user?.store_id || 0);
return await ctx.service.systems.inbox.group.members({
groupIds,
warehouseId,
keyword,
limit,
});
}
/**
* 禁用 / 解禁群。
* 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 / ai_enabled / template_reply_enabled。
* 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 时必填)。
* @param {number} [data.aiEnabled] 是否启用AI自动回复: 0=关闭 1=开启。
* @param {number} [data.templateReplyEnabled] 是否启用模板消息回复: 0=关闭(普通文本回复) 1=开启(模板消息回复)。
* @return {Promise<Object>} { message: 操作结果 }。
*/
async groupUpdate(data = {}) {
const { ctx } = this;
const {
id, groupType, subType, storeId,
expressInterfaces, systemExpressCode, aiEnabled,
templateReplyEnabled,
} = data;
const warehouseId = Number(ctx.user?.store_id || 0);
await ctx.service.systems.inbox.group.update({
id,
warehouseId,
groupType,
subType,
storeId,
expressInterfaces,
systemExpressCode,
aiEnabled,
templateReplyEnabled,
});
return { message: '群信息修改成功' };
}
/**
* 清空群类型配置(入参主键ID或外部群ID)。
* 清空 group_type / sub_type / store_id / 快递专属字段,AI 开关重置关闭,
* 仅保留群创建时信息。warehouseId 从 ctx.user.store_id 自动注入。
*
* @param {Object} data 请求数据。
* @param {number} data.id 群记录主键ID(必填)。
* @return {Promise<Object>} { message: 操作结果 }。
*/
async groupReset(data = {}) {
const { ctx } = this;
const { id } = data;
const warehouseId = Number(ctx.user?.store_id || 0);
await ctx.service.systems.inbox.group.reset({ id, warehouseId });
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,
});
}
/**
* 手动重推失败(push_status=3)的站内信消息。
* 兼容旧版 inbox.retry 路径,与 inbox.message.retry 等价。
* warehouseId 从 ctx.user.store_id 自动注入。
*
* @param {Object} data 请求数据。
* @param {number} data.id 消息ID(必填)。
* @return {Promise<Object>} { ok, id, ext_id, message }。
*/
async retry(data = {}) {
const { ctx } = this;
const warehouseId = Number(ctx.user?.store_id || 0);
return await ctx.service.inbox.message.retry({ warehouseId, id: data.id });
}
/**
* 系统回信:写入 outbound 记录。
* 兼容旧版 inbox.reply 路径,与 inbox.message.reply 等价。
* warehouseId 从 ctx.user.store_id 自动注入。
*
* @param {Object} data 请求数据。
* @param {number} data.parentId 原消息ID(必填)。
* @param {string} [data.content] 回信正文。
* @param {Object} [data.extData] 透传数据。
* @param {Object} [data.flags] 处理标记。
* @return {Promise<Object>} 回信结果。
*/
async reply(data = {}) {
const { ctx } = this;
const warehouseId = Number(ctx.user?.store_id || 0);
return await ctx.service.inbox.index.reply({ warehouseId, ...data });
}
}
module.exports = InboxSystemService;