移除未使用的客服列表相关代码,优化消息管理逻辑,新增消息状态管理功能,提升代码结构和可读性。

This commit is contained in:
超级老白兔
2025-10-23 12:55:57 +08:00
parent 81f225d9cb
commit 68a5350c19
5 changed files with 107 additions and 17 deletions

View File

@@ -27,8 +27,6 @@ const MessageList: React.FC<MessageListProps> = () => {
const kfSelected = useCkChatStore(state => state.kfSelected);
const { sendCommand } = useWebSocketStore();
const onContactClick = (session: ContractData | weChatGroup) => {
console.log(session);
setCurrentContact(session, true);
};
const [chatSessions, setChatSessions] = useState<

View File

@@ -52,11 +52,6 @@ export const chatInitAPIdata = async () => {
await asyncWeChatGroup(groupList);
// //获取控制终端列表
// const kfUserList: KfUserListData[] = await getCustomerList();
// //获取用户列表
// updateCustomerList(kfUserList);
//获取标签列表
const countLables = await getCountLables();
await asyncCountLables(countLables);
@@ -112,7 +107,6 @@ export const chatInitAPIdata = async () => {
return {
contractList,
groupList,
kfUserList,
};
} catch (error) {
console.error("获取联系人列表失败:", error);

View File

@@ -0,0 +1,39 @@
export interface Message {
id: number;
wechatId: string;
nickname: string;
alias: string;
avatar: string;
region: string;
signature: string;
bindQQ: string;
bindEmail: string;
bindMobile: string;
createTime: string;
currentDeviceId: number;
isDeleted: boolean;
deleteTime: string;
groupId: number;
memo: string;
wechatVersion: string;
labels: string[];
lastUpdateTime: string;
isOnline?: boolean;
momentsMax: number;
momentsNum: number;
[key: string]: any;
}
//Store State
export interface MessageState {
//消息列表
messageList: Message[];
//当前选中的消息
currentMessage: Message | null;
//更新消息列表
updateMessageList: (messageList: Message[]) => void;
//更新消息状态
updateMessageStatus: (messageId: number, status: string) => void;
//更新当前选中的消息
updateCurrentMessage: (message: Message) => void;
}

View File

@@ -0,0 +1,56 @@
import { create } from "zustand";
import { persist } from "zustand/middleware";
import { Message, MessageState } from "./message.data";
export const useMessageStore = create<MessageState>()(
persist(
(set, get) => ({
messageList: [], //消息列表
currentMessage: null, //当前选中的消息
updateMessageList: (messageList: Message[]) => set({ messageList }), //更新消息列表
updateCurrentMessage: (message: Message) =>
set({ currentMessage: message }), //更新当前选中的消息
updateMessageStatus: (messageId: number, status: string) =>
set({
messageList: get().messageList.map(message =>
message.id === messageId ? { ...message, status } : message,
),
}), //更新消息状态
}),
{
name: "message-storage",
partialize: state => ({
messageList: [],
currentMessage: null,
}),
},
),
);
/**
* 更新当前选中的消息
* @param message 消息
* @returns void
*/
export const updateCurrentMessage = (message: Message) =>
useMessageStore.getState().updateCurrentMessage(message);
/**
* 更新消息列表
* @param messageList 消息列表
* @returns void
*/
export const updateMessageList = (messageList: Message[]) =>
useMessageStore.getState().updateMessageList(messageList);
/**
* 获取当前选中的消息
* @returns Message | null
*/
export const getCurrentMessage = () =>
useMessageStore.getState().currentMessage;
/**
* 更新消息状态
* @param messageId 消息ID
* @param status 状态
* @returns void
*/
export const updateMessageStatus = (messageId: number, status: string) =>
useMessageStore.getState().updateMessageStatus(messageId, status);

View File

@@ -1,11 +1,11 @@
//消息管理器
import { getkfUserList, asyncKfUserList } from "@/store/module/ckchat/ckchat";
import { useWeChatStore } from "../weChat/weChat";
import { WebSocketMessage } from "./websocket";
import { deepCopy } from "@/utils/common";
import { Messages } from "./msg.data";
import { db } from "@/utils/db";
import { Modal } from "antd";
import { useCustomerStore, updateCustomerList } from "../weChat/customer";
// 消息处理器类型定义
type MessageHandler = (message: WebSocketMessage) => void;
const addMessage = useWeChatStore.getState().addMessage;
@@ -20,21 +20,24 @@ const messageHandlers: Record<string, MessageHandler> = {
CmdRequestWechatAccountsAliveStatusResp: message => {
// console.log("微信账号存活状态响应", message);
// 获取客服列表
const kfUserList = deepCopy(getkfUserList());
const customerList = deepCopy(useCustomerStore.getState().customerList);
const wechatAccountsAliveStatus = message.wechatAccountsAliveStatus || {};
// 遍历客服列表,更新存活状态
kfUserList.forEach(kfUser => {
kfUser.isOnline = wechatAccountsAliveStatus[kfUser.id];
});
// 遍历客服列表,更新在线状态
const updatedCustomerList = customerList.map(customer => ({
...customer,
isOnline: wechatAccountsAliveStatus[customer.id] || false,
}));
// 按在线状态排序,在线的排在前面
kfUserList.sort((a, b) => {
updatedCustomerList.sort((a, b) => {
if (a.isOnline && !b.isOnline) return -1;
if (!a.isOnline && b.isOnline) return 1;
return 0;
});
asyncKfUserList(kfUserList);
// 更新客服列表
updateCustomerList(updatedCustomerList);
},
// 发送消息响应
CmdSendMessageResp: message => {
@@ -57,7 +60,7 @@ const messageHandlers: Record<string, MessageHandler> = {
// 在这里添加具体的处理逻辑
receivedMsg(message.friendMessage || message.chatroomMessage);
},
CmdFriendInfoChanged: message => {
CmdFriendInfoChanged: () => {
// console.log("好友信息变更", message);
// 在这里添加具体的处理逻辑
},