perf: 请求登录qrcode; 全局提示状态

dev/supplier-email-drawer
Lei OT 6 months ago
parent d05709eae4
commit 7dc14871f1

@ -382,6 +382,16 @@ const whatsappMsgMapped = {
},
contentToRender: (contentObj) => null,
contentToUpdate: (msgcontent) => null,
contentToNotify: (contentObj) => {
return {
...contentObj,
status: contentObj?.status || '',
key: contentObj.to || '',
content: `WhatsApp号码: ${contentObj.to}`,
title: (contentObj.status === 'offline') ? `WhatsApp 断开连接` : '',
type: (contentObj.status === 'offline') ? 'warning' : 'info',
};
},
},
}
const emailMsgMapped = {

@ -0,0 +1,35 @@
import { useEffect } from 'react'
import { isEmpty } from '@/utils/commons'
import { App, notification } from 'antd'
import useConversationStore from '@/stores/ConversationStore'
export const useGlobalNotify = () => {
// const { notification } = App.useApp() // 在AntApp 中App.useApp() 获取不到notification
const [globalNotify, clearGlobalNotify] = useConversationStore((state) => [state.globalNotify, state.clearGlobalNotify])
useEffect(() => {
if (isEmpty(globalNotify)) {
return () => {}
}
// message.info(globalNotify[0].content, 3)
notification.open({
key: globalNotify[0].key,
message: globalNotify[0].title,
description: globalNotify[0].content,
duration: 6,
placement: 'top',
type: globalNotify[0].type,
onClick: () => {
clearGlobalNotify()
},
})
// setTimeout(() => {
// clearGlobalNotify()
// }, 3030)
return () => {}
}, [globalNotify])
return {};
}

@ -208,6 +208,10 @@ const websocketSlice = (set, get) => ({
].includes(resultType)) {
const _data = receivedMsgTypeMapped[resultType].getMsg(result);
setWai(_data)
if (['offline', 'close'].includes(_data.status)) {
const msgNotify = receivedMsgTypeMapped[resultType].contentToNotify(msgObj);
addGlobalNotify(msgNotify);
}
// setTimeout(() => {
// setWai({}); // 60s 后清空
// }, 60_000);

@ -25,6 +25,7 @@ import 'react-chat-elements/dist/main.css'
import EmailFetch from './Conversations/Online/Components/EmailFetch'
import FetchEmailWorker from './../workers/fetchEmailWorker?worker&url'
import { clearWebsocketLog, readWebsocketLog } from '@/utils/commons'
import { useGlobalNotify } from '@/hooks/useGlobalNotify'
// const fetchEmailWorkerURL = new URL('/src/workers/fetchEmailWorker.js', import.meta.url);
const fetchEmailWorker = new Worker(FetchEmailWorker, { type: 'module' });
@ -73,6 +74,8 @@ function AuthApp() {
}
}, [])
useGlobalNotify();
const startEmailInterval = (userId) => {
// const fetchEmailWorker = new Worker(fetchEmailWorkerURL, { type: 'module' });
fetchEmailWorker.onerror = function(error) {

@ -1,41 +1,13 @@
import { useEffect, useState } from 'react'
import { App, Tooltip, Button, FloatButton } from 'antd'
import { useState } from 'react'
import { FloatButton } from 'antd'
import { LoadingOutlined } from '@ant-design/icons'
import { getEmailFetchAction } from '@/actions/EmailActions'
import useAuthStore from '@/stores/AuthStore'
import useConversationStore from '@/stores/ConversationStore'
import { MailDownloadIcon } from '@/components/Icons'
import { isEmpty } from '@/utils/commons'
const EmailFetch = ({ ...props }) => {
const { notification, message } = App.useApp()
const { userId, emailList } = useAuthStore((state) => state.loginUser)
const [globalNotify, clearGlobalNotify] = useConversationStore((state) => [state.globalNotify, state.clearGlobalNotify]);
useEffect(() => {
if (isEmpty(globalNotify)) {
return () => {}
}
// message.info(globalNotify[0].content, 3)
notification.open({
key: globalNotify[0].key,
message: globalNotify[0].title,
description: globalNotify[0].content,
duration: 5,
placement: 'top',
type: globalNotify[0].type,
onClick: () => {
clearGlobalNotify()
},
})
// setTimeout(() => {
// clearGlobalNotify()
// }, 3030)
return () => {}
}, [globalNotify])
const [getEmailLoading, setEmailLoading] = useState(false)
const [fetchingText, setFetchingText] = useState('立即收件')
const handleGetEmail = async () => {

@ -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.');

@ -3,7 +3,8 @@ const whatsappEvents = require('../emitter');
module.exports = () => {
const sessions = new Map();
const createSession = (sessionId, ws) => {
const createSession = (sessionId, wsObj) => {
const { status, ...ws } = wsObj; // 不管status, 有session就是已连接
sessions.set(ws, sessionId);
whatsappEvents.emit('connection:connect', ws);
return { sessionId };

@ -105,13 +105,12 @@ const setupConnectionHandler = () => {
...objectMapper(connectionData, { whatsAppNo: [{ key: 'wa_id' }, { key: 'sesson_id' }], channelId: 'channel_id' }),
service_type: 'baileys',
},
{ connect_domain: domain, connect_name: domainName, channel_id: connectionData.channelId },
{ connect_domain: domain, connect_name: domainName },
);
// todo: 通知前端: 重新扫码
const webhookBody = webhookBodyBuilder({ ...connectionData, to: connectionData.whatsAppNo, connection: 'offline' }, 'creds:update');
await callWebhook(webhookBody);
} catch (error) {
logger.error({ connectionData, error }, 'error add connection');
logger.error({ connectionData, error }, 'error close connection');
}
});
// });

Loading…
Cancel
Save