'use strict'; /** * 站内信 IM 渠道来源工具。 * * 群ID 格式与渠道来源一一对应(生产 11 万+ 入站消息 100% 满足该对应关系): * - `xxx@chatroom` 结尾 → wx(个人微信) * - 纯数字(如 10845468709699144)→ qiwe(企业微信) * * ext_data.source 决定回执/通知透传给哪个桥接渠道;一旦与群ID格式不符, * 回执会被投递到错误渠道(典型现象:wx 群用 qiwe 渠道投递 → "qiwe offline" 503)。 */ /** 个人微信群ID 后缀 */ const WX_GROUP_ID_SUFFIX = '@chatroom'; /** * 按群ID格式推导渠道来源。 * @param {string} groupId 外部群ID。 * @return {string|null} wx / qiwe;无法判定时返回 null。 */ function resolveChannelSource(groupId) { const id = String(groupId || '').trim(); if (!id) return null; if (id.endsWith(WX_GROUP_ID_SUFFIX)) return 'wx'; if (/^\d+$/.test(id)) return 'qiwe'; return null; } /** * 按群ID格式校正 ext_data.source(不修改入参)。 * 仅在 ext_data 已带 source 且与群ID格式不符时返回新对象; * 无 ext_data / 无 source / 群ID无法判定 / 已一致时原样返回。 * @param {string} groupId 外部群ID。 * @param {Object|null} extData 原始透传数据。 * @return {Object|null} 校正后的透传数据。 */ function normalizeExtDataSource(groupId, extData) { if (!extData || typeof extData !== 'object' || Array.isArray(extData)) { return extData || null; } const expected = resolveChannelSource(groupId); const current = extData.source; if (!expected || !current || current === expected) { return extData; } return { ...extData, source: expected }; } module.exports = { WX_GROUP_ID_SUFFIX, resolveChannelSource, normalizeExtDataSource, };