|
|
|
@ -1,3 +1,5 @@
|
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
|
|
const { sessionStore } = require('../../core'); // Import from core/index.js
|
|
|
|
|
const { createWhatsApp } = require('../../core/baileys'); // Import from core/index.js
|
|
|
|
|
const { getConnection } = require('../../services/connections.service');
|
|
|
|
@ -7,27 +9,15 @@ const { domain, name: domainName } = require('../../config').server;
|
|
|
|
|
exports.newConnect = async ctx => {
|
|
|
|
|
try {
|
|
|
|
|
const { phone } = ctx.query;
|
|
|
|
|
const findSession = await getConnection({ sesson_id: phone, status: ['open', 'connecting'] }); // connect_domain: domain
|
|
|
|
|
// // todo: 只有一条
|
|
|
|
|
if (!isEmpty(findSession)) {
|
|
|
|
|
const { sesson_id: sessionId, status, ...connection } = findSession[0];
|
|
|
|
|
// if (['open', 'connecting'].includes(status)) {
|
|
|
|
|
// const sock = sessionStore.getSession(sessionId);
|
|
|
|
|
// if (!isEmpty(sock)) {
|
|
|
|
|
// return sock;
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
// return { qr: '', phone, sessionId, ...connection }; // 返回已登录
|
|
|
|
|
const existsSession = sessionStore.getSession(phone);
|
|
|
|
|
if (!isEmpty(existsSession)) {
|
|
|
|
|
return { phone, session_id: phone, ...existsSession, stauts: 'open', wai_server: domain };
|
|
|
|
|
}
|
|
|
|
|
const whatsApp1 = await createWhatsApp(phone);
|
|
|
|
|
const existsSession = sessionStore.getSession(phone);
|
|
|
|
|
let qr;
|
|
|
|
|
// if (isEmpty(existsSession)) {
|
|
|
|
|
qr = await whatsApp1.start();
|
|
|
|
|
const { sessionId } = sessionStore.createSession(phone, whatsApp1);
|
|
|
|
|
// }
|
|
|
|
|
ctx.assert(whatsApp1, 503, 'No available connections');
|
|
|
|
|
return { qr, phone, session_id: phone, wai_server: domainName };
|
|
|
|
|
whatsApp1.start();
|
|
|
|
|
sessionStore.createSession(phone, whatsApp1);
|
|
|
|
|
return { phone, session_id: phone, wai_server: domain, ...whatsApp1 };
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error('create connection error', error);
|
|
|
|
|
ctx.assert(null, 500, 'Failed to create connection or generate QR code.');
|
|
|
|
|