227 lines
7.5 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 });
},
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,
});
}
/**
* 禁用 / 解禁群。
* 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,
});
}
/**
* 手动重推失败(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;