Compare commits

..
5 Commits
2 changed files with 85 additions and 47 deletions
+57 -22
View File
@@ -226,13 +226,12 @@ import {
getContactInfo, getContactInfo,
getRecentMessages, getRecentMessages,
getContactMessages, getContactMessages,
sendMessage, // sendMessage,
} from "@/utils/api"; } from "@/utils/api";
import { import {
connectWebSocket, connectWebSocket,
// sendWebSocketMessage, sendWebSocketMessage,
closeWebSocket, closeWebSocket
// getWebSocket
} from "@/utils/websocket"; } from "@/utils/websocket";
export default { export default {
@@ -417,39 +416,46 @@ export default {
// 新消息 // 新消息
const newMessage = { const newMessage = {
type: "sendMessage",
messageId: Date.now() + "-" + Math.random().toString(36).slice(2),
senderName: this.currentUser.username, senderName: this.currentUser.username,
receiverName: this.currentContact.contactName, receiverName: this.currentContact.contactName,
content: content, content: content,
createdAt: new Date(), createdAt: new Date(),
}; };
sendMessage(newMessage) try {
.then((response) => { sendWebSocketMessage(newMessage);
console.log("发送消息成功", response.data);
this.contactMessages.push({ this.contactMessages.push({
...newMessage, ...newMessage,
from: "me", from: "me",
createdAt: newMessage.createdAt,
time: newMessage.createdAt,
}); });
// 更新最近消息列表 // 更新最近消息列表
this.updateRecentChat(this.currentContact.contactName, newMessage); this.updateRecentChat(
"send",
this.currentContact.contactName,
newMessage
);
console.log("contactMessages:" + this.contactMessages);
// 清空输入框 // 清空输入框
this.messageText = ""; this.messageText = "";
this.$nextTick(() => { this.$nextTick(() => {
this.scrollToBottom(); this.scrollToBottom();
}); });
}) } catch (error) {
.catch((error) => {
// 清空输入框 // 清空输入框
this.messageText = ""; this.messageText = "";
console.error(error); console.error(error);
this.$message.error("发送消息失败"); this.$message.error("发送消息失败");
}); }
}, },
// 更新最近消息列表 // 更新最近消息列表
updateRecentChat(contactName, newMessage) { updateRecentChat(messageType, contactName, newMessage) {
const index = this.recentChats.findIndex( const index = this.recentChats.findIndex(
(item) => item.contactName === contactName (item) => item.contactName === contactName
); );
@@ -460,9 +466,13 @@ export default {
chat.content = newMessage.content; chat.content = newMessage.content;
chat.createdAt = newMessage.createdAt; chat.createdAt = newMessage.createdAt;
// 未读数量 +1 // 接收非当前正在聊天的联系人新消息时,未读数量 +1
chat.unreadCount = if (
(chat.unreadCount || 0) + 1; messageType == "receive" &&
this.currentContact.contactName != contactName
) {
chat.unreadCount = (chat.unreadCount || 0) + 1;
}
this.recentChats.splice(index, 1); this.recentChats.splice(index, 1);
this.recentChats.unshift(chat); this.recentChats.unshift(chat);
@@ -473,13 +483,15 @@ export default {
contactName: contactName, contactName: contactName,
content: newMessage.content, content: newMessage.content,
createdAt: newMessage.createdAt, createdAt: newMessage.createdAt,
unreadCount: 1 unreadCount: 1,
}); });
} }
}, },
// 监听websocket连接成功事件
websocketOnOpen() {}, websocketOnOpen() {},
// 监听websocket接收消息事件
websocketOnMessage(data) { websocketOnMessage(data) {
// 格式如下: // 格式如下:
// { // {
@@ -493,10 +505,8 @@ export default {
// } // }
// } // }
if (data.type !== "newMessage") { // 当接收到新消息的时候,对消息显示进行处理,同时需要给websocket回一个ack确认收到的消息
return; if (data.type == "newMessage") {
}
const message = data.message; const message = data.message;
const senderName = message.senderName; const senderName = message.senderName;
@@ -505,18 +515,41 @@ export default {
// 是当前聊天联系人 // 是当前聊天联系人
if (senderName === currentContactName) { if (senderName === currentContactName) {
this.contactMessages.push(message); this.contactMessages.push({
...message,
from: "other",
time: message.createdAt,
});
} else { } else {
// 不是当前联系人,不加入 contactMessages,只更新最近聊天列表、未读数量 // 不是当前联系人,不加入 contactMessages,只更新最近聊天列表、未读数量
this.updateRecentChat(message); this.updateRecentChat("receive", senderName, message);
}
// 返回 ACK
sendWebSocketMessage({
type: "ack",
messageId: message.messageId
});
this.$nextTick(() => {
this.scrollToBottom();
});
} else if (data.type == "sendError") {
this.$message.error("发送失败,请重试");
this.$nextTick(() => {
this.scrollToBottom();
});
} else {
return;
} }
}, },
// 监听websocket连接报错事件
websocketOnErr() { websocketOnErr() {
this.$message.error("网络连接失败,请重试"); this.$message.error("网络连接失败,请重试");
this.logout(); this.logout();
}, },
// 创建websocket
createWebsocket() { createWebsocket() {
const token = this.$store.state.token; const token = this.$store.state.token;
const jwtToken = token.replace("Bearer ", ""); const jwtToken = token.replace("Bearer ", "");
@@ -881,6 +914,7 @@ export default {
.message-content { .message-content {
max-width: 60%; max-width: 60%;
margin: 0 10px; margin: 0 10px;
clear: both;
} }
.message-time { .message-time {
@@ -901,6 +935,7 @@ export default {
line-height: 1.5; line-height: 1.5;
word-break: break-word; word-break: break-word;
box-shadow: 0 1px 2px rgba(0, 0, 0, 0.05); box-shadow: 0 1px 2px rgba(0, 0, 0, 0.05);
float: right;
} }
.message-me .message-bubble { .message-me .message-bubble {
+6 -3
View File
@@ -43,10 +43,13 @@ const router = new VueRouter({
router.beforeEach((to, from, next) => { router.beforeEach((to, from, next) => {
const token = store.state.token; const token = store.state.token;
if(to.meta.requiresAuth) { if(to.meta.requiresAuth) {
if(token) { // 加入/login判断,防止vue-router报错
next() if(!token && to.path !== "/login") {
} else {
next('/login') next('/login')
return
} else {
next()
return
} }
} else { } else {
next() next()