IM部分的备注,消息发送代码分离

This commit is contained in:
2022-02-07 11:01:36 +08:00
parent d19f1378a6
commit b202a62d5d
5 changed files with 807 additions and 713 deletions

View File

@@ -5,6 +5,7 @@
export default { export default {
onLaunch: function() { onLaunch: function() {
im.initIm('lmxuhwaglu76d') im.initIm('lmxuhwaglu76d')
return
//#ifdef APP-PLUS //#ifdef APP-PLUS
// 获取系统版本号 // 获取系统版本号
getVersions({ getVersions({

View File

@@ -15,9 +15,9 @@
<view class="item" @click="scanQrCode"> <view class="item" @click="scanQrCode">
<uni-icons color="#555" type="scan" size="22"></uni-icons> <uni-icons color="#555" type="scan" size="22"></uni-icons>
</view> </view>
<view class="item" @click="onNav('', {})"> <!-- <view class="item" @click="onNav('', {})">
<uni-icons color="#555" custom-prefix="iconfont" type="icon-gengduo2" size="22"></uni-icons> <uni-icons color="#555" custom-prefix="iconfont" type="icon-gengduo2" size="22"></uni-icons>
</view> </view> -->
</view> </view>
</view> </view>
</view> </view>
@@ -131,6 +131,7 @@
} }
}, },
methods: { methods: {
// 隐藏功能菜单
hidePop() { hidePop() {
this.showPop = false this.showPop = false
this.pickedItem = {} this.pickedItem = {}
@@ -138,6 +139,7 @@
this.showShade = false this.showShade = false
}, 250) }, 250)
}, },
// 点击会话功能菜单
pickerMenu(e) { pickerMenu(e) {
const index = Number(e.currentTarget.dataset.index) const index = Number(e.currentTarget.dataset.index)
@@ -151,6 +153,7 @@
this.getConversationList() this.getConversationList()
this.hidePop() this.hidePop()
}, },
// 长按会话,展示功能菜单
onLongPress(e) { onLongPress(e) {
let [touches, style, item] = [e.touches[0], "", e.currentTarget.dataset.item] let [touches, style, item] = [e.touches[0], "", e.currentTarget.dataset.item]
@@ -218,6 +221,7 @@
}) })
} }
}, },
// 调起扫码
scanQrCode() { scanQrCode() {
uni.scanCode({ uni.scanCode({
success: (res) => { success: (res) => {

View File

@@ -82,7 +82,6 @@
importTabs: 1, importTabs: 1,
showAudioTranscribe: false, showAudioTranscribe: false,
transcribeTime: 60, transcribeTime: 60,
audioSrc: '',
audioContextPaused: true audioContextPaused: true
} }
}, },
@@ -197,8 +196,7 @@
clearInterval(transcribe) clearInterval(transcribe)
// 监听录音结束 // 监听录音结束
recorderManager.onStop(res => { recorderManager.onStop(res => {
if (res.tempFilePath) this.audioSrc = res.tempFilePath im.sentVoice(this.conversationType, this.targetId, res.tempFilePath, (60 - this
im.sendVoiceMsg(this.conversationType, this.targetId, res.tempFilePath, (60 - this
.transcribeTime), () => { .transcribeTime), () => {
this.getMessageList() this.getMessageList()
}) })
@@ -222,7 +220,7 @@
'RC:CombineMsg' 'RC:CombineMsg'
] ]
const timeStamp = new Date().getTime() const timeStamp = new Date().getTime()
const count = 30 // 获取的消息数量 const count = 10 // 获取的消息数量
const isForward = true // 是否向前获取 const isForward = true // 是否向前获取
RongIMLib.getHistoryMessagesByTimestamp(this.conversationType, this.targetId, objectNames, timeStamp, RongIMLib.getHistoryMessagesByTimestamp(this.conversationType, this.targetId, objectNames, timeStamp,
count, count,
@@ -240,8 +238,10 @@
}, },
send() { send() {
if (this.inputTxt === '') return if (this.inputTxt === '') return
im.sendMsg(this.conversationType, this.targetId, this.inputTxt, () => { im.sentText(this.conversationType, this.targetId, this.inputTxt, () => {
setTimeout(() => {
this.getMessageList() this.getMessageList()
}, 500)
this.inputTxt = '' this.inputTxt = ''
}) })
}, },
@@ -260,7 +260,6 @@
}, 100) }, 100)
} }
} }
} }
</script> </script>

View File

@@ -1,7 +1,7 @@
import * as RongIMLib from '@/uni_modules/RongCloud-IMWrapper/js_sdk/index' import * as RongIMLib from '@/uni_modules/RongCloud-IMWrapper/js_sdk/index'
import * as CallLib from '@/uni_modules/RongCloud-CallWrapper/lib/index' import * as CallLib from '@/uni_modules/RongCloud-CallWrapper/lib/index'
import store from '@/store/index.js' import store from '@/store/index.js'
import message from './message.js'
import { import {
getFriends, getFriends,
getUserInfo, getUserInfo,
@@ -123,43 +123,45 @@ const addListeners = () => {
newMessage(message) newMessage(message)
} }
}) })
// 音视频通话相关的
// 监听通话呼入 // 监听通话呼入
CallLib.onCallReceived(({ CallLib.onCallReceived(({
data data
}) => { }) => {
console.log('onCallReceived', data)
uni.navigateTo({ uni.navigateTo({
url: '/pages/im/private/call?targetId=' + data.targetId + '&mediaType=' + url: '/pages/im/private/call?targetId=' + data.targetId + '&mediaType=' +
data.mediaType data.mediaType
}) })
}) })
// 通话建立成功 // 通话建立成功
CallLib.onCallConnected(() => { CallLib.onCallConnected(() => {
uni.$emit('onCallConnected'); uni.$emit('onCallConnected');
}) })
// 外呼
CallLib.onCallOutgoing((res) => { CallLib.onCallOutgoing((res) => {
uni.$emit('onCallOutgoing'); uni.$emit('onCallOutgoing');
}) })
// 远端响铃
CallLib.onRemoteUserRinging((res) => { CallLib.onRemoteUserRinging((res) => {
uni.$emit('onRemoteUserRinging'); uni.$emit('onRemoteUserRinging');
}) })
// 远端加入
CallLib.onRemoteUserJoined((res) => { CallLib.onRemoteUserJoined((res) => {
uni.$emit('onRemoteUserJoined'); uni.$emit('onRemoteUserJoined');
}) })
// 断开链接
CallLib.onCallDisconnected((res) => { CallLib.onCallDisconnected((res) => {
console.log('断开链接', res); console.log('断开链接', res);
uni.$emit('onCallDisconnected'); uni.$emit('onCallDisconnected');
}) })
// 远端挂断
CallLib.onRemoteUserLeft((res) => { CallLib.onRemoteUserLeft((res) => {
console.log('远端离开', res); console.log('远端离开', res);
uni.$emit('onRemoteUserLeft'); uni.$emit('onRemoteUserLeft');
}) })
} }
// 维护消息列表 // 维护消息列表,检查是否需要通知声音,设置新消息提醒的数量
const newMessage = (msg) => { const newMessage = (msg) => {
RongIMLib.getConversationNotificationStatus(msg.conversationType, msg.targetId, ({ RongIMLib.getConversationNotificationStatus(msg.conversationType, msg.targetId, ({
code, code,
@@ -179,7 +181,6 @@ const newMessage = (msg) => {
} }
uni.$emit('onReceiveMessage', msg); uni.$emit('onReceiveMessage', msg);
// store.dispatch('newMessage', msg)
} }
function syncUserInfo(targetId) { function syncUserInfo(targetId) {
@@ -205,70 +206,6 @@ const triTone = () => {
} }
} }
/**
* 发送消息
* @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: '发送失败'
})
}
})
}
/**
* conversationType 会话类型
* voiceUrl 本地的录音路径
* time 录音时长
*/
const sendVoiceMsg = (conversationType, targetId, voiceUrl, time, callback) => {
console.log('sendVoiceMsg', plus.io.convertLocalFileSystemURL(voiceUrl));
const msg = {
conversationType: conversationType,
targetId: String(targetId),
content: {
objectName: 'RC:HQVCMsg',
local: 'file:///' + plus.io.convertLocalFileSystemURL(voiceUrl),
duration: time
}
}
RongIMLib.sendMediaMessage(msg, {
success: (messageId) => {
callback(messageId);
},
progress: (progress, messageId) => {
console.log(messageId);
},
cancel: (messageId) => {
// 发送取消回调
},
error: (errorCode, messageId) => {
console.log(errorCode, messageId);
}
})
}
/** /**
* 同步好友信息,保存头像地址等 * 同步好友信息,保存头像地址等
*/ */
@@ -284,9 +221,8 @@ const syncFriends = () => {
export default { export default {
initIm, initIm,
connect, connect,
sendMsg,
sendVoiceMsg,
setNotifyBadge, setNotifyBadge,
syncFriends, syncFriends,
syncUserInfo syncUserInfo,
...message
} }

154
utils/im/message.js Normal file
View File

@@ -0,0 +1,154 @@
import store from '@/store/index.js'
import * as RongIMLib from '@/uni_modules/RongCloud-IMWrapper/js_sdk/index'
/**
* 发送文本消息
* @param {number} conversationType 消息类型
* @param {string} targetId 会话id
* @param {string} content 消息内容
* @param {function} callback 回调函数
*/
const sentText = (conversationType, targetId, content, callback) => {
console.log('发送');
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: '发送失败'
})
}
})
}
/**
* 发送消息
* @param {number} conversationType 消息类型
* @param {string} targetId 会话id
* @param {string} voiceUrl 录音的本地路径
* @param {integer} time 录音时长
* @param {function} callback 录音时长
*/
const sentVoice = (conversationType, targetId, voiceUrl, time, callback) => {
const msg = {
conversationType: conversationType,
targetId: String(targetId),
content: {
objectName: 'RC:HQVCMsg',
local: 'file:///' + plus.io.convertLocalFileSystemURL(voiceUrl),
duration: time
}
}
RongIMLib.sendMediaMessage(msg, {
success: (messageId) => {
callback(messageId);
},
progress: (progress, messageId) => {
console.log(messageId);
},
cancel: (messageId) => {
// 发送取消回调
},
error: (errorCode, messageId) => {
console.log(errorCode, messageId);
}
})
}
const sentImage = (conversationType, targetId, imageUrl, time, callback) => {
const msg = {
conversationType: conversationType,
targetId: String(targetId),
content: {
objectName: 'RC:ImgMsg',
local: 'file:///' + plus.io.convertLocalFileSystemURL(imageUrl),
duration: time
}
}
RongIMLib.sendMediaMessage(msg, {
success: (messageId) => {
callback(messageId);
},
progress: (progress, messageId) => {
console.log(messageId);
},
cancel: (messageId) => {
// 发送取消回调
},
error: (errorCode, messageId) => {
console.log(errorCode, messageId);
}
})
}
const sentGif = (conversationType, targetId, gifUrl, time, callback) => {
const msg = {
conversationType: conversationType,
targetId: String(targetId),
content: {
objectName: 'RC:GIFMsg',
local: 'file:///' + plus.io.convertLocalFileSystemURL(gifUrl),
duration: time
}
}
RongIMLib.sendMediaMessage(msg, {
success: (messageId) => {
callback(messageId);
},
progress: (progress, messageId) => {
console.log(messageId);
},
cancel: (messageId) => {
// 发送取消回调
},
error: (errorCode, messageId) => {
console.log(errorCode, messageId);
}
})
}
const sendFile = (conversationType, targetId, fileUrl, time, callback) => {
const msg = {
conversationType: conversationType,
targetId: String(targetId),
content: {
objectName: 'RC:FileMsg',
local: 'file:///' + plus.io.convertLocalFileSystemURL(fileUrl),
duration: time
}
}
RongIMLib.sendMediaMessage(msg, {
success: (messageId) => {
callback(messageId);
},
progress: (progress, messageId) => {
console.log(messageId);
},
cancel: (messageId) => {
// 发送取消回调
},
error: (errorCode, messageId) => {
console.log(errorCode, messageId);
}
})
}
export default {
sentText,
sentVoice,
sentImage,
sentGif,
sendFile
}