refactor(websocket): 优化websocket消息处理及状态检查逻辑

移除调试日志输出并优化微信账号状态检查逻辑
添加深拷贝工具函数并应用于客服列表处理
将状态检查改为间隔10秒的轮询方式
This commit is contained in:
超级老白兔
2025-09-02 10:18:25 +08:00
parent 20658c3ca5
commit e7b795f744
6 changed files with 54 additions and 12 deletions

View File

@@ -16,6 +16,7 @@ const VerticalUserList: React.FC = () => {
};
const kfUserList = useCkChatStore(state => state.kfUserList);
const kfSelected = useCkChatStore(state => state.kfSelected);
return (
<div className={styles.verticalUserList}>
<div

View File

@@ -32,12 +32,14 @@ const CkboxPage: React.FC = () => {
const kfUserList = useCkChatStore(state => state.kfUserList);
const { sendCommand } = useWebSocketStore.getState();
useEffect(() => {
if (status == "connected") {
if (status == "connected" && kfUserList.length > 0) {
//获取UserId
sendCommand("CmdRequestWechatAccountsAliveStatus", {
wechatAccountIds: kfUserList.map(v => v.id),
seq: +new Date(),
});
setInterval(() => {
sendCommand("CmdRequestWechatAccountsAliveStatus", {
wechatAccountIds: kfUserList.map(v => v.id),
seq: +new Date(),
});
}, 10 * 1000);
}
}, [status]);

View File

@@ -111,7 +111,6 @@ export const initSocket = () => {
const accountId = getAccountId();
// 使用WebSocket store初始化连接
const { connect } = useWebSocketStore.getState();
console.log("发起链接", Token, accountId);
// 连接WebSocket
connect({

View File

@@ -27,10 +27,9 @@ export const useCkChatStore = createPersistStore<CkChatState>(
// await kfUserService.createManyWithServerId(data);
},
// 获取客服列表
getkfUserList: async () => {
getkfUserList: () => {
const state = useCkChatStore.getState();
return state.kfUserList;
// return await kfUserService.findAll();
},
// 异步设置标签列表
asyncCountLables: async (data: ContactGroupByLabel[]) => {

View File

@@ -1,4 +1,5 @@
//消息管理器
import { deepCopy } from "@/utils/common";
import { WebSocketMessage } from "./websocket";
import { getkfUserList, asyncKfUserList } from "@/store/module/ckchat/ckchat";
// 消息处理器类型定义
@@ -8,9 +9,9 @@ type MessageHandler = (message: WebSocketMessage) => void;
const messageHandlers: Record<string, MessageHandler> = {
// 微信账号存活状态响应
CmdRequestWechatAccountsAliveStatusResp: message => {
console.log("微信账号存活状态响应", message);
// console.log("微信账号存活状态响应", message);
// 获取客服列表
const kfUserList = getkfUserList();
const kfUserList = deepCopy(getkfUserList());
const wechatAccountsAliveStatus = message.wechatAccountsAliveStatus || {};
// 遍历客服列表,更新存活状态
kfUserList.forEach(kfUser => {
@@ -59,8 +60,6 @@ export const getRegisteredMessageTypes = (): string[] => {
// 消息管理核心函数
export const msgManageCore = (message: WebSocketMessage) => {
console.log("获取消息---------------------");
const cmdType = message.cmdType;
if (!cmdType) {
console.warn("消息缺少cmdType字段", message);

View File

@@ -113,3 +113,45 @@ export function getSafeAreaHeight() {
// 3. 默认值
return 0;
}
/**
* 深拷贝函数支持对象、数组、Date、RegExp等类型
* @param obj 要拷贝的对象
* @returns 深拷贝后的对象
*/
export function deepCopy<T>(obj: T): T {
// 处理 null 和 undefined
if (obj === null || obj === undefined) {
return obj;
}
// 处理基本数据类型
if (typeof obj !== "object") {
return obj;
}
// 处理 Date 对象
if (obj instanceof Date) {
return new Date(obj.getTime()) as T;
}
// 处理 RegExp 对象
if (obj instanceof RegExp) {
return new RegExp(obj.source, obj.flags) as T;
}
// 处理 Array
if (Array.isArray(obj)) {
return obj.map(item => deepCopy(item)) as T;
}
// 处理普通对象
const clonedObj = {} as T;
for (const key in obj) {
if (Object.prototype.hasOwnProperty.call(obj, key)) {
clonedObj[key] = deepCopy(obj[key]);
}
}
return clonedObj;
}