From 8cc3ed9352ebcd557b15a7b1cb93f2f1b4889c0d Mon Sep 17 00:00:00 2001 From: xsx <825657193@qq.com> Date: Sun, 31 Mar 2024 17:38:32 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E7=A7=BB=E5=8A=A8=E7=AB=AF=E6=94=AF?= =?UTF-8?q?=E6=8C=81=E8=AF=AD=E9=9F=B3=E6=B6=88=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- im-uniapp/common/recorder-app.js | 62 +++++ im-uniapp/common/recorder-h5.js | 54 +++++ .../chat-message-item/chat-message-item.vue | 96 +++++--- .../components/chat-record/chat-record.vue | 227 ++++++++++++++++++ im-uniapp/main.js | 8 + im-uniapp/package.json | 10 +- im-uniapp/pages/chat/chat-box.vue | 78 ++++-- im-uniapp/static/icon/iconfont.css | 14 +- im-uniapp/static/icon/iconfont.ttf | Bin 6696 -> 7216 bytes 9 files changed, 497 insertions(+), 52 deletions(-) create mode 100644 im-uniapp/common/recorder-app.js create mode 100644 im-uniapp/common/recorder-h5.js create mode 100644 im-uniapp/components/chat-record/chat-record.vue diff --git a/im-uniapp/common/recorder-app.js b/im-uniapp/common/recorder-app.js new file mode 100644 index 0000000..dce0cbf --- /dev/null +++ b/im-uniapp/common/recorder-app.js @@ -0,0 +1,62 @@ +import UNI_APP from '@/.env.js'; + +const rc = uni.getRecorderManager(); +// 录音开始时间 +let startTime = null; + +let start = () => { + return new Promise((resolve, reject) => { + rc.onStart(() => { + startTime = new Date(); + resolve() + }); + rc.onError((e) => { + console.log(e); + reject(e) + }) + rc.start({ + format: 'mp3' // 录音格式,可选值:aac/mp3 + }); + }) +} + +let pause = () => { + rc.stop(); +} + +let close = () => { + rc.stop(); +} + +let upload = () => { + return new Promise((resolve, reject) => { + rc.onStop((wavFile, a, b) => { + uni.uploadFile({ + url: UNI_APP.BASE_URL + '/file/upload', + header: { + accessToken: uni.getStorageSync("loginInfo").accessToken + }, + filePath: wavFile.tempFilePath, + name: 'file', + success: (res) => { + const duration = (new Date().getTime() - startTime.getTime()) / 1000 + const data = { + duration: Math.round(duration), + url: JSON.parse(res.data).data + } + resolve(data); + }, + fail: (e) => { + reject(e); + } + }) + }); + }) +} + +export { + start, + pause, + close, + upload +} \ No newline at end of file diff --git a/im-uniapp/common/recorder-h5.js b/im-uniapp/common/recorder-h5.js new file mode 100644 index 0000000..1f435ca --- /dev/null +++ b/im-uniapp/common/recorder-h5.js @@ -0,0 +1,54 @@ +import Recorder from 'js-audio-recorder'; +import UNI_APP from '@/.env.js'; + +let rc = null; +let start = () => { + if(rc != null){ + close(); + } + rc = new Recorder(); + return rc.start(); +} + +let pause = () => { + rc.pause(); +} + +let close = () => { + rc.destroy(); + rc = null; +} + +let upload = () => { + return new Promise((resolve, reject) => { + const wavBlob = rc.getWAVBlob(); + const newbolb = new Blob([wavBlob], { type: 'audio/wav'}) + const name = new Date().getDate() + '.wav'; + const file = new File([newbolb], name) + uni.uploadFile({ + url: UNI_APP.BASE_URL + '/file/upload', + header: { + accessToken: uni.getStorageSync("loginInfo").accessToken + }, + file: file, + name: 'file', + success: (res) => { + const data = { + duration: parseInt(rc.duration), + url: JSON.parse(res.data).data + } + resolve(data); + }, + fail: (e) => { + reject(e); + } + }) + }) +} + +export { + start, + pause, + close, + upload +} \ No newline at end of file diff --git a/im-uniapp/components/chat-message-item/chat-message-item.vue b/im-uniapp/components/chat-message-item/chat-message-item.vue index 74be926..6f69044 100644 --- a/im-uniapp/components/chat-message-item/chat-message-item.vue +++ b/im-uniapp/components/chat-message-item/chat-message-item.vue @@ -1,13 +1,16 @@