From 0e7827965ee22c163be263c30237da6de536a7f6 Mon Sep 17 00:00:00 2001 From: linju Date: Fri, 8 Nov 2024 15:34:40 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20=E4=BF=AE=E5=A4=8Dweb?= =?UTF-8?q?=E7=AB=AF=E5=9C=A8=E9=83=A8=E5=88=86=E6=83=85=E5=86=B5=E4=B8=8B?= =?UTF-8?q?=EF=BC=8C=E6=9C=AC=E5=9C=B0=E5=88=9B=E5=BB=BA=E7=9A=84=E5=8D=95?= =?UTF-8?q?=E8=81=8A=E4=B8=B4=E6=97=B6=E4=BC=9A=E8=AF=9D=E7=9B=B8=E5=85=B3?= =?UTF-8?q?=E9=A1=B5=E9=9D=A2=EF=BC=8C=E6=89=93=E5=BC=80=E5=90=8E=E7=9B=B4?= =?UTF-8?q?=E6=8E=A5=E5=88=B7=E6=96=B0=E4=BC=9A=E5=87=BA=E9=94=99=E7=9A=84?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pages/chat/chat.vue | 12 ++++++ pages/chat/info.vue | 20 ++-------- pages/index/index.vue | 65 ++++++++------------------------- pages/userList/userList.vue | 11 +----- sdk/state/Conversation.class.js | 19 ++++++---- 5 files changed, 43 insertions(+), 84 deletions(-) diff --git a/pages/chat/chat.vue b/pages/chat/chat.vue index 7ffedb2..875ba63 100644 --- a/pages/chat/chat.vue +++ b/pages/chat/chat.vue @@ -412,6 +412,18 @@ this.$refs["chat-input"]?.focus() }, 100) } + const {is_temp,friend_uid} = this.conversation + if(is_temp && friend_uid){ + // 为了避免在web端刷新页面之后,本地单聊临时会话丢失,将地址栏中的会话id参数替换为好友的uid,以此来确保在刷新页面之后,本地能够再次创建临时会话。 + const {route:path,options} = getCurrentPages().pop() + delete options.conversation_id + options.user_id = friend_uid + let paramSrt + for (const key in options) { + paramSrt = `${key}=${options[key]}` + } + history.replaceState(null, '', `/#/${path}?${paramSrt}`) + } // #endif }, onclickMsgList(){ diff --git a/pages/chat/info.vue b/pages/chat/info.vue index 8cf02a7..863459e 100644 --- a/pages/chat/info.vue +++ b/pages/chat/info.vue @@ -97,23 +97,9 @@ export default { methods: { async load(options) { console.log('options',options); - let conversation_id = options.conversation_id || options.id - // 如果只传了user_id,需要先获取conversation_id - if(!conversation_id){ - if(!options.user_id){ - console.error('参数错误') - return uni.showToast({ - title: '参数错误', - icon: 'none' - }); - } - conversation_id = await uniIm.utils.getConversationId(options.user_id) - console.log('conversation_id',conversation_id); - } - - let conversation = await uniIm.conversation.get(conversation_id) - this.conversation = conversation - this.friend_uid = conversation.friend_uid + const {user_id,id} = options + this.conversation = await uniIm.conversation.get(user_id ? {user_id} : id) + this.friend_uid = this.conversation.friend_uid let field = '_id,nickname,avatar_file' if (this.uniIDHasRole('staff')) { field += ',email' diff --git a/pages/index/index.vue b/pages/index/index.vue index ef906df..b24810a 100644 --- a/pages/index/index.vue +++ b/pages/index/index.vue @@ -57,7 +57,7 @@ ref="uni-im-filtered-conversation-list" id="conversation-list-box" :keyword="keyword" - @to-chat="toChat($event)" + @to-chat="toChat" @to-chat-filtered="toChatFiltered($event)" > @@ -81,7 +81,7 @@ - + @@ -473,35 +473,19 @@ // goods, // user_id // }); + if(user_id || conversation_id){ + this.toChat(conversation_id || {user_id}) + } + // 如果初始化时,指定了要访问的会话。将指定要访问的会话排序位置置顶,方便看到 + setTimeout(()=> this.currentConversation.customIndex = Date.now(), 0); // 如果列表小于30个会话,尝试着从云端拉取一次 if( this.conversationList.length < 30 ){ - await this.$nextTick() - await this.$refs['uni-im-conversation-list'].loadMore() + await this.$nextTick() + await this.$refs['uni-im-conversation-list'].loadMore() }else{ console.log('会话列表已满一页,需要用户自己滚动到底,再拉取更多'); } - // console.log('this.conversationList.length',this.conversationList.length); - if (conversation_id) { - console.log('conversation_id', conversation_id); - this.toChat(conversation_id) - } else if (user_id) { - //创建会话 - const currentConversation = await uniIm.conversation.get({ - friend_uid: user_id - }) - // console.log('currentConversation', currentConversation); - // 当前用户给对方发个消息 - this.toChat(currentConversation.id) - } - - if(user_id){ - // 如果初始化时,指定了要访问的user会话。将指定要访问的会话排序位置置顶,方便看到 - // 场景:插件市场,点击联系作者。自动将此会话放到首个 - setTimeout(()=> { - this.currentConversation.customIndex = Date.now() - }, 0); - } // 传递参数goods(对象格式,包含:商品名称name,链接url。自动设置对话框默认内容 if (this.isWidescreen && goods) { @@ -527,7 +511,7 @@ if(joinGroup){ // #ifdef H5 //删除URL后面的参数(主要是删除joinGroup=xxx),且不刷新页面 - history.pushState({}, '', '/#/'); + history.replaceState(null, '', '/#/') // #endif this.joinGroup(joinGroup) }; @@ -598,45 +582,26 @@ }) } }, - async toChat(param) { this.chatInfoIsShow = false; this.keyword = '' this.filteredConversationId = false - - const conversation_id = await getConversationId(param) - // console.log('toChat', conversation_id) - uniIm.currentConversationId = conversation_id + const {id} = await uniIm.conversation.get(param) + uniIm.currentConversationId = id if (this.isWidescreen) { // 若为宽屏,则切换右侧的组件 this.$nextTick(() => { let chatViewRef = this.$refs['chat-view'] if (chatViewRef) { - chatViewRef.load(conversation_id) + chatViewRef.load(id) } }) } else { // 若为窄屏,则打开新窗体 + // param 转成字符串 uni.navigateTo({ - url: '/uni_modules/uni-im/pages/chat/chat?conversation_id=' + conversation_id, + url: '/uni_modules/uni-im/pages/chat/chat?conversation_id=' + id, animationDuration: 300 }) } - - async function getConversationId(param){ - if (typeof param == 'string') { - return param - } else { - if (param.conversation_id) { - return param.conversation_id - } else if (param.group_id) { - return 'group_' + param.group_id - } else if (param.user_id || param.friend_uid) { - // 获取会话id(如果此单聊会话未创建会自动创建) - return (await uniIm.conversation.get(param)).id - } else { - throw new Error("toChat param is error") - } - } - } }, showChatInfo() { this.chatInfoIsShow = !this.chatInfoIsShow diff --git a/pages/userList/userList.vue b/pages/userList/userList.vue index 1897e45..e520b1f 100644 --- a/pages/userList/userList.vue +++ b/pages/userList/userList.vue @@ -75,21 +75,14 @@ import uniIm from '@/uni_modules/uni-im/sdk/index.js'; 'more'; // oading 的状态,可选值:more(loading前)、loading(loading中)、noMore(没有更多了) }, async toChat(user_id) { - //拿到会话(如果没有自动创建) - const currentConversation = await uniIm.conversation.get({ - friend_uid:user_id - }); - console.log('currentConversation', currentConversation); // 当前用户给对方发个消息 if (this.isWidescreen) { //若为宽屏,则触发右侧详情页的自定义事件,通知右侧窗体刷新详情 - uni.navigateTo({ - url: '/uni_modules/uni-im/pages/index/index?conversation_id=' + currentConversation.id - }); + location.href = '/#/uni_modules/uni-im/pages/index/index?user_id=' + user_id } else { // 若为窄屏,则打开新窗体,在新窗体打开详情页面 uni.navigateTo({ - url: '/uni_modules/uni-im/pages/chat/chat?conversation_id=' + currentConversation.id + url: '/uni_modules/uni-im/pages/chat/chat?user_id=' + user_id }); } }, diff --git a/sdk/state/Conversation.class.js b/sdk/state/Conversation.class.js index 301f65d..819f9bc 100644 --- a/sdk/state/Conversation.class.js +++ b/sdk/state/Conversation.class.js @@ -50,6 +50,14 @@ export default class Conversation extends CloudData { // 设置为默认按id查找会话,而不是按_id查找 return {id:param} }else if(typeof param === 'object' && param !== null && !Array.isArray(param)){ + const {user_id,friend_uid,conversation_id:id} = param + if(id){ + param.id = id + delete param.conversation_id + }else if(user_id){ + param.friend_uid = friend_uid || user_id + delete param.user_id + } if('source' in param){ // 本地查找不需要source字段 const source = param.source @@ -67,13 +75,6 @@ export default class Conversation extends CloudData { } },0) } - if('user_id' in param){ - // param = {friend_uid:param.user_id} - param.friend_uid = param.user_id - delete param.user_id - }else if('conversation_id' in param){ - param = {id:param.conversation_id} - } return param } } @@ -91,7 +92,9 @@ export default class Conversation extends CloudData { "type": param.friend_uid ? 1 : 2, "msgList": [], "update_time": Date.now(), - "customSortTime": Date.now() + "customSortTime": Date.now(), + // 是本地临时会话数据 + "is_temp": true } const conversation = this.add(conversationData) $users.get(friend_uid).then(res => { -- GitLab