diff --git a/app/service/inbox/message.js b/app/service/inbox/message.js index e5cdff0..d803526 100644 --- a/app/service/inbox/message.js +++ b/app/service/inbox/message.js @@ -399,6 +399,88 @@ class InboxMessageService extends Service { recent_24h_count: recentCount, }; } + + /** + * 手动重推失败(push_status=3)的站内信消息。 + * 重置为待推送状态(push_status=0)并触发异步推送。 + * @param {Object} params 重推参数。 + * @param {number} params.warehouseId 仓库ID(必填,服务端上下文注入)。 + * @param {number} params.id 消息ID。 + * @return {Promise} { ok, id, ext_id, message }。 + */ + async retry(params = {}) { + const { ctx } = this; + const { InboxMessage } = ctx.model; + + const warehouseId = Number(params.warehouseId); + const id = Number(params.id); + + if (!Number.isFinite(warehouseId) || warehouseId <= 0) { + throw new Error('warehouseId 无效'); + } + if (!Number.isFinite(id) || id <= 0) { + throw new Error('消息ID无效'); + } + + const record = await InboxMessage.findOne({ + where: { id, warehouse_id: warehouseId, deleted_at: null }, + }); + + if (!record) { + throw new Error('消息不存在或无权访问'); + } + if (record.push_status !== 3) { + throw new Error('仅失败状态的消息可以手动重推'); + } + + // 重置为待推送状态 + await InboxMessage.update( + { push_status: 0, push_response: null, push_started_at: null }, + { where: { id } } + ); + + // 回执记录 ext_id 形如 sys:ack:{原消息ext_id},反解出原消息 ext_id 以复用推送链路 + const extId = String(record.ext_id || ''); + const originalExtId = extId.startsWith('sys:ack:') ? extId.slice(8) : extId; + const hasUser = Boolean(record.user_id && record.user_name); + const pushData = { + warehouse_id: warehouseId, + ext_id: originalExtId, + parent_id: record.parent_id || null, + group_id: record.group_id || '', + group_name: record.group_name || '', + user_id: record.user_id || '', + user_name: record.user_name || '', + ext_data: record.ext_data || null, + }; + + // 触发异步推送 + setImmediate(() => { + ctx.service.inbox.processor.writeOutboundReply( + pushData, + record.content || '', + hasUser ? 'ai_reply' : 'system' + ).catch(async err => { + ctx.logger.error(`[inbox] 手动重推异步推送异常 id=${id}: ${err.message}`); + // 推送前校验失败时回写失败状态,避免消息卡在待推送 + try { + await InboxMessage.update( + { push_status: 3, push_response: { error: err.message } }, + { where: { id } } + ); + } catch (updateErr) { + ctx.logger.error(`[inbox] 重推失败状态回写异常 id=${id}: ${updateErr.message}`); + } + }); + }); + + return { + ok: true, + id: record.id, + ext_id: record.ext_id, + message: '已重置为待推送状态,正在异步重推', + }; + } } module.exports = InboxMessageService; diff --git a/app/service/systemMethods/inbox.js b/app/service/systemMethods/inbox.js index c0dccee..018de3c 100644 --- a/app/service/systemMethods/inbox.js +++ b/app/service/systemMethods/inbox.js @@ -44,6 +44,8 @@ class InboxSystemService extends MainService { 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), }; } @@ -186,6 +188,39 @@ class InboxSystemService extends MainService { pageSize, }); } + + /** + * 手动重推失败(push_status=3)的站内信消息。 + * 兼容旧版 inbox.retry 路径,与 inbox.message.retry 等价。 + * warehouseId 从 ctx.user.store_id 自动注入。 + * + * @param {Object} data 请求数据。 + * @param {number} data.id 消息ID(必填)。 + * @return {Promise} { 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} 回信结果。 + */ + 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;