feat(weChat): 添加receivedMsg方法处理接收消息逻辑

新增receivedMsg方法用于处理微信消息接收逻辑,区分当前会话消息和未读消息更新
优化updateChatSession实现,使用深拷贝避免直接修改状态
调整ckchat.data.ts中方法顺序,保持代码整洁
This commit is contained in:
超级老白兔
2025-09-03 17:47:43 +08:00
parent ff96312d53
commit 533c16be3f
5 changed files with 40 additions and 19 deletions

View File

@@ -52,13 +52,13 @@ export interface CkChatState {
asyncKfUserList: (data: KfUserListData[]) => void;
getKfUserInfo: (wechatAccountId: number) => KfUserListData | undefined;
asyncContractList: (data: ContractData[]) => void;
getChatSessions: () => any[];
asyncChatSessions: (data: any[]) => void;
updateChatSession: (session: ContractData | weChatGroup) => void;
deleteCtrlUser: (userId: number) => void;
updateCtrlUser: (user: KfUserListData) => void;
clearkfUserList: () => void;
getChatSessions: () => any[];
addChatSession: (session: any) => void;
updateChatSession: (session: any) => void;
deleteChatSession: (sessionId: string) => void;
setUserInfo: (userInfo: CkUserInfo) => void;
clearUserInfo: () => void;

View File

@@ -9,6 +9,7 @@ import {
} from "@/pages/pc/ckbox/data";
import { kfUserService, weChatGroupService, contractService } from "@/utils/db";
import { createContractList } from "@/store/module/ckchat/api";
import { deepCopy } from "../../../utils/common";
export const useCkChatStore = createPersistStore<CkChatState>(
set => ({
@@ -384,20 +385,11 @@ export const useCkChatStore = createPersistStore<CkChatState>(
},
// 更新聊天会话
updateChatSession: (session: ContractData | weChatGroup) => {
set(state => ({
chatSessions: state.chatSessions.map(item =>
item.id === session.id ? session : item,
),
}));
// 清除getChatSessions缓存
const state = useCkChatStore.getState();
if (
state.getChatSessions &&
typeof state.getChatSessions === "function"
) {
// 触发缓存重新计算
state.getChatSessions();
}
const state = deepCopy(useCkChatStore.getState());
const newSession = state.chatSessions.map(item =>
item.id === session.id ? session : item,
);
set({ chatSessions: newSession });
},
// 删除聊天会话
deleteChatSession: (sessionId: string) => {

View File

@@ -18,4 +18,5 @@ export interface WeChatState {
setVideoLoading: (messageId: number, isLoading: boolean) => void;
setVideoUrl: (messageId: number, videoUrl: string) => void;
addMessage: (message: ChatRecord) => void;
receivedMsg: (message: ChatRecord) => void;
}

View File

@@ -4,7 +4,11 @@ import { getChatMessages } from "@/pages/pc/ckbox/api";
import { WeChatState } from "./weChat.data";
import { clearUnreadCount, updateConfig } from "@/pages/pc/ckbox/api";
import { ContractData, weChatGroup } from "@/pages/pc/ckbox/data";
import { addChatSession } from "@/store/module/ckchat/ckchat";
import {
addChatSession,
getChatSessions,
updateChatSession,
} from "@/store/module/ckchat/ckchat";
export const useWeChatStore = create<WeChatState>()(
persist(
@@ -68,6 +72,29 @@ export const useWeChatStore = create<WeChatState>()(
}));
},
receivedMsg: message => {
const currentContract = useWeChatStore.getState().currentContract;
if (
currentContract &&
currentContract.wechatAccountId == message.wechatAccountId &&
currentContract.id == message.wechatFriendId
) {
set(state => ({
currentMessages: [...state.currentMessages, message],
}));
} else {
//更新消息列表unread数值根据接收的++1 这样
const chatSessions = getChatSessions();
const session = chatSessions.find(
item => item.id == message.wechatFriendId,
);
session.unreadCount++;
console.log("新消息", session);
updateChatSession(session);
}
},
updateMessage: (messageId, updates) => {
set(state => ({
currentMessages: state.currentMessages.map(msg =>

View File

@@ -9,6 +9,8 @@ import { useWeChatStore } from "@/store/module/weChat/weChat";
type MessageHandler = (message: WebSocketMessage) => void;
const setVideoUrl = useWeChatStore.getState().setVideoUrl;
const addMessage = useWeChatStore.getState().addMessage;
const receivedMsg = useWeChatStore.getState().receivedMsg;
// 消息处理器映射
const messageHandlers: Record<string, MessageHandler> = {
// 微信账号存活状态响应
@@ -41,9 +43,8 @@ const messageHandlers: Record<string, MessageHandler> = {
},
//收到消息
CmdNewMessage: (message: Messages) => {
console.log("收到消息", message.friendMessage);
// 在这里添加具体的处理逻辑
addMessage(message.friendMessage);
receivedMsg(message.friendMessage);
},
// 登录响应