diff --git a/pages/chat/chat.vue b/pages/chat/chat.vue index 7ffedb2a2c54cce29130ecaf37f23f57a1ab2372..875ba63e395bf7b0ac6b7d167c6d8c7ae37395e9 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 8cf02a71b8aae45ad354d1d52e1f08e5a63f2f05..863459e193c245393a97eaa8f9f41cdb04989730 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 ef906df47a42db8c655a1d958df1ef8d20aee4ba..b24810ae73c570541527fed35ee6c4e8198823bf 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 1897e45f53886c31bc048cbd3343e51090c1cea2..e520b1f88d39941c56b405780c6a87676033c2a3 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 301f65d3a5ee2dd794fdca929e87ee9ff760e1f5..819f9bc9898f8c080243b10fe4f50f5bbac9a9b4 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 => {