304 lines
10 KiB
JavaScript
304 lines
10 KiB
JavaScript
'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),
|
||
};
|
||
}
|
||
|
||
/**
|
||
* 生成货主绑群码。
|
||
* 委托给 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。
|
||
* 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: '群信息修改成功' };
|
||
}
|
||
|
||
/**
|
||
* 清空群类型配置(入参主键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;
|