把发消息写入数据库的时间都交给数据库来创建,只读取
This commit is contained in:
@@ -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);
|
||||||
@@ -333,7 +333,7 @@ export default {
|
|||||||
|
|
||||||
// 获取最近消息列表
|
// 获取最近消息列表
|
||||||
getRecentMessageList() {
|
getRecentMessageList() {
|
||||||
if(!this.currentUser) return;
|
if (!this.currentUser) return;
|
||||||
getRecentMessages()
|
getRecentMessages()
|
||||||
.then((response) => {
|
.then((response) => {
|
||||||
this.recentChats = response.data.recentChats || [];
|
this.recentChats = response.data.recentChats || [];
|
||||||
@@ -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 =
|
||||||
@@ -558,7 +537,7 @@ export default {
|
|||||||
const contactIndex = this.contactList.findIndex(
|
const contactIndex = this.contactList.findIndex(
|
||||||
(item) => item.contactName === senderName
|
(item) => item.contactName === senderName
|
||||||
);
|
);
|
||||||
if(contactIndex === -1) return;
|
if (contactIndex === -1) return;
|
||||||
|
|
||||||
this.updateRecentChat("receive", senderName, message);
|
this.updateRecentChat("receive", senderName, message);
|
||||||
}
|
}
|
||||||
@@ -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(() => {
|
||||||
|
|||||||
Reference in New Issue
Block a user