新消息提醒机制,调整为uni.$emit全局事件,网络状态变化亦然

This commit is contained in:
2022-01-28 11:39:26 +08:00
parent f9e2819cc7
commit a97a5fd0ca
4 changed files with 32 additions and 48 deletions

View File

@@ -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() { onShow() {
if (this.$store.state.token !== '') { if (this.$store.state.token !== '') {
this.getConversationList() this.getConversationList()
} }
this.isShown = true 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: { methods: {
hidePop() { hidePop() {
this.showPop = false this.showPop = false

View File

@@ -123,6 +123,18 @@
console.log('------------------获取到了录音的临时路径---------------') console.log('------------------获取到了录音的临时路径---------------')
console.log(res.tempFilePath) 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() { onUnload() {
RongIMLib.clearReadReceiptReceivedListener() 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: { methods: {
// 播放语音消息 // 播放语音消息
onPlayMsg() { onPlayMsg() {

View File

@@ -2,19 +2,10 @@ import im from "@/utils/im/index.js"
export default { export default {
state: { state: {
newMsg: {},
friends: {}, friends: {},
sender: {}, sender: {},
// IM连接状态0是正常
connection: 0
}, },
getters: { getters: {
newMessage(state) {
return state.newMsg
},
connection(state) {
return state.connection
},
friends(state) { friends(state) {
return state.friends return state.friends
}, },
@@ -40,28 +31,14 @@ export default {
} }
}, },
mutations: { mutations: {
newMessage(state, msg) {
Vue.set(state, 'newMsg', msg)
},
updateFriends(state, userInfo) { updateFriends(state, userInfo) {
Vue.set(state.friends, userInfo.userId, userInfo) Vue.set(state.friends, userInfo.userId, userInfo)
}, },
SET_state_sender(state, userInfo) { SET_state_sender(state, userInfo) {
state.sender = userInfo state.sender = userInfo
},
SET_connection_status(state, status) {
state.connection = status
} }
}, },
actions: { actions: {
updateConnectionStatus({commit}, status) {
commit('SET_connection_status', status)
},
newMessage({
commit
}, msg) {
commit('newMessage', msg)
},
setSenderInfo({ setSenderInfo({
commit commit
}, userInfo) { }, userInfo) {

View File

@@ -41,7 +41,6 @@ const setNotifyBadge = () => {
} }
} }
}) })
} }
/** /**
@@ -63,12 +62,16 @@ const connect = (token, userInfo, callback) => {
const model = uni.model.friendModel const model = uni.model.friendModel
model.find((err, results) => { model.find((err, results) => {
console.log('好友列表', results);
results.map(item => { results.map(item => {
store.dispatch('updateFriends', item) store.dispatch('updateFriends', item)
}) })
}) })
} }
/**
* 断开链接
*/
const disconnect = () => { const disconnect = () => {
RongIMLib.disconnect() RongIMLib.disconnect()
// 移除提醒数量 // 移除提醒数量
@@ -108,7 +111,7 @@ const addListeners = () => {
// 添加连接状态监听函数 // 添加连接状态监听函数
RongIMLib.addConnectionStatusListener((res) => { RongIMLib.addConnectionStatusListener((res) => {
console.log('连接状态监听', res.data.status); console.log('连接状态监听', res.data.status);
store.dispatch('updateConnectionStatus', res.data.status) uni.$emit('onConnectionStatusChange', res.data.status)
}) })
// 添加消息监听函数 // 添加消息监听函数
RongIMLib.addReceiveMessageListener((res) => { RongIMLib.addReceiveMessageListener((res) => {
@@ -173,7 +176,8 @@ const newMessage = (msg) => {
syncUserInfo(msg.targetId) syncUserInfo(msg.targetId)
} }
store.dispatch('newMessage', msg) uni.$emit('onReceiveMessage', msg);
// store.dispatch('newMessage', msg)
} }
function syncUserInfo(targetId) { function syncUserInfo(targetId) {