diff --git a/im-uniapp/App.vue b/im-uniapp/App.vue index de5c4bc..6be74ca 100644 --- a/im-uniapp/App.vue +++ b/im-uniapp/App.vue @@ -116,8 +116,11 @@ export default { this.chatStore.refreshChats(); }).catch((e) => { console.log(e) - this.$message.error("拉取离线消息失败"); - this.onExit(); + uni.showToast({ + title: "拉取离线消息失败", + icon: "none" + }) + this.exit(); }) }, pullPrivateOfflineMessage(minId) { diff --git a/im-uniapp/store/chatStore.js b/im-uniapp/store/chatStore.js index 462dca5..3321ff6 100644 --- a/im-uniapp/store/chatStore.js +++ b/im-uniapp/store/chatStore.js @@ -356,15 +356,9 @@ export default defineStore('chatStore', { cacheChats.sort((chat1, chat2) => chat2.lastSendTime - chat1.lastSendTime); // #ifndef APP-PLUS /** - * 由于h5和小程序的stroge只有5m,大约只能存储2w条消息, - * 所以这里每个会话只保留1000条消息,防止溢出 + * 由于h5和小程序的stroge只有5m,大约只能存储2w条消息,所以可能需要清理部分历史消息 */ - cacheChats.forEach(chat => { - if (chat.messages.length > 1000) { - let idx = chat.messages.length - 1000; - chat.messages = chat.messages.slice(idx); - } - }) + this.fliterMessage(cacheChats, 10000, 1000); // #endif // 记录热数据索引位置 cacheChats.forEach(chat => chat.hotMinIdx = chat.messages.length); @@ -375,6 +369,21 @@ export default defineStore('chatStore', { // 消息持久化 this.saveToStorage(true); }, + fliterMessage(chats, maxTotalSize, maxPerChatSize) { + // 每个会话只保留maxPerChatSize条消息 + let remainTotalSize = 0; + chats.forEach(chat => { + if (chat.messages.length > maxPerChatSize) { + let idx = chat.messages.length - maxPerChatSize; + chat.messages = chat.messages.slice(idx); + } + remainTotalSize += chat.messages.length; + }) + // 保证消息总数不超过maxTotalSize条,否则继续清理 + if (remainTotalSize > maxTotalSize) { + this.fliterMessage(chats, maxTotalSize, maxPerChatSize / 2); + } + }, saveToStorage(withColdMessage) { // 加载中不保存,防止卡顿 if (this.loading) { diff --git a/im-web/src/store/chatStore.js b/im-web/src/store/chatStore.js index 238e8cd..a910c68 100644 --- a/im-web/src/store/chatStore.js +++ b/im-web/src/store/chatStore.js @@ -354,14 +354,11 @@ export default defineStore('chatStore', { cacheChats.sort((chat1, chat2) => chat2.lastSendTime - chat1.lastSendTime); /** * 由于部分浏览器不支持websql或indexdb,只能使用localstorage,而localstorage大小只有10m,可能会导致缓存空间溢出 - * 解决办法:如果是使用localstorage的浏览器,每个会话只保留1000条消息,防止溢出 + * 解决办法:针对只能使用localstorage的浏览器,最多保留1w条消息,每个会话最多保留1000条消息 */ - cacheChats.forEach(chat => { - if (localForage.driver().includes("localStorage") && chat.messages.length > 1000) { - let idx = chat.messages.length - 1000; - chat.messages = chat.messages.slice(idx); - } - }) + if (localForage.driver().includes("localStorage")) { + this.fliterMessage(cacheChats, 10000, 1000) + } // 记录热数据索引位置 cacheChats.forEach(chat => chat.hotMinIdx = chat.messages.length); // 将消息一次性装载回来 @@ -371,6 +368,21 @@ export default defineStore('chatStore', { // 持久化消息 this.saveToStorage(true); }, + fliterMessage(chats, maxTotalSize, maxPerChatSize) { + // 每个会话只保留maxPerChatSize条消息 + let remainTotalSize = 0; + chats.forEach(chat => { + if (chat.messages.length > maxPerChatSize) { + let idx = chat.messages.length - maxPerChatSize; + chat.messages = chat.messages.slice(idx); + } + remainTotalSize += chat.messages.length; + }) + // 保证消息总数不超过maxTotalSize条,否则继续清理 + if (remainTotalSize > maxTotalSize) { + this.fliterMessage(chats, maxTotalSize, maxPerChatSize / 2); + } + }, saveToStorage(withColdMessage) { // 加载中不保存,防止卡顿 if (this.loading) {