diff --git a/apis/interfaces/im.js b/apis/interfaces/im.js index b6ff4d4..1c46001 100644 --- a/apis/interfaces/im.js +++ b/apis/interfaces/im.js @@ -4,7 +4,7 @@ * 碌碌无为枉半生,一朝惊醒万事空。 * moduleName: 聊聊 */ - +import store from '@/store' import { request } from '../index' @@ -84,9 +84,9 @@ const pedingFriend = (recipient) => { url: 'im/friends/' + recipient }) } - -/** - * 好友申请数量 + +/** + * 好友申请数量 */ const getPendingCount = () => { return request({ @@ -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 } diff --git a/components/im/imAUDIO.vue b/components/im/imAUDIO.vue index 33477ee..e253dff 100644 --- a/components/im/imAUDIO.vue +++ b/components/im/imAUDIO.vue @@ -2,7 +2,7 @@ - "{{msg.time}}" + "{{msg.duration}}" @@ -10,14 +10,16 @@ diff --git a/pages/im/private/chat.nvue b/pages/im/private/chat.nvue index 519fdbf..fa1d51c 100644 --- a/pages/im/private/chat.nvue +++ b/pages/im/private/chat.nvue @@ -7,11 +7,15 @@ {{ item.sentTime|timeCustomCN }} - + - - - + + + {{ item.sentStatus == 50 ? '已读': '未读'}} @@ -23,8 +27,10 @@ - - + + + + @@ -32,7 +38,8 @@ - + 发送 @@ -51,10 +58,10 @@ import imAUDIO from '@/components/im/imAUDIO' import imIMG from '@/components/im/imIMG' import imTXT from '@/components/im/imTXT' - + var transcribe var recorderManager = uni.getRecorderManager() - + const ChatList = uni.requireNativePlugin('dom') export default { data() { @@ -64,7 +71,6 @@ inputTxt: '', messages: [], conversationType: 1, - totalCount: 0, userInfo: { name: '', userId: '', @@ -77,30 +83,25 @@ audioContextPaused: true } }, - components:{ imAUDIO, imIMG, imTXT }, + components: { + imAUDIO, + imIMG, + imTXT + }, onLoad(e) { 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 }) - + RongIMLib.clearMessagesUnreadStatus(this.conversationType, this.targetId, new Date().getTime()) im.setNotifyBadge() RongIMLib.sendReadReceiptMessage(this.conversationType, this.targetId, new Date().getTime()) - + this.getMessageList() - + // 监听消息回执 RongIMLib.addReadReceiptReceivedListener(({ data @@ -109,8 +110,8 @@ this.getMessageList() } }) - - // 简童收到新消息,判断是否是当前会话,更新会话内容 + + // 监听收到新消息,判断是否是当前会话,更新会话内容 uni.$on('onReceiveMessage', (msg) => { if (msg.targetId == this.targetId) { RongIMLib.clearMessagesUnreadStatus(msg.conversationType, msg.targetId, msg.sentTime) @@ -185,19 +186,19 @@ }, // 结束录音 chendAudio(e) { - 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 - }) + 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() { // 获取消息列表 @@ -215,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, @@ -224,11 +225,7 @@ code, messages }) => { - - console.log(messages.reverse()) - if (code === 0) { - console.log(this.messages) this.messages = messages.reverse() this.scrollBottom() } @@ -236,7 +233,7 @@ ) }, send() { - if(this.inputTxt === '') return + if (this.inputTxt === '') return im.sendMsg(this.conversationType, this.targetId, this.inputTxt, () => { this.getMessageList() this.inputTxt = '' @@ -244,25 +241,26 @@ }, 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() { setTimeout(() => { - let el = this.$refs.chatBottom + let el = this.$refs.chatBottom ChatList.scrollToElement(el, { animated: true }) - },100) + }, 100) } } - + }