From b4e4ee54582deb901e798452f907d5911d9d1f2a Mon Sep 17 00:00:00 2001 From: Jason Date: Thu, 17 Feb 2022 14:21:53 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B6=88=E6=81=AF=E7=9B=91=E5=90=AC=E6=95=B4?= =?UTF-8?q?=E7=90=86=E5=AE=8C=E5=96=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pages/im/group/chat.nvue | 6 -- pages/im/index.vue | 2 - store/modules/im.js | 7 +- utils/im/index.js | 146 ++------------------------------------- utils/im/listeners.js | 138 ++++++++++++++++++++++++++++++++++++ utils/index.js | 8 +++ 6 files changed, 156 insertions(+), 151 deletions(-) diff --git a/pages/im/group/chat.nvue b/pages/im/group/chat.nvue index f1843f5..e53837f 100644 --- a/pages/im/group/chat.nvue +++ b/pages/im/group/chat.nvue @@ -95,11 +95,6 @@ }) // 清理聊天记录 uni.$once('cleanGroupMessage', this.getMessageList) - uni.$on('onGroupDismiss', () => { - uni.switchTab({ - url: '/pages/im/index' - }) - }) }, onNavigationBarButtonTap() { uni.navigateTo({ @@ -143,7 +138,6 @@ 100, true, (messages) => { - console.log(messages); this.messages = messages.reverse() this.scrollBottom() }) diff --git a/pages/im/index.vue b/pages/im/index.vue index 47c6d00..3701d17 100644 --- a/pages/im/index.vue +++ b/pages/im/index.vue @@ -77,7 +77,6 @@ methods: { checkNewFriendPending() { im.getPendingList((pendings) => { - console.log(pendings); this.hasNewFriends = pendings.length }) }, @@ -98,7 +97,6 @@ code, conversations }) => { - console.log(conversations); if (code === 0) { this.conversations = conversations } diff --git a/store/modules/im.js b/store/modules/im.js index 211f93a..fdc88d5 100644 --- a/store/modules/im.js +++ b/store/modules/im.js @@ -9,9 +9,10 @@ export default { contacts(state) { return state.contacts }, - contactInfo: (state) => (targetId) => { + contactInfo: (state) => (targetId) => { if (state.contacts[targetId]) { const info = state.contacts[targetId] + return { name: info.name, hash: info.hash, @@ -37,7 +38,7 @@ export default { updateContactInfo(state, contactInfo) { Vue.set(state.contacts, contactInfo.targetId, contactInfo) }, - setSenderInfo(state, contactInfo) { + setSenderInfo(state, contactInfo) { state.myInfo = { userId: contactInfo.targetId, name: contactInfo.name, @@ -49,7 +50,7 @@ export default { setSenderInfo({ commit }, contactInfo) { - commit('setSenderInfo', contactInfo) + commit('setSenderInfo', contactInfo) }, // 载入好友信息 launchContact({ diff --git a/utils/im/index.js b/utils/im/index.js index 2984b38..cc8e9ac 100644 --- a/utils/im/index.js +++ b/utils/im/index.js @@ -12,8 +12,9 @@ import { const initIm = (KEY) => { RongIMLib.init(KEY) - // CallLib.init() - addListeners() + CallLib.init() + listeners.imLibListeners() + listeners.callLibListeners() // 初始化的时候 自动链接 if (store.getters.getToken !== '') { getImToken().then(res => { @@ -59,7 +60,7 @@ const connect = (token, userInfo, callback) => { // 设置未读消息数量 setNotifyBadge() // 首次运行获取好友列表 - const FK = 'ZHK_' + userInfo.targetId + const FK = 'ZHKD_' + userInfo.targetId uni.getStorage({ key: FK, @@ -100,142 +101,6 @@ const disconnect = () => { }) } -// 允许通知的消息类型,触发更新消息列表操作 -const notifyMsgTypes = [ - 'RC:TxtMsg', - 'RC:VcMsg', - 'RC:HQVCMsg', - 'RC:ImgMsg', - 'RC:GIFMsg', - 'RC:ImgTextMsg', - 'RC:FileMsg', - 'RC:LBSMsg', - 'RC:SightMsg', - 'RC:ReferenceMsg', - 'RC:CombineMsg', -] - -function inArray(search, array) { - for (var i in array) { - if (array[i] == search) { - return true - } - } - return false -} - -const addListeners = () => { - // 添加连接状态监听函数 - RongIMLib.addConnectionStatusListener((res) => { - console.log('连接状态监听', res.data.status) - uni.$emit('onConnectionStatusChange', res.data.status) - }) - // 添加消息监听函数 - RongIMLib.addReceiveMessageListener((res) => { - const message = res.data.message - console.log('收到消息', message) - if (inArray(message.objectName, notifyMsgTypes)) { - if (!store.getters.contactIsExist(message.targetId)) { - getUserInfo(message.targetId).then(res => { - store.dispatch('initContact', res) - }).catch(err => { - console.log('ERR', err) - }) - } - if (!store.getters.contactIsExist(message.senderUserId)) { - getUserInfo(message.senderUserId).then(res => { - store.dispatch('initContact', res) - }).catch(err => { - console.log('ERR', err) - }) - } - newMessage(message) - } else if (message.objectName === RongIMLib.ObjectName.ProfileNotification) { - // 更新会话信息 - store.dispatch('updateContact', JSON.parse(message.content.data)) - // 调用完更新之后,删除这条消息 - RongIMLib.deleteMessagesByIds([message.messageId], ({ - code - }) => { - console.log('消息删除结果', code) - }) - } else if (message.objectName === RongIMLib.ObjectName.ContactNotification) { - // 触发一个新好友的通知事件 - uni.$emit('onContactNotification', message) - } else if (message.objectName === RongIMLib.ObjectName.GroupNotification) { - // 解散群 - if (message.content.operation === 'Dismiss') { - RongIMLib.cleanHistoryMessages(message.conversationType, message.targetId, - message.sentTime - 1, false, () => {}) - // 解散了就删了吧 - RongIMLib.removeConversation(message.conversationType, message.targetId, () => {}) - uni.$emit('onGroupDismiss', message.targetId) - } - uni.$emit('onReceiveMessage', message) - } - }) - - // 监听消息回执 - RongIMLib.addReadReceiptReceivedListener(({ - data - }) => { - uni.$emit('onReadReceiptReceived', data) - }) - - // 音视频通话相关的 - // 监听通话呼入 - // CallLib.onCallReceived(({ - // data - // }) => { - // uni.navigateTo({ - // url: '/pages/im/private/call?targetId=' + data.targetId + '&mediaType=' + - // data.mediaType - // }) - // }) - // // 通话建立成功 - // CallLib.onCallConnected(() => { - // uni.$emit('onCallConnected') - // }) - // // 外呼 - // CallLib.onCallOutgoing((res) => { - // uni.$emit('onCallOutgoing') - // }) - // // 远端响铃 - // CallLib.onRemoteUserRinging((res) => { - // uni.$emit('onRemoteUserRinging') - // }) - // // 远端加入 - // CallLib.onRemoteUserJoined((res) => { - // uni.$emit('onRemoteUserJoined') - // }) - // // 断开链接 - // CallLib.onCallDisconnected((res) => { - // console.log('断开链接', res) - // uni.$emit('onCallDisconnected') - // }) - // // 远端挂断 - // CallLib.onRemoteUserLeft((res) => { - // console.log('远端离开', res) - // uni.$emit('onRemoteUserLeft') - // }) -} - -// 维护消息列表,检查是否需要通知声音,设置新消息提醒的数量 -const newMessage = (msg) => { - RongIMLib.getConversationNotificationStatus(msg.conversationType, msg.targetId, ({ - code, - status - }) => { - if (code === 0) { - if (status) { - triTone() - } - } - }) - setNotifyBadge() - uni.$emit('onReceiveMessage', msg) -} - // 播放状态 let tipState = false @@ -256,7 +121,8 @@ const triTone = () => { export default { initIm, - connect, + connect, + disconnect, setNotifyBadge, ...message } diff --git a/utils/im/listeners.js b/utils/im/listeners.js index e69de29..9aba0a1 100644 --- a/utils/im/listeners.js +++ b/utils/im/listeners.js @@ -0,0 +1,138 @@ +import * as CallLib from '@/uni_modules/RongCloud-CallWrapper/lib/index' +import * as IMLib from '@/uni_modules/RongCloud-IMWrapper/js_sdk/index' +import store from '@/store/index.js' +import im from "@/utils/im/index.js" +import utils from '../index.js' +import { + getUserInfo +} from '@/apis/interfaces/im.js' + +// 维护消息列表,检查是否需要通知声音,设置新消息提醒的数量 +const onReceiveMessage = (message) => { + IMLib.getConversationNotificationStatus(message.conversationType, message.targetId, ({ + code, + status + }) => { + if (code === 0) { + if (status) { + // triTone() + } + } + }) + im.setNotifyBadge() + uni.$emit('onReceiveMessage', message) +} + +// 允许通知的消息类型,触发更新消息列表操作,提示音 +const notifyMsgTypes = [ + IMLib.ObjectName.Text, + IMLib.ObjectName.File, + IMLib.ObjectName.Image, + IMLib.ObjectName.GIF, + IMLib.ObjectName.Location, + IMLib.ObjectName.Voice, + IMLib.ObjectName.HQVoice, + IMLib.ObjectName.Sight, + IMLib.ObjectName.Text, + IMLib.ObjectName.Text, + IMLib.ObjectName.Text +] + +const imLibListeners = () => { + console.log('添加监听'); + // 添加连接状态监听函数 + IMLib.addConnectionStatusListener((res) => { + console.log('连接状态监听', res.data.status) + uni.$emit('onConnectionStatusChange', res.data.status) + }) + // 添加消息监听函数 + IMLib.addReceiveMessageListener((res) => { + const message = res.data.message + console.error('[收到消息]', message) + if (utils.inArray(message.objectName, notifyMsgTypes)) { + if (!store.getters.contactIsExist(message.targetId)) { + getUserInfo(message.targetId).then(res => { + store.dispatch('initContact', res) + }).catch(err => { + console.error('getUserInfo ERR', err) + }) + } + if (!store.getters.contactIsExist(message.senderUserId)) { + getUserInfo(message.senderUserId).then(res => { + store.dispatch('initContact', res) + }).catch(err => { + console.error('getUserInfo ERR', err) + }) + } + onReceiveMessage(message) + } else if (message.objectName === IMLib.ObjectName.ProfileNotification) { + // 更新会话信息 + store.dispatch('updateContact', JSON.parse(message.content.data)) + // 调用完更新之后,删除这条消息 + IMLib.deleteMessagesByIds([message.messageId]) + } else if (message.objectName === IMLib.ObjectName.ContactNotification) { + // 触发一个新好友的通知事件 + uni.$emit('onContactNotification', message) + } else if (message.objectName === IMLib.ObjectName.GroupNotification) { + // 解散群 + if (message.content.operation === 'Dismiss') { + IMLib.cleanHistoryMessages(message.conversationType, message.targetId, message.sentTime, false) + // 解散了就删了吧 + IMLib.removeConversation(message.conversationType, message.targetId) + uni.$emit('onGroupDismiss', message.targetId) + } + uni.$emit('onReceiveMessage', message) + } + }) + + // 监听消息回执 + IMLib.addReadReceiptReceivedListener(({ + data + }) => { + uni.$emit('onReadReceiptReceived', data) + }) +} + +const callLibListeners = () => { + // 音视频通话相关的 + // 监听通话呼入 + CallLib.onCallReceived(({ + data + }) => { + uni.navigateTo({ + url: '/pages/im/private/call?targetId=' + data.targetId + '&mediaType=' + + data.mediaType + }) + }) + // 通话建立成功 + CallLib.onCallConnected(() => { + uni.$emit('onCallConnected') + }) + // 外呼 + CallLib.onCallOutgoing((res) => { + uni.$emit('onCallOutgoing') + }) + // 远端响铃 + CallLib.onRemoteUserRinging((res) => { + uni.$emit('onRemoteUserRinging') + }) + // 远端加入 + CallLib.onRemoteUserJoined((res) => { + uni.$emit('onRemoteUserJoined') + }) + // 断开链接 + CallLib.onCallDisconnected((res) => { + console.log('断开链接', res) + uni.$emit('onCallDisconnected') + }) + // 远端挂断 + CallLib.onRemoteUserLeft((res) => { + console.log('远端离开', res) + uni.$emit('onRemoteUserLeft') + }) +} + +export default { + imLibListeners, + callLibListeners +} diff --git a/utils/index.js b/utils/index.js index 665e4d5..b772318 100644 --- a/utils/index.js +++ b/utils/index.js @@ -1,6 +1,14 @@ import env from './conf/env.js' export default { + inArray: (search, array) => { + for (var i in array) { + if (array[i] == search) { + return true + } + } + return false + }, checkPhone: (phone) => { let re = /^[0-9]+.?[0-9]*/; if (phone.length === 11) {