From 83061d7c8e9e43b27a821cb69ac969ea0e154ef8 Mon Sep 17 00:00:00 2001 From: Jason Date: Sun, 30 Jan 2022 17:46:36 +0800 Subject: [PATCH] =?UTF-8?q?=E8=AF=AD=E9=9F=B3=E6=B6=88=E6=81=AF=E5=8F=91?= =?UTF-8?q?=E9=80=81=E6=88=90=E5=8A=9F=EF=BC=8C=E6=92=AD=E6=94=BE=E6=9A=82?= =?UTF-8?q?=E6=97=B6=E4=B8=8D=E6=94=AF=E6=8C=81=E5=B8=A6token=E7=9A=84?= =?UTF-8?q?=E8=B7=AF=E5=BE=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apis/interfaces/im.js | 54 ++- components/im/imAUDIO.vue | 171 ++++---- pages/im/private/chat.nvue | 801 +++++++++++++++++++------------------ utils/im/index.js | 35 +- 4 files changed, 585 insertions(+), 476 deletions(-) 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 4cf74ad..8fad659 100644 --- a/components/im/imAUDIO.vue +++ b/components/im/imAUDIO.vue @@ -1,80 +1,93 @@ - - - - - diff --git a/pages/im/private/chat.nvue b/pages/im/private/chat.nvue index af3096d..cdc89d3 100644 --- a/pages/im/private/chat.nvue +++ b/pages/im/private/chat.nvue @@ -1,393 +1,412 @@ - - - - - diff --git a/utils/im/index.js b/utils/im/index.js index d65a54e..55b8988 100644 --- a/utils/im/index.js +++ b/utils/im/index.js @@ -117,7 +117,7 @@ const addListeners = () => { RongIMLib.addReceiveMessageListener((res) => { console.log('收到消息', res.data.message); const message = res.data.message - if (inArray(message.objectName, notifyMsgTypes)) { + if (inArray(message.objectName, notifyMsgTypes)) { console.log('new Message'); newMessage(message) } @@ -235,6 +235,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