IM部分的备注,消息发送代码分离
This commit is contained in:
1
App.vue
1
App.vue
@@ -5,6 +5,7 @@
|
||||
export default {
|
||||
onLaunch: function() {
|
||||
im.initIm('lmxuhwaglu76d')
|
||||
return
|
||||
//#ifdef APP-PLUS
|
||||
// 获取系统版本号
|
||||
getVersions({
|
||||
|
||||
@@ -15,9 +15,9 @@
|
||||
<view class="item" @click="scanQrCode">
|
||||
<uni-icons color="#555" type="scan" size="22"></uni-icons>
|
||||
</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>
|
||||
</view>
|
||||
</view> -->
|
||||
</view>
|
||||
</view>
|
||||
</view>
|
||||
@@ -131,6 +131,7 @@
|
||||
}
|
||||
},
|
||||
methods: {
|
||||
// 隐藏功能菜单
|
||||
hidePop() {
|
||||
this.showPop = false
|
||||
this.pickedItem = {}
|
||||
@@ -138,6 +139,7 @@
|
||||
this.showShade = false
|
||||
}, 250)
|
||||
},
|
||||
// 点击会话功能菜单
|
||||
pickerMenu(e) {
|
||||
const index = Number(e.currentTarget.dataset.index)
|
||||
|
||||
@@ -151,6 +153,7 @@
|
||||
this.getConversationList()
|
||||
this.hidePop()
|
||||
},
|
||||
// 长按会话,展示功能菜单
|
||||
onLongPress(e) {
|
||||
let [touches, style, item] = [e.touches[0], "", e.currentTarget.dataset.item]
|
||||
|
||||
@@ -218,6 +221,7 @@
|
||||
})
|
||||
}
|
||||
},
|
||||
// 调起扫码
|
||||
scanQrCode() {
|
||||
uni.scanCode({
|
||||
success: (res) => {
|
||||
|
||||
@@ -82,7 +82,6 @@
|
||||
importTabs: 1,
|
||||
showAudioTranscribe: false,
|
||||
transcribeTime: 60,
|
||||
audioSrc: '',
|
||||
audioContextPaused: true
|
||||
}
|
||||
},
|
||||
@@ -197,8 +196,7 @@
|
||||
clearInterval(transcribe)
|
||||
// 监听录音结束
|
||||
recorderManager.onStop(res => {
|
||||
if (res.tempFilePath) this.audioSrc = res.tempFilePath
|
||||
im.sendVoiceMsg(this.conversationType, this.targetId, res.tempFilePath, (60 - this
|
||||
im.sentVoice(this.conversationType, this.targetId, res.tempFilePath, (60 - this
|
||||
.transcribeTime), () => {
|
||||
this.getMessageList()
|
||||
})
|
||||
@@ -222,7 +220,7 @@
|
||||
'RC:CombineMsg'
|
||||
]
|
||||
const timeStamp = new Date().getTime()
|
||||
const count = 30 // 获取的消息数量
|
||||
const count = 10 // 获取的消息数量
|
||||
const isForward = true // 是否向前获取
|
||||
RongIMLib.getHistoryMessagesByTimestamp(this.conversationType, this.targetId, objectNames, timeStamp,
|
||||
count,
|
||||
@@ -240,8 +238,10 @@
|
||||
},
|
||||
send() {
|
||||
if (this.inputTxt === '') return
|
||||
im.sendMsg(this.conversationType, this.targetId, this.inputTxt, () => {
|
||||
this.getMessageList()
|
||||
im.sentText(this.conversationType, this.targetId, this.inputTxt, () => {
|
||||
setTimeout(() => {
|
||||
this.getMessageList()
|
||||
}, 500)
|
||||
this.inputTxt = ''
|
||||
})
|
||||
},
|
||||
@@ -260,7 +260,6 @@
|
||||
}, 100)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
</script>
|
||||
|
||||
|
||||
@@ -1,7 +1,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 {
|
||||
getFriends,
|
||||
getUserInfo,
|
||||
@@ -123,43 +123,45 @@ const addListeners = () => {
|
||||
newMessage(message)
|
||||
}
|
||||
})
|
||||
// 音视频通话相关的
|
||||
// 监听通话呼入
|
||||
CallLib.onCallReceived(({
|
||||
data
|
||||
}) => {
|
||||
console.log('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,
|
||||
@@ -179,7 +181,6 @@ const newMessage = (msg) => {
|
||||
}
|
||||
|
||||
uni.$emit('onReceiveMessage', msg);
|
||||
// store.dispatch('newMessage', msg)
|
||||
}
|
||||
|
||||
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 {
|
||||
initIm,
|
||||
connect,
|
||||
sendMsg,
|
||||
sendVoiceMsg,
|
||||
setNotifyBadge,
|
||||
syncFriends,
|
||||
syncUserInfo
|
||||
syncUserInfo,
|
||||
...message
|
||||
}
|
||||
|
||||
154
utils/im/message.js
Normal file
154
utils/im/message.js
Normal 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
|
||||
}
|
||||
Reference in New Issue
Block a user