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

This commit is contained in:
2026-09-29 17:01:05 +08:00
parent 88cf0abc96
commit 064578f294
+19 -23
View File
@@ -296,7 +296,6 @@ export default {
created() { created() {
this.getCurrentUser(); this.getCurrentUser();
this.getRecentMessageList();
this.createWebsocket(); this.createWebsocket();
}, },
@@ -324,6 +323,7 @@ export default {
getUserInfo() getUserInfo()
.then((response) => { .then((response) => {
this.currentUser = response.data; this.currentUser = response.data;
this.getRecentMessageList();
}) })
.catch((error) => { .catch((error) => {
console.error(error); console.error(error);
@@ -435,31 +435,10 @@ export default {
senderName: this.currentUser.username, senderName: this.currentUser.username,
receiverName: this.currentContact.contactName, receiverName: this.currentContact.contactName,
content: content, content: content,
createdAt: new Date(),
}; };
try { try {
sendWebSocketMessage(newMessage); 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) { } catch (error) {
// 清空输入框 // 清空输入框
this.messageText = ""; this.messageText = "";
@@ -539,7 +518,7 @@ export default {
// 给发消息的人返回 ACK // 给发消息的人返回 ACK
sendWebSocketMessage({ sendWebSocketMessage({
type: "ack", type: "ack",
messageId: message.messageId messageId: message.messageId,
}); });
const currentContactName = const currentContactName =
@@ -566,6 +545,23 @@ export default {
this.$nextTick(() => { this.$nextTick(() => {
this.scrollToBottom(); 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") { } else if (data.type == "sendError") {
this.$message.error("发送失败,请重试"); this.$message.error("发送失败,请重试");
this.$nextTick(() => { this.$nextTick(() => {