perf: 二维码失效后退出 WA

dev/supplier-email-drawer
LiaoYijun 9 months ago
parent 526363ff2d
commit 03127e3910

@ -44,6 +44,7 @@ waEmitter.on('connection:close', event => {
const createWhatsApp = async phone => { const createWhatsApp = async phone => {
let waSocket = null; let waSocket = null;
let qrCode = null;
const channelId = generateId(); const channelId = generateId();
const whatsAppNo = phone; const whatsAppNo = phone;
const { state, saveCreds } = await useMultiFileAuthState('baileys_auth_info/' + phone + '_' + channelId); const { state, saveCreds } = await useMultiFileAuthState('baileys_auth_info/' + phone + '_' + channelId);
@ -254,22 +255,32 @@ const createWhatsApp = async phone => {
const { connection, lastDisconnect, qr } = update; const { connection, lastDisconnect, qr } = update;
if (connection === 'close') { if (connection === 'close') {
// waSocket.end(error => console.error('end.error: ', error)); if((lastDisconnect?.error)?.output?.statusCode !== DisconnectReason.loggedOut) {
// waSocket.logout(msg => console.error('logout.msg: ', msg)); start();
console.log('Connection closed: ', lastDisconnect); } else {
console.log('Connection closed: ', lastDisconnect);
waEmitter.emit('connection:close', { waEmitter.emit('connection:close', {
whatsAppNo, whatsAppNo,
status: 'offline', status: 'offline',
}); });
}
} else if (connection === 'open') { } else if (connection === 'open') {
// 扫码成功后向这个群发消息,后续就能使用 API 发送了。
sendTextMessage('120363363417115199@g.us', whatsAppNo + ' 扫码成功:' + new Date().toString());
waEmitter.emit('connection:open', { waEmitter.emit('connection:open', {
status: 'open', whatsAppNo status: 'open', whatsAppNo
}); });
} else if (qr !== undefined) { } else if (qr !== undefined) {
// WebSocket 创建成功等待扫码,如果没有扫码会更新 qr // WebSocket 创建成功等待扫码,如果没有扫码会更新 qr
// 第一次一分钟,后面是 20 秒更新一次 // 第一次一分钟,后面是 20 秒更新一次
resolve(qr); if (qrCode === null) {
qrCode = qr;
resolve(qr);
} else {
waSocket.logout(() => '二维码已过期');
}
} }
}); });

Loading…
Cancel
Save