把发消息写入数据库的时间都交给数据库来创建,只读取

This commit is contained in:
2026-09-29 17:01:05 +08:00
parent 88cf0abc96
commit 064578f294
+21 -25
View File
@@ -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,7 +537,7 @@ export default {
const contactIndex = this.contactList.findIndex(
(item) => item.contactName === senderName
);
if(contactIndex === -1) return;
if (contactIndex === -1) return;
this.updateRecentChat("receive", senderName, message);
}
@@ -566,6 +545,23 @@ export default {
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(() => {