前端:
1、更新界面图标 2、修改消息缓存逻辑 3、新增清空未读消息数 后端: 1、修复不同时区导致的时间显示问题 2、新增清空未读消息数接口
This commit is contained in:
@@ -10,8 +10,20 @@ export const useChatStore = defineStore('chat', {
|
||||
pageSize: 20
|
||||
}),
|
||||
actions: {
|
||||
// 抽取统一的排序去重方法
|
||||
pushAndSortMessages(newMsgs) {
|
||||
const combined = [...this.messages, ...newMsgs];
|
||||
// 1. 根据 msgId 或唯一 key 去重
|
||||
const uniqueMap = new Map();
|
||||
combined.forEach(m => uniqueMap.set(m.msgId || m.id, m));
|
||||
|
||||
// 2. 转换为数组并按时间戳升序排序 (旧的在前,新的在后)
|
||||
this.messages = Array.from(uniqueMap.values()).sort((a, b) => {
|
||||
return new Date(a.timeStamp).getTime() - new Date(b.timeStamp).getTime();
|
||||
});
|
||||
},
|
||||
async addMessage(msg, sessionId) {
|
||||
await messagesDb.save({ ...msg, sessionId });
|
||||
await messagesDb.save({ ...msg, sessionId, isLoading: false });
|
||||
this.messages.push({ ...msg, sessionId })
|
||||
},
|
||||
/**
|
||||
@@ -32,13 +44,13 @@ export const useChatStore = defineStore('chat', {
|
||||
const serverHistoryMsg = await this.fetchHistoryFromServer(this.activeConversationId, conversation.lastReadMessageId);
|
||||
//对消息进行过滤,防止重复消息
|
||||
const filterMsg = serverHistoryMsg.filter(m => !this.messages.find(exist => exist.msgId === m.msgId));
|
||||
this.messages = [...filterMsg, ...this.messages]
|
||||
this.pushAndSortMessages([...filterMsg, ...this.messages]);
|
||||
}
|
||||
//拉取新消息
|
||||
this.fetchNewMsgFromServier(this.activeConversationId).then((newMsg) => {
|
||||
//去重
|
||||
const filterNewMsg = newMsg.filter(m => !this.messages.find(exist => exist.msgId === m.msgId));
|
||||
this.messages = [...filterNewMsg, ...this.messages]
|
||||
this.pushAndSortMessages([...filterNewMsg, ...this.messages])
|
||||
});
|
||||
},
|
||||
/**
|
||||
@@ -88,7 +100,7 @@ export const useChatStore = defineStore('chat', {
|
||||
} else {
|
||||
const fetchMsg = await this.fetchHistoryFromServer(this.conversationId, this.messages[0].msgId);
|
||||
const newMsgs = fetchMsg.filter(m => !this.messages.find(exist => exist.msgId === m.msgId));
|
||||
this.messages = [...newMsgs, ...this.messages]
|
||||
this.pushAndSortMessages([...newMsgs, ...this.messages])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user