代码优化,变量名称调整
This commit is contained in:
@@ -2,6 +2,7 @@ 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 message from './message.js'
|
||||
import listeners from './listeners.js'
|
||||
import {
|
||||
getFriends,
|
||||
getUserInfo,
|
||||
@@ -47,25 +48,25 @@ const setNotifyBadge = () => {
|
||||
/**
|
||||
* 连接IM服务
|
||||
* @param {string} token token
|
||||
* @param {object} userInfo {userId: string, name: string, portraitUrl: string}
|
||||
* @param {object} userInfo {targetId: string, name: string, portraitUrl: string}
|
||||
*/
|
||||
const connect = (token, userInfo, callback) => {
|
||||
RongIMLib.connect(token, res => {
|
||||
callback(res)
|
||||
callback(res)
|
||||
// 更新个人信息
|
||||
store.dispatch('setSenderInfo', userInfo)
|
||||
// 设置未读消息数量
|
||||
setNotifyBadge()
|
||||
// 首次运行获取好友列表
|
||||
const FK = 'IFT_' + userInfo.userId
|
||||
const FK = 'IFTJ_' + userInfo.targetId
|
||||
|
||||
uni.getStorage({
|
||||
key: FK,
|
||||
success: () => {
|
||||
const model = uni.model.friendModel
|
||||
success: () => {
|
||||
const model = uni.model.contactModel
|
||||
model.find((err, results) => {
|
||||
results.map(item => {
|
||||
store.dispatch('launchFriend', item)
|
||||
store.dispatch('launchContact', item)
|
||||
})
|
||||
})
|
||||
},
|
||||
@@ -73,9 +74,9 @@ const connect = (token, userInfo, callback) => {
|
||||
// 程序是首次运行,初始化加载好友信息
|
||||
getFriends().then(res => {
|
||||
res.map(item => {
|
||||
store.dispatch('initFriend', item)
|
||||
store.dispatch('initContact', item)
|
||||
})
|
||||
uni.setStorageSync(FK, userInfo.userId)
|
||||
uni.setStorageSync(FK, userInfo.targetId)
|
||||
})
|
||||
}
|
||||
})
|
||||
@@ -114,36 +115,42 @@ const notifyMsgTypes = [
|
||||
function inArray(search, array) {
|
||||
for (var i in array) {
|
||||
if (array[i] == search) {
|
||||
return true;
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false;
|
||||
return false
|
||||
}
|
||||
|
||||
const addListeners = () => {
|
||||
// 添加连接状态监听函数
|
||||
RongIMLib.addConnectionStatusListener((res) => {
|
||||
console.log('连接状态监听', res.data.status);
|
||||
console.log('连接状态监听', res.data.status)
|
||||
uni.$emit('onConnectionStatusChange', res.data.status)
|
||||
})
|
||||
// 添加消息监听函数
|
||||
RongIMLib.addReceiveMessageListener((res) => {
|
||||
const message = res.data.message
|
||||
console.log('收到消息', message);
|
||||
console.log('收到消息', message)
|
||||
if (inArray(message.objectName, notifyMsgTypes)) {
|
||||
console.log('通知并计数的消息');
|
||||
if (!store.getters.contactIsExist(message.targetId)) {
|
||||
getUserInfo(message.targetId).then(res => {
|
||||
store.dispatch('initContact', res)
|
||||
}).catch(err => {
|
||||
console.log('ERR', err)
|
||||
})
|
||||
}
|
||||
newMessage(message)
|
||||
} else if (message.objectName === RongIMLib.ObjectName.ProfileNotification) {
|
||||
store.dispatch('updateFriend', JSON.parse(message.content.data))
|
||||
store.dispatch('updateContact', JSON.parse(message.content.data))
|
||||
// 调用完更新之后,删除这条消息
|
||||
RongIMLib.deleteMessagesByIds([message.messageId], ({
|
||||
code
|
||||
}) => {
|
||||
console.log('消息删除结果', code);
|
||||
console.log('消息删除结果', code)
|
||||
})
|
||||
} else if (message.objectName === RongIMLib.ObjectName.ContactNotification) {
|
||||
// 触发一个新好友的通知事件
|
||||
uni.$emit('onContactNotification', message.content)
|
||||
} else if (message.objectName === RongIMLib.ObjectName.ContactNotification) {
|
||||
// 触发一个新好友的通知事件
|
||||
uni.$emit('onContactNotification', message.content)
|
||||
}
|
||||
})
|
||||
|
||||
@@ -166,34 +173,34 @@ const addListeners = () => {
|
||||
})
|
||||
// 通话建立成功
|
||||
CallLib.onCallConnected(() => {
|
||||
uni.$emit('onCallConnected');
|
||||
uni.$emit('onCallConnected')
|
||||
})
|
||||
// 外呼
|
||||
CallLib.onCallOutgoing((res) => {
|
||||
uni.$emit('onCallOutgoing');
|
||||
uni.$emit('onCallOutgoing')
|
||||
})
|
||||
// 远端响铃
|
||||
CallLib.onRemoteUserRinging((res) => {
|
||||
uni.$emit('onRemoteUserRinging');
|
||||
uni.$emit('onRemoteUserRinging')
|
||||
})
|
||||
// 远端加入
|
||||
CallLib.onRemoteUserJoined((res) => {
|
||||
uni.$emit('onRemoteUserJoined');
|
||||
uni.$emit('onRemoteUserJoined')
|
||||
})
|
||||
// 断开链接
|
||||
CallLib.onCallDisconnected((res) => {
|
||||
console.log('断开链接', res);
|
||||
uni.$emit('onCallDisconnected');
|
||||
console.log('断开链接', res)
|
||||
uni.$emit('onCallDisconnected')
|
||||
})
|
||||
// 远端挂断
|
||||
CallLib.onRemoteUserLeft((res) => {
|
||||
console.log('远端离开', res);
|
||||
uni.$emit('onRemoteUserLeft');
|
||||
console.log('远端离开', res)
|
||||
uni.$emit('onRemoteUserLeft')
|
||||
})
|
||||
}
|
||||
|
||||
// 维护消息列表,检查是否需要通知声音,设置新消息提醒的数量
|
||||
const newMessage = (msg) => {
|
||||
const newMessage = (msg) => {
|
||||
RongIMLib.getConversationNotificationStatus(msg.conversationType, msg.targetId, ({
|
||||
code,
|
||||
status
|
||||
@@ -203,13 +210,13 @@ const newMessage = (msg) => {
|
||||
triTone()
|
||||
}
|
||||
}
|
||||
});
|
||||
setNotifyBadge()
|
||||
if (msg.conversationType === RongIMLib.ConversationType.PRIVATE) {
|
||||
uni.$emit('onReceivePrivateMessage', msg);
|
||||
} else {
|
||||
uni.$emit('onReceiveGroupMessage', msg);
|
||||
}
|
||||
})
|
||||
setNotifyBadge()
|
||||
if (msg.conversationType === RongIMLib.ConversationType.PRIVATE) {
|
||||
uni.$emit('onReceivePrivateMessage', msg)
|
||||
} else {
|
||||
uni.$emit('onReceiveGroupMessage', msg)
|
||||
}
|
||||
}
|
||||
|
||||
// 播放状态
|
||||
|
||||
Reference in New Issue
Block a user