From 8771bc3a3e5042cebd375e19372d01baa66f900f Mon Sep 17 00:00:00 2001 From: prr <3099672575@qq.com> Date: Thu, 7 Nov 2024 17:37:55 +0800 Subject: [PATCH] =?UTF-8?q?bug=EF=BC=9A=E7=B1=BB=E5=9E=8B=E6=9C=AA?= =?UTF-8?q?=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- frontend/src/stores/chat.ts | 56 +++++++++++++++++++++++++++++++------ frontend/src/stores/db.ts | 2 +- 2 files changed, 48 insertions(+), 10 deletions(-) diff --git a/frontend/src/stores/chat.ts b/frontend/src/stores/chat.ts index e7f8535..49c3a59 100644 --- a/frontend/src/stores/chat.ts +++ b/frontend/src/stores/chat.ts @@ -216,8 +216,6 @@ export const useChatStore = defineStore('chatStore', () => { const sendMessage = async (messageType: string) => { - - console.log("messageType", messageType) if (messageType == "text" && message.value.trim() == '') { return } @@ -279,6 +277,7 @@ export const useChatStore = defineStore('chatStore', () => { ...Message, isMe: true, message: await getImageSrc(Message.message), + previewMessage: "[图片消息]", previewTimeFormat: formatTime(Date.now()), displayName: userInfo.value.nickname, chatId: Message.type === 'group' ? targetGroupInfo.value.chatId : targetUserInfo.value.chatId, @@ -292,6 +291,7 @@ export const useChatStore = defineStore('chatStore', () => { await db.addOne("workbenchGroupChatRecord", messageHistory); } else if (Message.type === 'user') { await db.addOne("workbenchChatRecord", messageHistory); + updateUserSessionList(messageHistory.previewMessage as string) } chatHistory.value.push(messageHistory); fileSendActive.value = true; @@ -357,6 +357,7 @@ export const useChatStore = defineStore('chatStore', () => { await db.addOne("workbenchGroupChatRecord", messageHistory); } else if (Message.type === 'user') { await db.addOne("workbenchChatRecord", messageHistory); + updateUserSessionList(messageHistory.previewMessage as string) } chatHistory.value.push(messageHistory); fileSendActive.value = true; @@ -405,6 +406,7 @@ export const useChatStore = defineStore('chatStore', () => { messageHistory = { ...Message, isMe: true, + previewMessage: message.value, previewTimeFormat: formatTime(Date.now()), displayName: userInfo.value.nickname, chatId: targetUserInfo.value.chatId.toString(), @@ -412,16 +414,20 @@ export const useChatStore = defineStore('chatStore', () => { createdAt: Date.now() } await db.addOne("workbenchChatRecord", messageHistory); + // 更新sessionlist表的字段 + updateUserSessionList(messageHistory.message) } else if (Message.type === 'group') { messageHistory = { ...Message, + previewMessage: message.value, previewTimeFormat: formatTime(Date.now()), displayName: targetGroupInfo.value.name, isMe: true, chatId: targetGroupInfo.value.chatId.toString(), createdAt: Date.now() } + await db.addOne("workbenchGroupChatRecord", messageHistory); } @@ -429,18 +435,30 @@ export const useChatStore = defineStore('chatStore', () => { chatHistory.value.push(messageHistory); // 清空输入框 clearMessage(); - messageSendStatus.value = true } } + const updateUserSessionList = async (message: string) => { + // 更新sessionlist表的字段 + const sessionList = await db.getByField("workbenchSessionList", "chatId", targetUserInfo.value.chatId) + await db.update("workbenchSessionList", sessionList[0].id, { + previewMessage: message, + previewTimeFormat: formatTime(Date.now()) + }) + + initChatList() + } + + + // 更新聊天和聊天记录 const changeChatListAndChatHistory = async (data: any) => { // 从 conversationList 数据库中查找是否存在对应的会话 - const conversation = await db.getByField("workbenchSessionList", 'userId', data.userId); + const conversation = await db.getByField("workbenchSessionList", 'chatId', data.userId); // 准备会话更新数据 const updatedConversation = { @@ -454,7 +472,7 @@ export const useChatStore = defineStore('chatStore', () => { displayName: data.userInfo.nickname, nickname: data.userInfo.nickname, time: Date.now(), - previewMessage: data.message, + previewMessage: data.previewMessage, previewTimeFormat: formatTime(Date.now()), // 时间格式化函数 createdAt: Date.now(), }; @@ -675,8 +693,6 @@ export const useChatStore = defineStore('chatStore', () => { if (isPresence[0].id !== data.userId) { return } - - console.log("data---------", data) // 添加消息记录 const addMessageHistory = { type: data.type, @@ -711,9 +727,28 @@ export const useChatStore = defineStore('chatStore', () => { await db.addOne("workbenchChatRecord", addMessageHistory) - console.log("添加成功") // 更新 chatList 和 conversationList表 changeChatListAndChatHistory(data) + + console.log(chatList.value[0].chatId) + console.log(data.toUserId) + + // // 找到chatlist中的对应项,更新previewMessage和previewTimeFormat + // const chatListIndex = chatList.value.findIndex((chat: { chatId: string; }) => chat.chatId === data.toUserId); + // console.log(chatListIndex) + // if (chatListIndex !== -1) { + // if (chatListIndex !== -1) { + // chatList.value[chatListIndex].previewMessage = addMessageHistory.previewMessage; + // chatList.value[chatListIndex].previewTimeFormat = addMessageHistory.previewTimeFormat; + // } + + // // 更新sessionlist表的字段 + // const sessionList = await db.getByField("workbenchSessionList", "chatId", targetUserInfo.value.chatId) + // await db.update("workbenchSessionList", sessionList[0].id, { + // previewMessage: message, + // previewTimeFormat: formatTime(Date.now()) + // }) + // 更改聊天记录 chatHistory.value.push(addMessageHistory) messageReceiveStatus.value = true @@ -1063,10 +1098,13 @@ export const useChatStore = defineStore('chatStore', () => { // 从groupList中删除 groupList.value = groupList.value.filter((group: any) => group.group_id !== group_id) await db.deleteByField("workbenchGroupUserList", "group_id", group_id) + // 获取群列表 + await getGroupList() + // 初始化聊天列表 + initChatList() targetGroupInfo.value = {} targetChatId.value = '' drawerVisible.value = false - initChatList() notifySuccess("退出群聊成功") } diff --git a/frontend/src/stores/db.ts b/frontend/src/stores/db.ts index 294cd02..888b539 100644 --- a/frontend/src/stores/db.ts +++ b/frontend/src/stores/db.ts @@ -30,7 +30,7 @@ dbInit.version(1).stores({ workbenchChatRecord: { addIndex: (arg0: string, arg1: (obj: { toUserId: any; }) => any) => void; }; }) => { // 手动添加索引 - tx.workbenchSessionList.addIndex('userId', (obj: { userId: any; }) => obj.userId); + tx.workbenchSessionList.addIndex('chatId', (obj: { chatId: any; }) => obj.chatId); tx.workbenchChatRecord.addIndex('toUserId', (obj: { toUserId: any; }) => obj.toUserId); tx.workbenchChatUser.addIndex('chatId', (obj: { chatId: any; }) => obj.chatId); tx.workbenchGroupChatRecord.addIndex('chatId', (obj: { chatId: any; }) => obj.chatId);