refactor(websocket): 优化websocket消息处理及状态检查逻辑
移除调试日志输出并优化微信账号状态检查逻辑 添加深拷贝工具函数并应用于客服列表处理 将状态检查改为间隔10秒的轮询方式
This commit is contained in:
@@ -16,6 +16,7 @@ const VerticalUserList: React.FC = () => {
|
|||||||
};
|
};
|
||||||
const kfUserList = useCkChatStore(state => state.kfUserList);
|
const kfUserList = useCkChatStore(state => state.kfUserList);
|
||||||
const kfSelected = useCkChatStore(state => state.kfSelected);
|
const kfSelected = useCkChatStore(state => state.kfSelected);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className={styles.verticalUserList}>
|
<div className={styles.verticalUserList}>
|
||||||
<div
|
<div
|
||||||
|
|||||||
@@ -32,12 +32,14 @@ const CkboxPage: React.FC = () => {
|
|||||||
const kfUserList = useCkChatStore(state => state.kfUserList);
|
const kfUserList = useCkChatStore(state => state.kfUserList);
|
||||||
const { sendCommand } = useWebSocketStore.getState();
|
const { sendCommand } = useWebSocketStore.getState();
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (status == "connected") {
|
if (status == "connected" && kfUserList.length > 0) {
|
||||||
//获取UserId
|
//获取UserId
|
||||||
sendCommand("CmdRequestWechatAccountsAliveStatus", {
|
setInterval(() => {
|
||||||
wechatAccountIds: kfUserList.map(v => v.id),
|
sendCommand("CmdRequestWechatAccountsAliveStatus", {
|
||||||
seq: +new Date(),
|
wechatAccountIds: kfUserList.map(v => v.id),
|
||||||
});
|
seq: +new Date(),
|
||||||
|
});
|
||||||
|
}, 10 * 1000);
|
||||||
}
|
}
|
||||||
}, [status]);
|
}, [status]);
|
||||||
|
|
||||||
|
|||||||
@@ -111,7 +111,6 @@ export const initSocket = () => {
|
|||||||
const accountId = getAccountId();
|
const accountId = getAccountId();
|
||||||
// 使用WebSocket store初始化连接
|
// 使用WebSocket store初始化连接
|
||||||
const { connect } = useWebSocketStore.getState();
|
const { connect } = useWebSocketStore.getState();
|
||||||
console.log("发起链接", Token, accountId);
|
|
||||||
|
|
||||||
// 连接WebSocket
|
// 连接WebSocket
|
||||||
connect({
|
connect({
|
||||||
|
|||||||
@@ -27,10 +27,9 @@ export const useCkChatStore = createPersistStore<CkChatState>(
|
|||||||
// await kfUserService.createManyWithServerId(data);
|
// await kfUserService.createManyWithServerId(data);
|
||||||
},
|
},
|
||||||
// 获取客服列表
|
// 获取客服列表
|
||||||
getkfUserList: async () => {
|
getkfUserList: () => {
|
||||||
const state = useCkChatStore.getState();
|
const state = useCkChatStore.getState();
|
||||||
return state.kfUserList;
|
return state.kfUserList;
|
||||||
// return await kfUserService.findAll();
|
|
||||||
},
|
},
|
||||||
// 异步设置标签列表
|
// 异步设置标签列表
|
||||||
asyncCountLables: async (data: ContactGroupByLabel[]) => {
|
asyncCountLables: async (data: ContactGroupByLabel[]) => {
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
//消息管理器
|
//消息管理器
|
||||||
|
import { deepCopy } from "@/utils/common";
|
||||||
import { WebSocketMessage } from "./websocket";
|
import { WebSocketMessage } from "./websocket";
|
||||||
import { getkfUserList, asyncKfUserList } from "@/store/module/ckchat/ckchat";
|
import { getkfUserList, asyncKfUserList } from "@/store/module/ckchat/ckchat";
|
||||||
// 消息处理器类型定义
|
// 消息处理器类型定义
|
||||||
@@ -8,9 +9,9 @@ type MessageHandler = (message: WebSocketMessage) => void;
|
|||||||
const messageHandlers: Record<string, MessageHandler> = {
|
const messageHandlers: Record<string, MessageHandler> = {
|
||||||
// 微信账号存活状态响应
|
// 微信账号存活状态响应
|
||||||
CmdRequestWechatAccountsAliveStatusResp: message => {
|
CmdRequestWechatAccountsAliveStatusResp: message => {
|
||||||
console.log("微信账号存活状态响应", message);
|
// console.log("微信账号存活状态响应", message);
|
||||||
// 获取客服列表
|
// 获取客服列表
|
||||||
const kfUserList = getkfUserList();
|
const kfUserList = deepCopy(getkfUserList());
|
||||||
const wechatAccountsAliveStatus = message.wechatAccountsAliveStatus || {};
|
const wechatAccountsAliveStatus = message.wechatAccountsAliveStatus || {};
|
||||||
// 遍历客服列表,更新存活状态
|
// 遍历客服列表,更新存活状态
|
||||||
kfUserList.forEach(kfUser => {
|
kfUserList.forEach(kfUser => {
|
||||||
@@ -59,8 +60,6 @@ export const getRegisteredMessageTypes = (): string[] => {
|
|||||||
|
|
||||||
// 消息管理核心函数
|
// 消息管理核心函数
|
||||||
export const msgManageCore = (message: WebSocketMessage) => {
|
export const msgManageCore = (message: WebSocketMessage) => {
|
||||||
console.log("获取消息---------------------");
|
|
||||||
|
|
||||||
const cmdType = message.cmdType;
|
const cmdType = message.cmdType;
|
||||||
if (!cmdType) {
|
if (!cmdType) {
|
||||||
console.warn("消息缺少cmdType字段", message);
|
console.warn("消息缺少cmdType字段", message);
|
||||||
|
|||||||
@@ -113,3 +113,45 @@ export function getSafeAreaHeight() {
|
|||||||
// 3. 默认值
|
// 3. 默认值
|
||||||
return 0;
|
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;
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user