From 9876e87e2f7ea9203b9f0d3b1cae5e477cd81fd1 Mon Sep 17 00:00:00 2001 From: DCloud_JSON Date: Thu, 25 Apr 2024 15:11:47 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20=E7=BB=9F=E4=B8=80?= =?UTF-8?q?=E7=94=A8conversation.getCached=E8=8E=B7=E5=8F=96=E6=9C=AC?= =?UTF-8?q?=E5=9C=B0=E4=BC=9A=E8=AF=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- components/uni-im-msg/popup-control.vue | 2 +- components/uni-im-msg/uni-im-msg.vue | 9 ++- sdk/ext/MsgManager.class.js | 2 +- sdk/init/getCloudMsg.js | 88 ++++++++++++------------- sdk/methods/conversation/index.js | 4 +- 5 files changed, 54 insertions(+), 51 deletions(-) diff --git a/components/uni-im-msg/popup-control.vue b/components/uni-im-msg/popup-control.vue index 3dd5e73..ce59440 100644 --- a/components/uni-im-msg/popup-control.vue +++ b/components/uni-im-msg/popup-control.vue @@ -286,7 +286,7 @@ let isGroupAdmin = false if(group_id){ - let conversation = uniIm.conversation.dataList.find(i=>i.id == conversation_id) + let conversation = uniIm.conversation.getCached(conversation_id) isGroupAdmin = conversation.group_info.user_id == current_uid } // console.log('isGroupAdmin',isGroupAdmin); diff --git a/components/uni-im-msg/uni-im-msg.vue b/components/uni-im-msg/uni-im-msg.vue index ea93e0e..efff25c 100644 --- a/components/uni-im-msg/uni-im-msg.vue +++ b/components/uni-im-msg/uni-im-msg.vue @@ -191,7 +191,7 @@ }, computed: { currentConversation() { - return uniIm.conversation.dataList.find(conversation => conversation.id == this.msg.conversation_id) + return uniIm.conversation.getCached(this.msg.conversation_id) }, friendlyTime() { let time = this.msg.create_time || this.msg.client_create_time @@ -222,8 +222,11 @@ } }, isFromAdmin() { - const conversation = uniIm.conversation.dataList.find(conversation => conversation.id == this.msg.conversation_id) - return conversation.group_id && conversation.group_member[this.msg.from_uid]?.role?.includes('admin') + const { + group_id, + from_uid + } = this.msg + return group_id && this.currentConversation.group_member[from_uid]?.role?.includes('admin') }, mineId() { return uniCloud.getCurrentUserInfo().uid diff --git a/sdk/ext/MsgManager.class.js b/sdk/ext/MsgManager.class.js index dd31f43..0b1cb4c 100644 --- a/sdk/ext/MsgManager.class.js +++ b/sdk/ext/MsgManager.class.js @@ -301,7 +301,7 @@ export default class Message { if (!Array.isArray(datas)) { datas = [datas] } - let _currentConversation = $state.conversation.dataList.find(item => item.id == this.conversation_id) + let _currentConversation = $state.conversation.getCached(this.conversation_id) // 隐藏的会话,收到消息后需要再次显示出来 if(_currentConversation.hidden){ _currentConversation.hidden = false diff --git a/sdk/init/getCloudMsg.js b/sdk/init/getCloudMsg.js index 102d682..de19f09 100644 --- a/sdk/init/getCloudMsg.js +++ b/sdk/init/getCloudMsg.js @@ -1,7 +1,7 @@ import $state from '@/uni_modules/uni-im/sdk/state/index.js'; const uniImCo = uniCloud.importObject("uni-im-co", { customUI: false -}) +}) const dbJQL = uniCloud.databaseForJQL(); // 获得云端数据,适用于:socket突然断开丢失,或者应用iOS切到后台拿不到透传等场景使用 获取丢失的数据 let getCloudMsgIng = false @@ -41,14 +41,14 @@ function getCloudMsg({ async function getConversationList() { // 根据本地会话的最大更新时间,查询云端数据 let maxConversation = ([...$state.conversation.dataList].sort((a, b) => b.update_time - a.update_time))[0] - console.log('maxConversation:' + maxConversation) - - if (!maxConversation) { + console.log('maxConversation:' + maxConversation) + + if (!maxConversation) { getCloudMsgIng = false return - } - let minUpdateTime = maxConversation.update_time - + } + let minUpdateTime = maxConversation.update_time + console.log('minUpdateTime', minUpdateTime); let {data:conversationDatas} = await uniImCo.getConversationList({ @@ -59,7 +59,7 @@ function getCloudMsg({ conversationDatas.forEach(newConversationInfo => { // console.error('newConversationInfo',newConversationInfo) // 判断是否已经存在 - let conversation = $state.conversation.dataList.find(item => item.id === newConversationInfo.id) + let conversation = $state.conversation.getCached(newConversationInfo.id) if (conversation) { // 更新本地会话数据 Object.assign(conversation, newConversationInfo) @@ -86,7 +86,7 @@ function getCloudMsg({ if (conversationDatas.length === 30) { console.error("可能存在下一页数据"); return await getConversationList() - } + } console.error(`更新会话:${conversationDatas.length}`) } getCloudMsgIng = false @@ -104,11 +104,11 @@ function getCloudMsg({ export default getCloudMsg -async function seaveMsgs(msgs) { - if (msgs.length === 0) { - return +async function seaveMsgs(msgs) { + if (msgs.length === 0) { + return } - const conversation = $state.conversation.dataList.find(item => item.id === msgs[0].conversation_id) + const conversation = $state.conversation.getCached(msgs[0].conversation_id) msgs.sort((a, b) => a.update_time - b.update_time) msgs.forEach(async msg => { // 判断是否出现重复 @@ -137,38 +137,38 @@ async function seaveMsgs(msgs) { } } }) -} - -async function getConversationMsgs({ - limit = 100, - conversation, - minUpdateTime = 0 -}){ - // console.error('getConversationMsgs minUpdateTime',minUpdateTime); - // 按会话查 - const conversation_id = conversation.id - let res = await dbJQL.collection('uni-im-msg').where({ - conversation_id, - "update_time": dbJQL.command.gt(minUpdateTime), - }) - .orderBy('update_time', 'asc') - .limit(limit) - .get() - console.log('查询到新msg数据', res,res.data.length); - seaveMsgs(res.data) - // 递推查询 - if (res.data.length === limit) { - arguments[0].minUpdateTime = res.data[limit - 1].update_time - await getConversationMsgs(arguments[0]) - } -} - -// async function getMsgList({ -// minUpdateTime = 0, -// conversation_ids +} + +async function getConversationMsgs({ + limit = 100, + conversation, + minUpdateTime = 0 +}){ + // console.error('getConversationMsgs minUpdateTime',minUpdateTime); + // 按会话查 + const conversation_id = conversation.id + let res = await dbJQL.collection('uni-im-msg').where({ + conversation_id, + "update_time": dbJQL.command.gt(minUpdateTime), + }) + .orderBy('update_time', 'asc') + .limit(limit) + .get() + console.log('查询到新msg数据', res,res.data.length); + seaveMsgs(res.data) + // 递推查询 + if (res.data.length === limit) { + arguments[0].minUpdateTime = res.data[limit - 1].update_time + await getConversationMsgs(arguments[0]) + } +} + +// async function getMsgList({ +// minUpdateTime = 0, +// conversation_ids // }) { // const limit = 1000 -// console.log('minUpdateTime', minUpdateTime); +// console.log('minUpdateTime', minUpdateTime); // console.log('conversation_ids', conversation_ids); // let res = await uniImCo.getMsgList({ // conversation_ids, @@ -194,7 +194,7 @@ async function getConversationMsgs({ // } // if (res.data.length === limit) { -// minUpdateTime = res.data[limit - 1].update_time +// minUpdateTime = res.data[limit - 1].update_time // let res2 = await getMsgList(arguments[0]) // res.data = res.data.concat(res2.data) // return res diff --git a/sdk/methods/conversation/index.js b/sdk/methods/conversation/index.js index b32a09a..81824bd 100644 --- a/sdk/methods/conversation/index.js +++ b/sdk/methods/conversation/index.js @@ -199,7 +199,7 @@ export default { return datas.reduce((resList, item, index) => { // console.log('resList',resList); // 判断是否存在,再新增。 - let conversation_item = $state.conversation.dataList.find(conversation => conversation.id == item.id) + let conversation_item = $state.conversation.getCached(item.id) if (conversation_item) { // 此会话已经存在,返回给add方法调用者,不重复插到$state.conversation.dataList resList.push(conversation_item) @@ -216,7 +216,7 @@ export default { $state.conversation.dataList.push(conversation) // 拿到响应式的conversation对象 - const _conversation = $state.conversation.dataList.find(i => i.id === conversation.id) + const _conversation = $state.conversation.getCached(conversation.id) // 设置字段为响应式 let activeProperty = _conversation.activeProperty() Object.keys(activeProperty).forEach(key => { -- GitLab