消息监听整理完善
This commit is contained in:
@@ -95,11 +95,6 @@
|
|||||||
})
|
})
|
||||||
// 清理聊天记录
|
// 清理聊天记录
|
||||||
uni.$once('cleanGroupMessage', this.getMessageList)
|
uni.$once('cleanGroupMessage', this.getMessageList)
|
||||||
uni.$on('onGroupDismiss', () => {
|
|
||||||
uni.switchTab({
|
|
||||||
url: '/pages/im/index'
|
|
||||||
})
|
|
||||||
})
|
|
||||||
},
|
},
|
||||||
onNavigationBarButtonTap() {
|
onNavigationBarButtonTap() {
|
||||||
uni.navigateTo({
|
uni.navigateTo({
|
||||||
@@ -143,7 +138,6 @@
|
|||||||
100,
|
100,
|
||||||
true,
|
true,
|
||||||
(messages) => {
|
(messages) => {
|
||||||
console.log(messages);
|
|
||||||
this.messages = messages.reverse()
|
this.messages = messages.reverse()
|
||||||
this.scrollBottom()
|
this.scrollBottom()
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -77,7 +77,6 @@
|
|||||||
methods: {
|
methods: {
|
||||||
checkNewFriendPending() {
|
checkNewFriendPending() {
|
||||||
im.getPendingList((pendings) => {
|
im.getPendingList((pendings) => {
|
||||||
console.log(pendings);
|
|
||||||
this.hasNewFriends = pendings.length
|
this.hasNewFriends = pendings.length
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
@@ -98,7 +97,6 @@
|
|||||||
code,
|
code,
|
||||||
conversations
|
conversations
|
||||||
}) => {
|
}) => {
|
||||||
console.log(conversations);
|
|
||||||
if (code === 0) {
|
if (code === 0) {
|
||||||
this.conversations = conversations
|
this.conversations = conversations
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,9 +9,10 @@ export default {
|
|||||||
contacts(state) {
|
contacts(state) {
|
||||||
return state.contacts
|
return state.contacts
|
||||||
},
|
},
|
||||||
contactInfo: (state) => (targetId) => {
|
contactInfo: (state) => (targetId) => {
|
||||||
if (state.contacts[targetId]) {
|
if (state.contacts[targetId]) {
|
||||||
const info = state.contacts[targetId]
|
const info = state.contacts[targetId]
|
||||||
|
|
||||||
return {
|
return {
|
||||||
name: info.name,
|
name: info.name,
|
||||||
hash: info.hash,
|
hash: info.hash,
|
||||||
@@ -37,7 +38,7 @@ export default {
|
|||||||
updateContactInfo(state, contactInfo) {
|
updateContactInfo(state, contactInfo) {
|
||||||
Vue.set(state.contacts, contactInfo.targetId, contactInfo)
|
Vue.set(state.contacts, contactInfo.targetId, contactInfo)
|
||||||
},
|
},
|
||||||
setSenderInfo(state, contactInfo) {
|
setSenderInfo(state, contactInfo) {
|
||||||
state.myInfo = {
|
state.myInfo = {
|
||||||
userId: contactInfo.targetId,
|
userId: contactInfo.targetId,
|
||||||
name: contactInfo.name,
|
name: contactInfo.name,
|
||||||
@@ -49,7 +50,7 @@ export default {
|
|||||||
setSenderInfo({
|
setSenderInfo({
|
||||||
commit
|
commit
|
||||||
}, contactInfo) {
|
}, contactInfo) {
|
||||||
commit('setSenderInfo', contactInfo)
|
commit('setSenderInfo', contactInfo)
|
||||||
},
|
},
|
||||||
// 载入好友信息
|
// 载入好友信息
|
||||||
launchContact({
|
launchContact({
|
||||||
|
|||||||
@@ -12,8 +12,9 @@ import {
|
|||||||
|
|
||||||
const initIm = (KEY) => {
|
const initIm = (KEY) => {
|
||||||
RongIMLib.init(KEY)
|
RongIMLib.init(KEY)
|
||||||
// CallLib.init()
|
CallLib.init()
|
||||||
addListeners()
|
listeners.imLibListeners()
|
||||||
|
listeners.callLibListeners()
|
||||||
// 初始化的时候 自动链接
|
// 初始化的时候 自动链接
|
||||||
if (store.getters.getToken !== '') {
|
if (store.getters.getToken !== '') {
|
||||||
getImToken().then(res => {
|
getImToken().then(res => {
|
||||||
@@ -59,7 +60,7 @@ const connect = (token, userInfo, callback) => {
|
|||||||
// 设置未读消息数量
|
// 设置未读消息数量
|
||||||
setNotifyBadge()
|
setNotifyBadge()
|
||||||
// 首次运行获取好友列表
|
// 首次运行获取好友列表
|
||||||
const FK = 'ZHK_' + userInfo.targetId
|
const FK = 'ZHKD_' + userInfo.targetId
|
||||||
|
|
||||||
uni.getStorage({
|
uni.getStorage({
|
||||||
key: FK,
|
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
|
let tipState = false
|
||||||
|
|
||||||
@@ -256,7 +121,8 @@ const triTone = () => {
|
|||||||
|
|
||||||
export default {
|
export default {
|
||||||
initIm,
|
initIm,
|
||||||
connect,
|
connect,
|
||||||
|
disconnect,
|
||||||
setNotifyBadge,
|
setNotifyBadge,
|
||||||
...message
|
...message
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,6 +1,14 @@
|
|||||||
import env from './conf/env.js'
|
import env from './conf/env.js'
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
|
inArray: (search, array) => {
|
||||||
|
for (var i in array) {
|
||||||
|
if (array[i] == search) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
},
|
||||||
checkPhone: (phone) => {
|
checkPhone: (phone) => {
|
||||||
let re = /^[0-9]+.?[0-9]*/;
|
let re = /^[0-9]+.?[0-9]*/;
|
||||||
if (phone.length === 11) {
|
if (phone.length === 11) {
|
||||||
|
|||||||
Reference in New Issue
Block a user