From a97a5fd0ca1922b706adb22a4878bde525e62062 Mon Sep 17 00:00:00 2001 From: Jason Date: Fri, 28 Jan 2022 11:39:26 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E6=B6=88=E6=81=AF=E6=8F=90=E9=86=92?= =?UTF-8?q?=E6=9C=BA=E5=88=B6=EF=BC=8C=E8=B0=83=E6=95=B4=E4=B8=BAuni.$emit?= =?UTF-8?q?=E5=85=A8=E5=B1=80=E4=BA=8B=E4=BB=B6=EF=BC=8C=E7=BD=91=E7=BB=9C?= =?UTF-8?q?=E7=8A=B6=E6=80=81=E5=8F=98=E5=8C=96=E4=BA=A6=E7=84=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pages/im/index.vue | 23 ++++++++++++----------- pages/im/private/index.vue | 22 ++++++++++++---------- store/modules/im.js | 25 +------------------------ utils/im/index.js | 10 +++++++--- 4 files changed, 32 insertions(+), 48 deletions(-) diff --git a/pages/im/index.vue b/pages/im/index.vue index f6a32be..2d18d93 100644 --- a/pages/im/index.vue +++ b/pages/im/index.vue @@ -101,8 +101,19 @@ } } }, + onLoad() { + if (this.isShown) { + uni.$on('onReceiveMessage', (msg) => { + console.log(msg); + this.getConversationList() + }) + uni.$on('onConnectionStatusChange', (status) => { + this.connection = status + }) + } + }, onShow() { - if (this.$store.state.token !== '') { + if (this.$store.state.token !== '') { this.getConversationList() } this.isShown = true @@ -125,16 +136,6 @@ } } }, - watch: { - '$store.getters.newMessage': function(n, o) { - if (this.isShown) { - this.getConversationList() - } - }, - '$store.getters.connection': function(n, o) { - this.connection = n - } - }, methods: { hidePop() { this.showPop = false diff --git a/pages/im/private/index.vue b/pages/im/private/index.vue index e0e70c8..947f48e 100644 --- a/pages/im/private/index.vue +++ b/pages/im/private/index.vue @@ -123,6 +123,18 @@ console.log('------------------获取到了录音的临时路径---------------') console.log(res.tempFilePath) }) + // 简童收到新消息,判断是否是当前会话,更新会话内容 + uni.$on('onReceiveMessage', (msg) => { + if (msg.targetId == this.targetId) { + RongIMLib.clearMessagesUnreadStatus(msg.conversationType, msg.targetId, msg.sentTime) + RongIMLib.sendReadReceiptMessage(msg.conversationType, msg.targetId, msg.sentTime) + this.getMessageList() + im.setNotifyBadge() + } + }) + }, + beforeDestroy() { + uni.$off('onReceiveMessage') }, onUnload() { RongIMLib.clearReadReceiptReceivedListener() @@ -146,16 +158,6 @@ }) } }, - watch: { - '$store.getters.newMessage': function(msg) { - if (msg.targetId == this.targetId) { - RongIMLib.clearMessagesUnreadStatus(msg.conversationType, msg.targetId, msg.sentTime) - RongIMLib.sendReadReceiptMessage(msg.conversationType, msg.targetId, msg.sentTime) - this.getMessageList() - im.setNotifyBadge() - } - } - }, methods: { // 播放语音消息 onPlayMsg() { diff --git a/store/modules/im.js b/store/modules/im.js index c9db26e..e72b86e 100644 --- a/store/modules/im.js +++ b/store/modules/im.js @@ -2,19 +2,10 @@ import im from "@/utils/im/index.js" export default { state: { - newMsg: {}, friends: {}, sender: {}, - // IM连接状态,0是正常 - connection: 0 }, getters: { - newMessage(state) { - return state.newMsg - }, - connection(state) { - return state.connection - }, friends(state) { return state.friends }, @@ -40,28 +31,14 @@ export default { } }, mutations: { - newMessage(state, msg) { - Vue.set(state, 'newMsg', msg) - }, updateFriends(state, userInfo) { Vue.set(state.friends, userInfo.userId, userInfo) }, SET_state_sender(state, userInfo) { state.sender = userInfo - }, - SET_connection_status(state, status) { - state.connection = status } }, - actions: { - updateConnectionStatus({commit}, status) { - commit('SET_connection_status', status) - }, - newMessage({ - commit - }, msg) { - commit('newMessage', msg) - }, + actions: { setSenderInfo({ commit }, userInfo) { diff --git a/utils/im/index.js b/utils/im/index.js index a04ece5..2131e2c 100644 --- a/utils/im/index.js +++ b/utils/im/index.js @@ -41,7 +41,6 @@ const setNotifyBadge = () => { } } }) - } /** @@ -63,12 +62,16 @@ const connect = (token, userInfo, callback) => { const model = uni.model.friendModel model.find((err, results) => { + console.log('好友列表', results); results.map(item => { store.dispatch('updateFriends', item) }) }) } +/** + * 断开链接 + */ const disconnect = () => { RongIMLib.disconnect() // 移除提醒数量 @@ -108,7 +111,7 @@ const addListeners = () => { // 添加连接状态监听函数 RongIMLib.addConnectionStatusListener((res) => { console.log('连接状态监听', res.data.status); - store.dispatch('updateConnectionStatus', res.data.status) + uni.$emit('onConnectionStatusChange', res.data.status) }) // 添加消息监听函数 RongIMLib.addReceiveMessageListener((res) => { @@ -173,7 +176,8 @@ const newMessage = (msg) => { syncUserInfo(msg.targetId) } - store.dispatch('newMessage', msg) + uni.$emit('onReceiveMessage', msg); + // store.dispatch('newMessage', msg) } function syncUserInfo(targetId) {