127 lines
3.6 KiB
JavaScript
127 lines
3.6 KiB
JavaScript
/**
|
|
* 聊天消息管理器
|
|
* @type {{currentRoomId: null, unreadCounts: {}, messageCache: {}, roomMessages: {}, enterRoom(*): void, leaveRoom(): void, getCurrentRoomId(): *, increaseUnreadCount(*): void, resetUnreadCount(*): void, getUnreadCount(*): *, getAllUnreadCount(): *, notifyUnreadChange(): void, cacheMessage(*, *): void, addMessageToRoom(*, *): void, getRoomMessages(*): *, handleIncomingMessage(*): void}}
|
|
*/
|
|
const ChatManager = {
|
|
// 当前房间ID
|
|
currentRoomId: null,
|
|
|
|
// 存储未读消息数 { roomId: count }
|
|
unreadCounts: {},
|
|
|
|
// 消息缓存 { roomId: [message1, message2, ...] }
|
|
messageCache: {},
|
|
|
|
// 房间消息存储 { roomId: [message1, message2, ...] }
|
|
roomMessages: {},
|
|
|
|
// 进入聊天室
|
|
enterRoom(roomId) {
|
|
this.currentRoomId = roomId;
|
|
uni.setStorageSync('currentRoomId', roomId);
|
|
console.log(`进入房间: ${roomId}`);
|
|
|
|
// 重置当前房间的未读消息
|
|
this.resetUnreadCount(roomId);
|
|
|
|
// 如果有缓存消息,则触发事件,然后清除缓存
|
|
if (this.messageCache[roomId]) {
|
|
const cachedMessages = this.messageCache[roomId];
|
|
cachedMessages.forEach(message => {
|
|
this.addMessageToRoom(roomId, message);
|
|
});
|
|
delete this.messageCache[roomId];
|
|
}
|
|
},
|
|
|
|
// 离开聊天室
|
|
leaveRoom() {
|
|
this.currentRoomId = null;
|
|
uni.removeStorageSync('currentRoomId');
|
|
console.log('离开当前房间');
|
|
},
|
|
|
|
// 获取当前房间ID
|
|
getCurrentRoomId() {
|
|
return this.currentRoomId || uni.getStorageSync('currentRoomId');
|
|
},
|
|
|
|
// 增加未读消息数
|
|
increaseUnreadCount(roomId) {
|
|
// 如果当前不在该房间,才增加未读计数
|
|
if (this.getCurrentRoomId() !== roomId) {
|
|
this.unreadCounts[roomId] = (this.unreadCounts[roomId] || 0) + 1;
|
|
this.notifyUnreadChange();
|
|
console.log(`房间 ${roomId} 未读消息 +1, 当前: ${this.unreadCounts[roomId]}`);
|
|
}
|
|
},
|
|
|
|
// 重置未读消息数
|
|
resetUnreadCount(roomId) {
|
|
if (this.unreadCounts[roomId]) {
|
|
this.unreadCounts[roomId] = 0;
|
|
this.notifyUnreadChange();
|
|
console.log(`重置房间 ${roomId} 未读消息数`);
|
|
}
|
|
},
|
|
|
|
// 获取未读消息数
|
|
getUnreadCount(roomId) {
|
|
return this.unreadCounts[roomId] || 0;
|
|
},
|
|
|
|
// 获取所有未读消息数
|
|
getAllUnreadCount() {
|
|
return Object.values(this.unreadCounts).reduce((sum, count) => sum + count, 0);
|
|
},
|
|
|
|
// 通知未读消息变化
|
|
notifyUnreadChange() {
|
|
uni.$emit('unread-message-update', {
|
|
total: this.getAllUnreadCount(),
|
|
byRoom: {...this.unreadCounts}
|
|
});
|
|
},
|
|
|
|
// 缓存消息(当收到消息但不在该房间时)
|
|
cacheMessage(roomId, message) {
|
|
if (!this.messageCache[roomId]) {
|
|
this.messageCache[roomId] = [];
|
|
}
|
|
this.messageCache[roomId].push(message);
|
|
},
|
|
|
|
// 添加消息到房间
|
|
addMessageToRoom(roomId, message) {
|
|
if (!this.roomMessages[roomId]) {
|
|
this.roomMessages[roomId] = [];
|
|
}
|
|
this.roomMessages[roomId].push(message);
|
|
},
|
|
|
|
// 获取房间消息
|
|
getRoomMessages(roomId) {
|
|
return this.roomMessages[roomId] || [];
|
|
},
|
|
|
|
// 处理收到的消息
|
|
handleIncomingMessage(message) {
|
|
const roomId = message.room_id;
|
|
const currentRoomId = this.getCurrentRoomId();
|
|
|
|
// 添加消息到房间
|
|
this.addMessageToRoom(roomId, message);
|
|
|
|
// 如果当前在消息所属的房间,直接显示
|
|
if (currentRoomId === roomId) {
|
|
// 通知聊天页面显示消息
|
|
uni.$emit('new-room-message', message);
|
|
} else {
|
|
// 不在当前房间,缓存消息并增加未读消息数
|
|
this.cacheMessage(roomId, message);
|
|
this.increaseUnreadCount(roomId);
|
|
}
|
|
}
|
|
};
|
|
|
|
export { ChatManager }; |