162 lines
5.6 KiB
JavaScript
162 lines
5.6 KiB
JavaScript
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 = () => {
|
|
// 添加连接状态监听函数
|
|
IMLib.addConnectionStatusListener((res) => {
|
|
console.error('连接状态监听', 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) {
|
|
console.error('触发一个新好友的通知事件', message);
|
|
// 触发一个新好友的通知事件
|
|
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
|
|
}) => {
|
|
console.error("监听私聊消息已读回执: ", data);
|
|
uni.$emit('onReadReceiptReceived', data)
|
|
})
|
|
// 监听消息撤回操作
|
|
IMLib.addRecallMessageListener((res) => {
|
|
IMLib.getMessage(res.data.messageId, (res) => {
|
|
console.error("消息撤回: ", res.message);
|
|
uni.$emit('onRecallMessage', res.message)
|
|
})
|
|
})
|
|
// 监听需要群聊消息回执
|
|
IMLib.addReceiptRequestListener(({
|
|
data
|
|
}) => {
|
|
console.error('onReceiptRequested', data);
|
|
uni.$emit('onReceiptRequest', data)
|
|
})
|
|
// 群消息已读的回执
|
|
IMLib.addReceiptResponseListener((res) => {
|
|
console.error('onReceiptResponse', res);
|
|
})
|
|
}
|
|
|
|
const callLibListeners = () => {
|
|
// 音视频通话相关的
|
|
// 监听通话呼入
|
|
CallLib.onCallReceived(({
|
|
data
|
|
}) => {
|
|
console.error('onCallReceived');
|
|
uni.navigateTo({
|
|
url: '/pages/im/private/call?targetId=' + data.targetId + '&mediaType=' +
|
|
data.mediaType
|
|
})
|
|
})
|
|
// 通话建立成功
|
|
CallLib.onCallConnected(() => {
|
|
uni.$emit('onCallConnected')
|
|
})
|
|
// 外呼
|
|
CallLib.onCallOutgoing((res) => {
|
|
console.error('onCallOutgoing', res);
|
|
uni.$emit('onCallOutgoing')
|
|
})
|
|
// 远端响铃
|
|
CallLib.onRemoteUserRinging((res) => {
|
|
console.error('onRemoteUserRinging', res);
|
|
uni.$emit('onRemoteUserRinging')
|
|
})
|
|
// 远端加入
|
|
CallLib.onRemoteUserJoined((res) => {
|
|
console.error('远端接听');
|
|
uni.$emit('onRemoteUserJoined')
|
|
})
|
|
// 断开链接
|
|
CallLib.onCallDisconnected((res) => {
|
|
console.error('断开链接', res)
|
|
uni.$emit('onCallDisconnected')
|
|
})
|
|
// 远端挂断
|
|
CallLib.onRemoteUserLeft((res) => {
|
|
console.error('远端离开', res)
|
|
uni.$emit('onRemoteUserLeft')
|
|
})
|
|
}
|
|
|
|
export default {
|
|
imLibListeners,
|
|
callLibListeners
|
|
}
|