231 lines
6.0 KiB
JavaScript
231 lines
6.0 KiB
JavaScript
import * as RongIMLib from '@/uni_modules/RongCloud-IMWrapper/js_sdk/index'
|
||
import * as CallLib from '@/uni_modules/RongCloud-CallWrapper/lib/index'
|
||
import store from '@/store/index.js'
|
||
import {
|
||
getFriends,
|
||
getUserInfo
|
||
} from '@/apis/interfaces/im.js'
|
||
|
||
const initIm = (KEY) => {
|
||
RongIMLib.init(KEY)
|
||
CallLib.init()
|
||
addListeners()
|
||
}
|
||
|
||
const setNotifyBadge = () => {
|
||
// 获取未读消息数量
|
||
RongIMLib.getTotalUnreadCount(({
|
||
code,
|
||
count
|
||
}) => {
|
||
if (code === 0) {
|
||
if (count > 0) {
|
||
uni.setTabBarBadge({
|
||
index: 3,
|
||
text: String(count > 99 ? '99+' : count)
|
||
})
|
||
} else {
|
||
uni.removeTabBarBadge({
|
||
index: 3
|
||
})
|
||
}
|
||
}
|
||
})
|
||
}
|
||
|
||
/**
|
||
* 连接IM服务
|
||
* @param {string} token token
|
||
* @param {object} userInfo {userId: string, name: string, portraitUrl: string}
|
||
*/
|
||
const connect = (token, userInfo, callback) => {
|
||
RongIMLib.connect(token, res => {
|
||
console.log('连接结果', res);
|
||
|
||
callback(res)
|
||
})
|
||
|
||
store.dispatch('setSenderInfo', userInfo)
|
||
|
||
setNotifyBadge()
|
||
|
||
const model = uni.model.friendModel
|
||
|
||
model.find((err, results) => {
|
||
results.map(item => {
|
||
store.dispatch('updateFriends', item)
|
||
})
|
||
})
|
||
}
|
||
|
||
const disconnect = () => {
|
||
RongIMLib.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);
|
||
store.dispatch('updateConnectionStatus', res.data.status)
|
||
})
|
||
// 添加消息监听函数
|
||
RongIMLib.addReceiveMessageListener((res) => {
|
||
console.log('收到消息', res.data.message);
|
||
const message = res.data.message
|
||
if (inArray(message.objectName, notifyMsgTypes)) {
|
||
newMessage(message)
|
||
}
|
||
})
|
||
// 监听通话呼入
|
||
CallLib.onCallReceived((res) => {
|
||
console.log("Engine:OnCallReceived=>" + "监听通话呼入, 目标id=>", res.data.targetId);
|
||
console.log('RES', res);
|
||
uni.navigateTo({
|
||
url: '/pages/im/private/call?targetId=' + res.data.targetId,
|
||
success: (err) => {
|
||
console.log('跳转视频通话成功');
|
||
},
|
||
fail: (err) => {
|
||
console.log('跳转视频页失败', err);
|
||
}
|
||
})
|
||
})
|
||
CallLib.onCallOutgoing((res) => {
|
||
console.log("主叫端拨出电话后,通过回调 onCallOutgoing,通知当前 call 的详细信息", res)
|
||
})
|
||
CallLib.onCallConnected((res) => {
|
||
console.log("Engine:OnCallConnected=>" + "通话接通时,通过回调 onCallConnected 通知当前 call 的详细信息", res)
|
||
});
|
||
CallLib.onRemoteUserJoined((res) => {
|
||
console.log("Engine:OnRemoteUserJoined=>" + "主叫端拨出电话,被叫端收到请求后,加入通话,被叫端Id为=>", res.data.userId);
|
||
})
|
||
CallLib.onCallDisconnected((res) => {
|
||
console.log("Engine:OnCallDisconnected=>" + "挂断成功, 挂断原因=>", res.data.reason)
|
||
})
|
||
CallLib.onRemoteUserLeft((res) => {
|
||
console.log("Engine:OnRemoteUserLeft=>" + "远端用户挂断,远端Id为=>", res.data.reason)
|
||
})
|
||
}
|
||
|
||
// 维护消息列表
|
||
const newMessage = (msg) => {
|
||
RongIMLib.getConversationNotificationStatus(msg.conversationType, msg.targetId, ({
|
||
code,
|
||
status
|
||
}) => {
|
||
if (code === 0) {
|
||
if (status) {
|
||
triTone()
|
||
}
|
||
}
|
||
});
|
||
|
||
setNotifyBadge()
|
||
|
||
if (!store.getters.hasUser(msg.targetId)) {
|
||
syncUserInfo(msg.targetId)
|
||
}
|
||
|
||
store.dispatch('newMessage', msg)
|
||
}
|
||
|
||
function syncUserInfo(targetId) {
|
||
getUserInfo(targetId).then(res => {
|
||
store.dispatch('updateFriends', res)
|
||
})
|
||
}
|
||
|
||
// 播放状态
|
||
let tipState = false
|
||
|
||
const triTone = () => {
|
||
if (tipState == false) {
|
||
const innerAudioContext = uni.createInnerAudioContext()
|
||
innerAudioContext.autoplay = true
|
||
innerAudioContext.src = '/static/tri-tone.mp3'
|
||
innerAudioContext.onPlay(() => {
|
||
tipState = true
|
||
})
|
||
innerAudioContext.onEnded(() => {
|
||
tipState = false
|
||
})
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 发送消息
|
||
* @param {number} conversationType 消息类型
|
||
* @param {string} targetId 会话id
|
||
* @param {string} content 消息内容
|
||
* @param {function} callback 回调函数
|
||
*/
|
||
const sendMsg = (conversationType, targetId, content, callback) => {
|
||
const msg = {
|
||
conversationType: conversationType,
|
||
targetId: String(targetId),
|
||
content: {
|
||
objectName: 'RC:TxtMsg',
|
||
content: content,
|
||
user: store.getters.sender
|
||
}
|
||
}
|
||
RongIMLib.sendMessage(msg, ({
|
||
code,
|
||
messageId
|
||
}) => {
|
||
if (code === 0) {
|
||
callback(messageId)
|
||
} else {
|
||
uni.showToast({
|
||
icon: 'none',
|
||
title: '发送失败'
|
||
})
|
||
}
|
||
})
|
||
}
|
||
|
||
/**
|
||
* 同步好友信息,保存头像地址等
|
||
*/
|
||
const syncFriends = () => {
|
||
getFriends().then(res => {
|
||
res.map(item => {
|
||
console.log('item', item);
|
||
store.dispatch('updateFriends', item)
|
||
})
|
||
})
|
||
}
|
||
|
||
export default {
|
||
initIm,
|
||
connect,
|
||
sendMsg,
|
||
setNotifyBadge,
|
||
syncFriends,
|
||
syncUserInfo
|
||
}
|