diff --git a/src/config.js b/src/config.js index d3df70e..fe8ae0a 100644 --- a/src/config.js +++ b/src/config.js @@ -9,7 +9,9 @@ // export const API_HOST = 'http://202.103.68.144:8889/v2'; // export const WS_URL = 'ws://202.103.68.144:8889'; // export const EMAIL_HOST = 'http://202.103.68.231:888/service-mail'; -export const WAI_HOST = 'http://47.83.248.120/api/v1'; +// export const WAI_HOST = 'http://47.83.248.120/api/v1'; // 香港服务器 +export const WAI_HOST = 'http://47.254.53.81/api/v1'; // 美国服务器 + export const EMAIL_ATTA_HOST = 'https://p9axztuwd7x8a7.mycht.cn/attatchment'; // 邮件附件 // prod: // export const WAI_HOST = 'https://wai-server-qq4qmtq7wc9he4.mycht.cn/api/v1'; diff --git a/src/stores/AuthStore.js b/src/stores/AuthStore.js index 094b887..b73a106 100644 --- a/src/stores/AuthStore.js +++ b/src/stores/AuthStore.js @@ -31,7 +31,7 @@ const useAuthStore = create(devtools((set, get) => ({ const { loginUser } = get() if (perm === PERM_USE_WHATSAPP) { - return ['404', '383', '227'].includes(loginUser.userId) + return ['370', '143', '495', '404', '383', '227'].includes(loginUser.userId) } if (perm === PERM_USE_EMAL) { diff --git a/src/views/accounts/Profile.jsx b/src/views/accounts/Profile.jsx index b312b21..1e73a12 100644 --- a/src/views/accounts/Profile.jsx +++ b/src/views/accounts/Profile.jsx @@ -56,7 +56,7 @@ function Profile() { setTimeout(() => { // 模拟扫码登录成功,实际要请求后端状态来确定 setQRStatus('scanned') - }, 10000); + }, 50000); }) .catch(ex => { setQRStatus('expired') diff --git a/wai-server/config/components/log4j.config.js b/wai-server/config/components/log4j.config.js index c46902d..5408ec4 100644 --- a/wai-server/config/components/log4j.config.js +++ b/wai-server/config/components/log4j.config.js @@ -39,7 +39,8 @@ log4js.configure(logConfig); const logger = log4js.getLogger(); -// console.log = logger.info.bind(logger); -// console.error = logger.error.bind(logger); +console.log = logger.info.bind(logger); +console.info = logger.info.bind(logger); +console.error = logger.error.bind(logger); module.exports = logger; diff --git a/wai-server/core/baileys/common.js b/wai-server/core/baileys/helper.js similarity index 100% rename from wai-server/core/baileys/common.js rename to wai-server/core/baileys/helper.js diff --git a/wai-server/core/baileys/index.js b/wai-server/core/baileys/index.js index 757f978..d1189fc 100644 --- a/wai-server/core/baileys/index.js +++ b/wai-server/core/baileys/index.js @@ -12,7 +12,7 @@ const { const { writeFile } = require('fs/promises'); const waEmitter = require('../emitter'); -const { formatPhoneNumber, parsePhoneNumber, formatStatus, formatTimestamp } = require('./common'); +const { formatPhoneNumber, parsePhoneNumber, formatStatus, formatTimestamp } = require('./helper'); const generateId = require('../../utils/generateId.util'); const NodeCache = require('node-cache'); const P = require('pino'); @@ -51,31 +51,35 @@ const createWhatsApp = async phone => { const { version, isLatest } = await fetchLatestBaileysVersion(); const waVersion = version.join('.') + ', ' + (isLatest ? 'latest' : 'out'); - const sendTextMessage = async (whatsAppNo, content) => { - const number = formatPhoneNumber(whatsAppNo); + const sendTextMessage = async (number, content) => { + const jid = formatPhoneNumber(number); try { - await waSocket.sendMessage(number, { text: content }); + return await waSocket.sendMessage(jid, { text: content }); } catch (ex) { waEmitter.emit('message.error', { messge: '发送文本消息出错', + from: whatsAppNo, + to: number, error: ex }) - console.error('发送文本消息出错: ', ex); + console.error(`${whatsAppNo}-${number}发送文本消息出错: `, ex); } }; - const sendImageMessage = async (whatsAppNo, imageUrl) => { - const number = formatPhoneNumber(whatsAppNo); + const sendImageMessage = async (number, imageUrl) => { + const jid = formatPhoneNumber(number); try { - await waSocket.sendMessage(number, { + return await waSocket.sendMessage(jid, { image: { url: imageUrl }, }); } catch (ex) { waEmitter.emit('message.error', { messge: '发送图片消息出错', + from: whatsAppNo, + to: number, error: ex }) - console.error('发送图片消息出错: ', ex); + console.error(`${whatsAppNo}-${number}发送图片消息出错: `, ex); } }; @@ -94,8 +98,13 @@ const createWhatsApp = async phone => { if (upsert.type === 'notify') { for (const msg of upsert.messages) { - const messageType = Object.keys(msg.message)[0]; + // 没有类型的消息,先忽略 + if (!msg.message) { + continue; + } + + const messageType = Object.keys(msg.message)[0]; console.log('messageType', messageType); if (messageType === 'imageMessage') { // download the message @@ -130,7 +139,6 @@ const createWhatsApp = async phone => { if (msg.key.fromMe) { waEmitter.emit('message:updated', { id: msg.key.id, - //status: formatStatus(msg.update.status), status: formatStatus(msg.status), from: whatsAppNo, to: fromWhatsAppNo, @@ -145,6 +153,7 @@ const createWhatsApp = async phone => { id: parsePhoneNumber(msg.key.participant), name: msg.pushName, }, + eventSource: 'messages.upsert.notify', updateTime: formatTimestamp(msg.messageTimestamp), }); } else { @@ -165,6 +174,7 @@ const createWhatsApp = async phone => { id: parsePhoneNumber(msg.key.participant), name: msg.pushName, }, + eventSource: 'messages.upsert.notify', createTime: formatTimestamp(msg.messageTimestamp), }); } @@ -192,6 +202,7 @@ const createWhatsApp = async phone => { id: parsePhoneNumber(msg.participant), name: msg.pushName, }, + eventSource: 'messages.upsert.append', updateTime: formatTimestamp(msg.messageTimestamp), }); } @@ -216,6 +227,7 @@ const createWhatsApp = async phone => { id: parsePhoneNumber(msg.key.participant), name: msg.pushName, }, + eventSource: 'messages.updated', updateTime: formatTimestamp(new Date().getTime() / 1000), }); } @@ -228,7 +240,6 @@ const createWhatsApp = async phone => { logger, auth: { creds: state.creds, - /** caching makes the store faster to send/recv messages */ keys: makeCacheableSignalKeyStore(state.keys, logger), }, // https://github.com/WhiskeySockets/Baileys/blob/31bc8ab/src/Utils/generics.ts#L21 @@ -238,36 +249,26 @@ const createWhatsApp = async phone => { generateHighQualityLinkPreview: false, syncFullHistory: false, }); - - // 不绑定不会影响扫码登录 - store?.bind(waSocket.ev); - // something about the connection changed - // maybe it closed, or we received all offline message or connection opened waSocket.ev.on('connection.update', async update => { console.log('connection update: ', update); - const { connection, lastDisconnect, qr, isOnline } = update; + const { connection, lastDisconnect, qr } = update; if (connection === 'close') { - console.log('链接断开:', lastDisconnect); - if (lastDisconnect?.error?.output?.statusCode !== DisconnectReason.loggedOut) { - console.log('正在重连:'); - start(); - } else { - // waSocket.end(error => console.error('end.error: ', error)); - // waSocket.logout(msg => console.error('logout.msg: ', msg)); - console.log('Connection closed. You are logged out.'); + // waSocket.end(error => console.error('end.error: ', error)); + // waSocket.logout(msg => console.error('logout.msg: ', msg)); + console.log('Connection closed: ', lastDisconnect); - waEmitter.emit('connection:close', { - whatsAppNo, - status: 'offline', - }); - } + waEmitter.emit('connection:close', { + whatsAppNo, + status: 'offline', + }); } else if (connection === 'open') { waEmitter.emit('connection:open', { status: 'open', whatsAppNo }); } else if (qr !== undefined) { // WebSocket 创建成功等待扫码,如果没有扫码会更新 qr + // 第一次一分钟,后面是 20 秒更新一次 resolve(qr); } }); @@ -283,6 +284,9 @@ const createWhatsApp = async phone => { console.info('creds.update'); await saveCreds(); }); + + // 不绑定不会影响扫码登录 + store?.bind(waSocket.ev); }); }; diff --git a/wai-server/core/baileys/local.js b/wai-server/core/baileys/local.js new file mode 100644 index 0000000..eea48c0 --- /dev/null +++ b/wai-server/core/baileys/local.js @@ -0,0 +1,9 @@ +const { createWhatsApp } = require('./index'); + +const run = async () => { + const whatsApp = await createWhatsApp('18777396951'); + const qr = await whatsApp.start(); + console.info('qr: ', qr); +}; + +run();