ming #1
54
app/lib/inbox_channel.js
Normal file
54
app/lib/inbox_channel.js
Normal file
@ -0,0 +1,54 @@
|
|||||||
|
'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,
|
||||||
|
};
|
||||||
@ -1,6 +1,7 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
const Service = require('egg').Service;
|
const Service = require('egg').Service;
|
||||||
|
const { normalizeExtDataSource } = require('../../lib/inbox_channel');
|
||||||
|
|
||||||
const WAREHOUSE_INTERFACE_TYPE_ID = 3;
|
const WAREHOUSE_INTERFACE_TYPE_ID = 3;
|
||||||
|
|
||||||
@ -46,13 +47,15 @@ class InboxProcessorService extends Service {
|
|||||||
};
|
};
|
||||||
|
|
||||||
// 群存在但 ext_data 为空,且入站消息带有 ext_data → 写回群配置
|
// 群存在但 ext_data 为空,且入站消息带有 ext_data → 写回群配置
|
||||||
|
// 写回前按群ID格式校正渠道来源,避免把错误渠道固化进群配置
|
||||||
if (group && !info.ext_data && msgExtData && typeof msgExtData === 'object' && Object.keys(msgExtData).length > 0) {
|
if (group && !info.ext_data && msgExtData && typeof msgExtData === 'object' && Object.keys(msgExtData).length > 0) {
|
||||||
try {
|
try {
|
||||||
|
const normalizedExt = normalizeExtDataSource(groupId, msgExtData);
|
||||||
await ctx.model.InboxGroup.update(
|
await ctx.model.InboxGroup.update(
|
||||||
{ ext_data: msgExtData },
|
{ ext_data: normalizedExt },
|
||||||
{ where: { group_id: groupId, warehouse_id: warehouseId } }
|
{ where: { group_id: groupId, warehouse_id: warehouseId } }
|
||||||
);
|
);
|
||||||
info.ext_data = msgExtData;
|
info.ext_data = normalizedExt;
|
||||||
ctx.logger.info(`[inbox processor] 群 ext_data 写回成功: ${groupId}`);
|
ctx.logger.info(`[inbox processor] 群 ext_data 写回成功: ${groupId}`);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
ctx.logger.warn(`[inbox processor] 群 ext_data 写回失败: ${e.message}`);
|
ctx.logger.warn(`[inbox processor] 群 ext_data 写回失败: ${e.message}`);
|
||||||
@ -489,6 +492,18 @@ class InboxProcessorService extends Service {
|
|||||||
throw new Error(`回执缺少必填字段: user_id=${userId || '?'}, user_name=${userName || '?'}`);
|
throw new Error(`回执缺少必填字段: user_id=${userId || '?'}, user_name=${userName || '?'}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 按群ID格式校正渠道来源(@chatroom→wx / 纯数字→qiwe):
|
||||||
|
// 群配置 ext_data 渠道错配时,回执会被桥接投递到错误渠道而丢失
|
||||||
|
const rawExtData = data.ext_data || null;
|
||||||
|
const outboundExtData = normalizeExtDataSource(groupId, rawExtData);
|
||||||
|
if (rawExtData && outboundExtData && rawExtData.source
|
||||||
|
&& rawExtData.source !== outboundExtData.source) {
|
||||||
|
ctx.logger.warn(
|
||||||
|
`[inbox processor] 回执 ext_data 渠道已校正 group_id=${groupId} ` +
|
||||||
|
`${rawExtData.source} → ${outboundExtData.source}`
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
const ackExtId = `sys:ack:${extId}`;
|
const ackExtId = `sys:ack:${extId}`;
|
||||||
const callbackConfig = await this.getApiAppCallbackConfig(warehouseId);
|
const callbackConfig = await this.getApiAppCallbackConfig(warehouseId);
|
||||||
try {
|
try {
|
||||||
@ -510,7 +525,7 @@ class InboxProcessorService extends Service {
|
|||||||
group_name: groupName,
|
group_name: groupName,
|
||||||
user_name: isSystem ? null : userName,
|
user_name: isSystem ? null : userName,
|
||||||
content: replyContent,
|
content: replyContent,
|
||||||
ext_data: data.ext_data || null, flags: {},
|
ext_data: outboundExtData, flags: {},
|
||||||
});
|
});
|
||||||
outboundId = created.id;
|
outboundId = created.id;
|
||||||
}
|
}
|
||||||
@ -524,7 +539,7 @@ class InboxProcessorService extends Service {
|
|||||||
data: {
|
data: {
|
||||||
type: isSystem ? 'system' : source === 'ai_reply' ? 'ai' : source === 'bind_confirm' ? 'bind' : source === 'balance' ? 'balance' : 'error',
|
type: isSystem ? 'system' : source === 'ai_reply' ? 'ai' : source === 'bind_confirm' ? 'bind' : source === 'balance' ? 'balance' : 'error',
|
||||||
content: replyContent,
|
content: replyContent,
|
||||||
ext_data: data.ext_data || null,
|
ext_data: outboundExtData,
|
||||||
ext_id: String(outboundId),
|
ext_id: String(outboundId),
|
||||||
group_id: groupId,
|
group_id: groupId,
|
||||||
...(isSystem ? {} : { user_id: userId, user_name: userName }),
|
...(isSystem ? {} : { user_id: userId, user_name: userName }),
|
||||||
|
|||||||
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
const { Service } = require('egg');
|
const { Service } = require('egg');
|
||||||
const crypto = require('crypto');
|
const crypto = require('crypto');
|
||||||
|
const { normalizeExtDataSource } = require('../../lib/inbox_channel');
|
||||||
|
|
||||||
/** 仓库接口类型 ID(interface_type_id=3) */
|
/** 仓库接口类型 ID(interface_type_id=3) */
|
||||||
const WAREHOUSE_INTERFACE_TYPE_ID = 3;
|
const WAREHOUSE_INTERFACE_TYPE_ID = 3;
|
||||||
@ -257,6 +258,8 @@ class WarehouseNotifyService extends Service {
|
|||||||
const { ctx } = this;
|
const { ctx } = this;
|
||||||
const groupId = String((group && group.group_id) || '').trim();
|
const groupId = String((group && group.group_id) || '').trim();
|
||||||
const groupName = String((group && group.group_name) || '').trim();
|
const groupName = String((group && group.group_name) || '').trim();
|
||||||
|
// 按群ID格式校正渠道来源(@chatroom→wx / 纯数字→qiwe),避免通知投递到错误渠道
|
||||||
|
const normalizedExtData = normalizeExtDataSource(groupId, extData);
|
||||||
// 成员ID规范化:仅接受字符串数组,去重并过滤空值(向后兼容:旧调用不传时为空数组)
|
// 成员ID规范化:仅接受字符串数组,去重并过滤空值(向后兼容:旧调用不传时为空数组)
|
||||||
const normalizedUserIds = Array.isArray(userIds)
|
const normalizedUserIds = Array.isArray(userIds)
|
||||||
? [ ...new Set(userIds.map(item => String(item || '').trim()).filter(Boolean)) ]
|
? [ ...new Set(userIds.map(item => String(item || '').trim()).filter(Boolean)) ]
|
||||||
@ -325,7 +328,7 @@ class WarehouseNotifyService extends Service {
|
|||||||
group_name: groupName || null,
|
group_name: groupName || null,
|
||||||
user_name: null,
|
user_name: null,
|
||||||
content: normalizedContent,
|
content: normalizedContent,
|
||||||
ext_data: extData || null,
|
ext_data: normalizedExtData,
|
||||||
flags: {},
|
flags: {},
|
||||||
push_status: 1,
|
push_status: 1,
|
||||||
push_started_at: new Date(),
|
push_started_at: new Date(),
|
||||||
@ -342,7 +345,7 @@ class WarehouseNotifyService extends Service {
|
|||||||
type: 'system',
|
type: 'system',
|
||||||
content: normalizedContent,
|
content: normalizedContent,
|
||||||
ext_id: sourceExtId,
|
ext_id: sourceExtId,
|
||||||
ext_data: extData || null,
|
ext_data: normalizedExtData,
|
||||||
timestamp: Math.floor(Date.now() / 1000),
|
timestamp: Math.floor(Date.now() / 1000),
|
||||||
};
|
};
|
||||||
if (groupId) {
|
if (groupId) {
|
||||||
|
|||||||
134
script/fix-inbox-group-channel.cjs
Normal file
134
script/fix-inbox-group-channel.cjs
Normal file
@ -0,0 +1,134 @@
|
|||||||
|
'use strict';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 修复站内信群表 / 消息表 ext_data.source 的渠道错配。
|
||||||
|
*
|
||||||
|
* 规则(生产 11 万+ 入站消息 100% 满足):
|
||||||
|
* - group_id 以 @chatroom 结尾 → source 应为 wx(个人微信)
|
||||||
|
* - group_id 为纯数字 → source 应为 qiwe(企业微信)
|
||||||
|
*
|
||||||
|
* 背景:群表 ext_data 的渠道来源会被回执/通知原样透传给桥接,
|
||||||
|
* 错配时回执投递到错误渠道(典型:wx 群按 qiwe 投递 → "qiwe offline" 503,回信丢失)。
|
||||||
|
*
|
||||||
|
* 用法:
|
||||||
|
* 开发库 dry-run: node script/fix-inbox-group-channel.cjs
|
||||||
|
* 开发库执行 : node script/fix-inbox-group-channel.cjs --apply
|
||||||
|
* 生产库 dry-run: $env:NODE_ENV='production'; node script/fix-inbox-group-channel.cjs
|
||||||
|
* 生产库执行 : $env:NODE_ENV='production'; node script/fix-inbox-group-channel.cjs --apply --messages
|
||||||
|
*
|
||||||
|
* 参数:
|
||||||
|
* --apply 实际写入(默认仅统计将修改的行数)
|
||||||
|
* --messages 同时修复 wms_inbox_message 历史记录(默认只修群表)
|
||||||
|
* --no-backup 跳过备份(默认写入前把待改行导出到 temp/ 便于回滚)
|
||||||
|
*
|
||||||
|
* 幂等:只改 source 取值与群ID格式不符的行,可重复执行。
|
||||||
|
*/
|
||||||
|
|
||||||
|
/** 条件:@chatroom 群却记了 qiwe */
|
||||||
|
const CHATROOM_AS_QIWE = "group_id LIKE '%@chatroom' AND JSON_UNQUOTE(JSON_EXTRACT(ext_data, '$.source')) = ?";
|
||||||
|
/** 条件:纯数字群却记了 wx */
|
||||||
|
const NUMERIC_AS_WX = "group_id REGEXP '^[0-9]+$' AND JSON_UNQUOTE(JSON_EXTRACT(ext_data, '$.source')) = ?";
|
||||||
|
|
||||||
|
(async () => {
|
||||||
|
const args = process.argv.slice(2);
|
||||||
|
const APPLY = args.includes('--apply');
|
||||||
|
const WITH_MESSAGES = args.includes('--messages');
|
||||||
|
const NO_BACKUP = args.includes('--no-backup');
|
||||||
|
|
||||||
|
try {
|
||||||
|
const fs = require('node:fs');
|
||||||
|
const path = require('node:path');
|
||||||
|
const configs = require('../database/config.js');
|
||||||
|
const env = process.env.NODE_ENV || 'development';
|
||||||
|
const cfg = configs[env];
|
||||||
|
if (!cfg) {
|
||||||
|
console.error(`NO_CONFIG_FOR_ENV: ${env}`);
|
||||||
|
process.exit(2);
|
||||||
|
}
|
||||||
|
|
||||||
|
const mysql = require('mysql2/promise');
|
||||||
|
const conn = await mysql.createConnection({
|
||||||
|
host: cfg.host,
|
||||||
|
port: cfg.port || 3306,
|
||||||
|
user: cfg.username,
|
||||||
|
password: cfg.password,
|
||||||
|
database: cfg.database,
|
||||||
|
});
|
||||||
|
|
||||||
|
console.log(`环境 : ${env}`);
|
||||||
|
console.log(`目标库 : ${cfg.host}:${cfg.port || 3306}/${cfg.database}`);
|
||||||
|
console.log(`模式 : ${APPLY ? 'APPLY(写入)' : 'DRY-RUN(只读)'}`);
|
||||||
|
console.log(`修复范围 : 群表${WITH_MESSAGES ? ' + 消息表(历史记录)' : ''}\n`);
|
||||||
|
|
||||||
|
const tasks = [
|
||||||
|
{ table: 'wms_inbox_group', label: '群 @chatroom→wx ', where: CHATROOM_AS_QIWE, from: 'qiwe', to: 'wx' },
|
||||||
|
{ table: 'wms_inbox_group', label: '群 数字→qiwe ', where: NUMERIC_AS_WX, from: 'wx', to: 'qiwe' },
|
||||||
|
];
|
||||||
|
if (WITH_MESSAGES) {
|
||||||
|
tasks.push(
|
||||||
|
{ table: 'wms_inbox_message', label: '消息 @chatroom→wx', where: CHATROOM_AS_QIWE, from: 'qiwe', to: 'wx' },
|
||||||
|
{ table: 'wms_inbox_message', label: '消息 数字→qiwe ', where: NUMERIC_AS_WX, from: 'wx', to: 'qiwe' }
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
const backupRows = [];
|
||||||
|
let total = 0;
|
||||||
|
|
||||||
|
for (const task of tasks) {
|
||||||
|
const sql = `SELECT id, group_id, group_name, ext_data, updated_at FROM ${task.table} WHERE ${task.where}`;
|
||||||
|
const [ rows ] = await conn.query(sql, [ task.from ]);
|
||||||
|
console.log(`[${task.label}] 待修复 ${rows.length} 行(source: ${task.from} → ${task.to})`);
|
||||||
|
total += rows.length;
|
||||||
|
for (const row of rows) {
|
||||||
|
backupRows.push({ table: task.table, id: row.id, group_id: row.group_id, ext_data: row.ext_data });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (total === 0) {
|
||||||
|
console.log('\n无需修复,数据已符合「@chatroom→wx / 纯数字→qiwe」。');
|
||||||
|
await conn.end();
|
||||||
|
process.exit(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
const backupFile = path.join(
|
||||||
|
__dirname, '..', 'temp',
|
||||||
|
`inbox-extdata-channel-backup-${new Date().toISOString().replace(/[:.]/g, '-')}.json`
|
||||||
|
);
|
||||||
|
if (APPLY && !NO_BACKUP) {
|
||||||
|
fs.mkdirSync(path.dirname(backupFile), { recursive: true });
|
||||||
|
fs.writeFileSync(backupFile, JSON.stringify(backupRows, null, 2), 'utf8');
|
||||||
|
console.log(`\n备份已写入: ${backupFile}(${backupRows.length} 行原值)`);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!APPLY) {
|
||||||
|
console.log('\nDRY-RUN 结束,未修改任何数据。确认无误后加 --apply 执行。');
|
||||||
|
await conn.end();
|
||||||
|
process.exit(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log('');
|
||||||
|
for (const task of tasks) {
|
||||||
|
const [ result ] = await conn.query(
|
||||||
|
`UPDATE ${task.table} SET ext_data = JSON_SET(ext_data, '$.source', ?) WHERE ${task.where}`,
|
||||||
|
[ task.to, task.from ]
|
||||||
|
);
|
||||||
|
console.log(`[${task.label}] 已更新 ${result.affectedRows} 行`);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 复核
|
||||||
|
console.log('\n复核(应全为 0):');
|
||||||
|
for (const task of tasks) {
|
||||||
|
const [ rows ] = await conn.query(
|
||||||
|
`SELECT COUNT(*) AS c FROM ${task.table} WHERE ${task.where}`,
|
||||||
|
[ task.from ]
|
||||||
|
);
|
||||||
|
console.log(` ${task.label} 剩余: ${rows[0].c}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
await conn.end();
|
||||||
|
process.exit(0);
|
||||||
|
} catch (err) {
|
||||||
|
console.error('ERROR', err.message);
|
||||||
|
process.exit(3);
|
||||||
|
}
|
||||||
|
})();
|
||||||
Loading…
x
Reference in New Issue
Block a user