From 064578f294f280db669e182dc0c0d8a99b448158 Mon Sep 17 00:00:00 2001 From: Seven <1530011482@qq.com> Date: Tue, 29 Sep 2026 17:01:05 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8A=8A=E5=8F=91=E6=B6=88=E6=81=AF=E5=86=99?= =?UTF-8?q?=E5=85=A5=E6=95=B0=E6=8D=AE=E5=BA=93=E7=9A=84=E6=97=B6=E9=97=B4?= =?UTF-8?q?=E9=83=BD=E4=BA=A4=E7=BB=99=E6=95=B0=E6=8D=AE=E5=BA=93=E6=9D=A5?= =?UTF-8?q?=E5=88=9B=E5=BB=BA=EF=BC=8C=E5=8F=AA=E8=AF=BB=E5=8F=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/pages/index/ChatPage.vue | 48 +++++++++++++++++------------------- 1 file changed, 22 insertions(+), 26 deletions(-) diff --git a/src/pages/index/ChatPage.vue b/src/pages/index/ChatPage.vue index 4096263..5c97817 100644 --- a/src/pages/index/ChatPage.vue +++ b/src/pages/index/ChatPage.vue @@ -296,7 +296,6 @@ export default { created() { this.getCurrentUser(); - this.getRecentMessageList(); this.createWebsocket(); }, @@ -324,6 +323,7 @@ export default { getUserInfo() .then((response) => { this.currentUser = response.data; + this.getRecentMessageList(); }) .catch((error) => { console.error(error); @@ -333,7 +333,7 @@ export default { // 获取最近消息列表 getRecentMessageList() { - if(!this.currentUser) return; + if (!this.currentUser) return; getRecentMessages() .then((response) => { this.recentChats = response.data.recentChats || []; @@ -435,31 +435,10 @@ export default { senderName: this.currentUser.username, receiverName: this.currentContact.contactName, content: content, - createdAt: new Date(), }; try { sendWebSocketMessage(newMessage); - this.contactMessages.push({ - ...newMessage, - from: "me", - createdAt: newMessage.createdAt, - time: newMessage.createdAt, - }); - - // 更新最近消息列表 - this.updateRecentChat( - "send", - this.currentContact.contactName, - newMessage - ); - console.log("contactMessages:" + this.contactMessages); - - // 清空输入框 - this.messageText = ""; - this.$nextTick(() => { - this.scrollToBottom(); - }); } catch (error) { // 清空输入框 this.messageText = ""; @@ -539,7 +518,7 @@ export default { // 给发消息的人返回 ACK sendWebSocketMessage({ type: "ack", - messageId: message.messageId + messageId: message.messageId, }); const currentContactName = @@ -558,14 +537,31 @@ export default { const contactIndex = this.contactList.findIndex( (item) => item.contactName === senderName ); - if(contactIndex === -1) return; - + if (contactIndex === -1) return; + this.updateRecentChat("receive", senderName, message); } this.$nextTick(() => { this.scrollToBottom(); }); + + // 发送新消息成功之后,需要把新消息显示到聊天框中(由于要拿数据库里的写入时间,所以要等websocket返回成功再刷新) + } else if (data.type == "sendSuccess") { + const message = data.message; + this.contactMessages.push({ + ...message, + from: "me", + time: message.createdAt, + }); + + // 更新最近消息列表 + this.updateRecentChat("send", this.currentContact.contactName, message); + // 清空输入框 + this.messageText = ""; + this.$nextTick(() => { + this.scrollToBottom(); + }); } else if (data.type == "sendError") { this.$message.error("发送失败,请重试"); this.$nextTick(() => {