语音消息发送成功,播放暂时不支持带token的路径

This commit is contained in:
2022-01-30 17:46:36 +08:00
parent 3ae6ab881b
commit 83061d7c8e
4 changed files with 585 additions and 476 deletions

View File

@@ -4,7 +4,7 @@
* 碌碌无为枉半生,一朝惊醒万事空。
* moduleName: 聊聊
*/
import store from '@/store'
import {
request
} from '../index'
@@ -94,6 +94,49 @@ const getPendingCount = () => {
})
}
/**
* 上传聊天附件
* 图片
* 语音
* 视频
*/
// 基础配置
const config = {
apiUrl: 'http://api.zh.shangkelian.cn/api/', // 正式环境
timeout: 60000
}
const uploadMessageFile = (file, type) => {
config.header = {
'Accept': 'application/json',
'Authorization': store.getters.getToken || ''
}
return new Promise((resolve, reject) => {
uni.uploadFile({
url: config.apiUrl + 'im/upload',
filePath: file,
name: 'upload',
formData: {
type
},
header: config.header || {},
success: (res) => {
if (res.statusCode === 200) {
let updData = JSON.parse(res.data)
if (updData.status_code === 200) {
resolve(updData.data)
} else {
reject(updData)
}
}
},
fail: (err) => {
console.log('ERR', err);
}
})
})
}
export {
getImToken,
deleteFriend,
@@ -105,5 +148,6 @@ export {
rejectFriend,
searchFriend,
pedingFriend,
getPendingCount
getPendingCount,
uploadMessageFile
}

View File

@@ -2,7 +2,7 @@
<view>
<view class="im--audio" :class="guest ? 'right': 'left'" @click="onPlayMsg">
<image v-if="!guest" class="audio-mp3" src="@/static/icon/audio_green.png" mode="widthFix"></image>
<text class="audio-text">"{{msg.time}}"</text>
<text class="audio-text">"{{msg.duration}}"</text>
<image v-if="guest" class="audio-mp3" src="@/static/icon/audio_white.png" mode="widthFix"></image>
</view>
</view>
@@ -10,14 +10,16 @@
<script>
export default {
name:"im",
props:{
msg : {
type : Object,
name: "im",
props: {
msg: {
type: Object,
default: () => {
return {
src: "",
time: "20"
local: '',
remote: '',
objectName: '',
duration: 0
}
}
},
@@ -26,11 +28,12 @@
default: true
}
},
methods:{
methods: {
// 播放语音消息
onPlayMsg() {
console.log('播放', this.msg.remote);
let innerAudioContext = uni.createInnerAudioContext()
innerAudioContext.src = this.audioSrc
innerAudioContext.src = this.msg.remote
if (this.audioContextPaused) {
innerAudioContext.play()
this.audioContextPaused = false
@@ -38,15 +41,19 @@
}
innerAudioContext.stop()
innerAudioContext.onStop(resStop => {
console.log('停止');
this.audioContextPaused = true
})
innerAudioContext.onError(err => {
console.log(err);
})
}
}
}
</script>
<style scoped>
.im--audio{
.im--audio {
flex-direction: row;
justify-content: space-between;
align-items: center;
@@ -54,27 +61,33 @@
width: 170rpx;
padding: 0 20rpx;
box-sizing: border-box;
},
.im--audio.left{
}
,
.im--audio.left {
border-radius: 0 20rpx 20rpx 20rpx;
background: white;
}
.im--audio.right{
.im--audio.right {
border-radius: 20rpx 0 20rpx 20rpx;
background: #34CE98;
}
.audio-mp3{
.audio-mp3 {
width: 38rpx;
height: 38rpx;
}
.audio-text{
.audio-text {
font-size: 30rpx;
}
.im--audio.left .audio-text{
.im--audio.left .audio-text {
color: #333;
}
.im--audio.right .audio-text{
.im--audio.right .audio-text {
color: white;
}
</style>

View File

@@ -7,11 +7,15 @@
<text class="cell-time-text">{{ item.sentTime|timeCustomCN }}</text>
</view>
<view class="cell-item" :class="item.messageDirection == 1 ? 'right' : 'left'">
<image class="active" :src="userInfo.portraitUrl" mode="aspectFill" @click="showFriend(targetId, item.messageDirection)"></image>
<image class="active" :src="userInfo.portraitUrl" mode="aspectFill"
@click="showFriend(targetId, item.messageDirection)"></image>
<view class="msg">
<imAUDIO v-if="item.objectName === 'RC:VcMsg'" :guest="item.messageDirection == 1" :msg="item.content.content"></imAUDIO>
<imIMG v-if="item.objectName === 'RC:ImgMsg'" :guest="item.messageDirection == 1" :msg="item.content.content"></imIMG>
<imTXT v-if="item.objectName === 'RC:TxtMsg'" :guest="item.messageDirection == 1" :msg="item.content.content"></imTXT>
<imAUDIO v-if="item.objectName === 'RC:HQVCMsg'" :guest="item.messageDirection == 1"
:msg="item.content"></imAUDIO>
<imIMG v-if="item.objectName === 'RC:ImgMsg'" :guest="item.messageDirection == 1"
:msg="item.content.content"></imIMG>
<imTXT v-if="item.objectName === 'RC:TxtMsg'" :guest="item.messageDirection == 1"
:msg="item.content.content"></imTXT>
<view class="state" v-if="item.messageDirection == 1">
<text class="state-text">{{ item.sentStatus == 50 ? '已读': '未读'}}</text>
</view>
@@ -23,8 +27,10 @@
<!-- footer -->
<view class="chat-footer">
<view class="msg-type" @click="msgType">
<image class="msg-type-icon" src="@/static/icon/key-icon.png" v-if="importTabs === 0" mode="widthFix"></image>
<image class="msg-type-icon" src="@/static/icon/msg-icon.png" v-if="importTabs === 1" mode="widthFix"></image>
<image class="msg-type-icon" src="@/static/icon/key-icon.png" v-if="importTabs === 0" mode="widthFix">
</image>
<image class="msg-type-icon" src="@/static/icon/msg-icon.png" v-if="importTabs === 1" mode="widthFix">
</image>
</view>
<block v-if="importTabs === 0">
<view class="chat-mp3" hover-class="chat-hover" @touchstart="startAudio" @touchend="chendAudio">
@@ -32,7 +38,8 @@
</view>
</block>
<block v-if="importTabs === 1">
<input class="chat-input" type="text" v-model="inputTxt" confirm-type="发送" @confirm="send" cursor-spacing="10" />
<input class="chat-input" type="text" v-model="inputTxt" confirm-type="发送" @confirm="send"
cursor-spacing="10" />
</block>
<text class="chat-push" size="mini" @click="send">发送</text>
</view>
@@ -64,7 +71,6 @@
inputTxt: '',
messages: [],
conversationType: 1,
totalCount: 0,
userInfo: {
name: '',
userId: '',
@@ -77,23 +83,14 @@
audioContextPaused: true
}
},
components:{ imAUDIO, imIMG, imTXT },
components: {
imAUDIO,
imIMG,
imTXT
},
onLoad(e) {
console.log(e)
console.log(11111)
this.targetId = e.targetId
this.conversationType = e.conversationType // 会话类型
// 消息总数量
RongIMLib.getMessageCount(this.conversationType, this.targetId, ({
code,
count
}) => {
if (code == 0) {
this.totalCount = count
}
})
this.userInfo = this.$store.getters.userInfo(this.targetId)
uni.setNavigationBarTitle({
title: this.$store.getters.userInfo(this.targetId).name
@@ -114,13 +111,7 @@
}
})
// 监听录音结束
recorderManager.onStop(res => {
if (res.tempFilePath) this.audioSrc = res.tempFilePath
console.log('------------------获取到了录音的临时路径---------------')
console.log(res.tempFilePath)
})
// 简童收到新消息,判断是否是当前会话,更新会话内容
// 监听收到新消息,判断是否是当前会话,更新会话内容
uni.$on('onReceiveMessage', (msg) => {
if (msg.targetId == this.targetId) {
RongIMLib.clearMessagesUnreadStatus(msg.conversationType, msg.targetId, msg.sentTime)
@@ -198,8 +189,16 @@
if (!this.showAudioTranscribe) return
recorderManager.stop()
clearInterval(transcribe)
// 监听录音结束
recorderManager.onStop(res => {
if (res.tempFilePath) this.audioSrc = res.tempFilePath
im.sendVoiceMsg(this.conversationType, this.targetId, res.tempFilePath, (60 - this
.transcribeTime), () => {
this.getMessageList()
})
this.transcribeTime = 60
this.showAudioTranscribe = false
})
},
getMessageList() {
// 获取消息列表
@@ -217,7 +216,7 @@
'RC:CombineMsg'
]
const timeStamp = new Date().getTime()
const count = 20 // 获取的消息数量
const count = 30 // 获取的消息数量
const isForward = true // 是否向前获取
RongIMLib.getHistoryMessagesByTimestamp(this.conversationType, this.targetId, objectNames, timeStamp,
count,
@@ -227,7 +226,6 @@
messages
}) => {
if (code === 0) {
console.log(this.messages)
this.messages = messages.reverse()
this.scrollBottom()
}
@@ -235,7 +233,7 @@
)
},
send() {
if(this.inputTxt === '') return
if (this.inputTxt === '') return
im.sendMsg(this.conversationType, this.targetId, this.inputTxt, () => {
this.getMessageList()
this.inputTxt = ''
@@ -243,7 +241,8 @@
},
showFriend(targetId, type) {
uni.navigateTo({
url: type === 1 ? '/pages/im/friends/mine?targetId=' +targetId : '/pages/im/friends/info?targetId=' +targetId
url: type === 1 ? '/pages/im/friends/mine?targetId=' + targetId :
'/pages/im/friends/info?targetId=' + targetId
})
},
scrollBottom() {
@@ -252,7 +251,7 @@
ChatList.scrollToElement(el, {
animated: true
})
},100)
}, 100)
}
}
@@ -261,7 +260,7 @@
<style scoped>
.audio-transcribe {
background: rgba(0,0,0,.6);
background: rgba(0, 0, 0, .6);
position: fixed;
height: 200rpx;
width: 300rpx;
@@ -274,7 +273,7 @@
justify-content: center;
}
.audio-transcribe-src{
.audio-transcribe-src {
width: 88rpx;
height: 88rpx;
}
@@ -285,22 +284,26 @@
}
/* 窗口 */
.chat{
.chat {
background: #F3F6FB;
flex: 1;
}
.chat-scroll{
.chat-scroll {
flex: 1;
}
.cell{
padding:10rpx 30rpx;
.cell {
padding: 10rpx 30rpx;
}
.cell-time{
.cell-time {
justify-content: center;
align-items: center;
padding-bottom: 20rpx;
}
.cell-time-text{
.cell-time-text {
background: #fff;
font-size: 24rpx;
color: #666;
@@ -308,57 +311,70 @@
padding: 0 20rpx;
border-radius: 10rpx;
}
.cell-item{
.cell-item {
width: 690rpx;
justify-content: flex-start;
}
.cell-footer{
.cell-footer {
height: 20rpx;
}
.active{
.active {
width: 78rpx;
height: 78rpx;
background-color: white;
border-radius: 10rpx;
}
.msg{
.msg {
margin: 0 20rpx;
}
.state{
.state {
padding-top: 10rpx;
}
.state-text{
.state-text {
font-size: 24rpx;
color: #666;
}
.cell-item.left{
.cell-item.left {
flex-direction: row;
}
.cell-item.right{
.cell-item.right {
flex-direction: row-reverse;
}
.cell-item.right .state{
.cell-item.right .state {
flex-direction: row;
justify-content: flex-end;
}
/* footer */
.chat-footer{
.chat-footer {
background: white;
padding: 20rpx 30rpx;
display: flex;
justify-content: space-between;
flex-direction: row;
}
.msg-type{
.msg-type {
width: 70rpx;
height: 70rpx;
}
.msg-type > .msg-type-icon{
.msg-type>.msg-type-icon {
margin: 5rpx;
width: 60rpx;
height: 60rpx;
}
.chat-mp3{
.chat-mp3 {
background: #F3F6FB;
height: 70rpx;
line-height: 70rpx;
@@ -368,11 +384,13 @@
border-radius: 10rpx;
margin-right: 15rpx;
}
.chat-mp3-text{
.chat-mp3-text {
font-size: 30rpx;
color: #333;
}
.chat-input{
.chat-input {
background: #F3F6FB;
height: 70rpx;
width: 460rpx;
@@ -380,7 +398,8 @@
margin-right: 15rpx;
padding: 0 20rpx;
}
.chat-push{
.chat-push {
background: #34CE98;
color: white;
width: 120rpx;

View File

@@ -236,6 +236,38 @@ const sendMsg = (conversationType, targetId, content, callback) => {
})
}
/**
* 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);
}
})
}
/**
* 同步好友信息,保存头像地址等
*/
@@ -252,6 +284,7 @@ export default {
initIm,
connect,
sendMsg,
sendVoiceMsg,
setNotifyBadge,
syncFriends,
syncUserInfo