消息监听整理完善

This commit is contained in:
2022-02-17 14:21:53 +08:00
parent c4704cb5ae
commit b4e4ee5458
6 changed files with 156 additions and 151 deletions

View File

@@ -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()
})

View File

@@ -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
}

View File

@@ -12,6 +12,7 @@ export default {
contactInfo: (state) => (targetId) => {
if (state.contacts[targetId]) {
const info = state.contacts[targetId]
return {
name: info.name,
hash: info.hash,

View File

@@ -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
@@ -257,6 +122,7 @@ const triTone = () => {
export default {
initIm,
connect,
disconnect,
setNotifyBadge,
...message
}

View File

@@ -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
}

View File

@@ -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) {