// components/recording/index.js const $$ = require('../../utils/util'); const app = getApp(); Component({ /** * 组件的属性列表 */ properties: {}, /** * 组件的初始数据 */ countdown: null, recordMannager: null, data: { imgUrl: $$.url.img, overlayShow: false, second: 60, // 录音时长 recordVisible: false, // 是否开启录音 popup: { visible: false, content: '', }, tooltipVisible: false, }, pageLifetimes: { hide: function () { if (this.countdown) { clearInterval(this.countdown); } if (this.recordMannager) { this.recordMannager.stop(); } }, }, /** * 组件的方法列表 */ methods: { // 使用改文字 _handleUseWords() { this.triggerEvent('getwords', this.data.popup.content); this.setData({ popup: { visible: false, content: '' }, overlayShow: false }); }, // 修改文字 _handleChangeWords(e) { this.data.popup.content = e.detail; this.setData({ popup: this.data.popup }); }, // 语音转文字 _transferText(e) { $$.showLoading(); let speakUrl = e.tempFilePath; let that = this; wx.uploadFile({ url: `${$$.baseUrl}${$$.url.sys}/api/v1/xfyun/speech`, filePath: speakUrl, name: 'fileNames', header: { Authorization: app.globalData.token }, complete(res) { $$.hideLoading(); if (res.errMsg === 'uploadFile:ok') { const { code, data, msg } = JSON.parse(res.data); if (code === '0' || code === 0) { that.setData({ popup: { visible: true, content: data, }, tooltipVisible: data ? true : false, }); setTimeout(() => { that.setData({ tooltipVisible: false }); }, 2000); } else { $$.showToast({ icon: 'error', title: msg }); } } else { $$.showToast({ icon: 'error', title: '录音转写失败' }); } }, }); }, // 录音开始触发 _startRecord() { let that = this; this.countdown = setInterval(() => { if (that.data.second - 1 <= 0) { that.recordMannager.stop(); } that.setData({ second: that.data.second - 1 }); }, 1000); this.setData({ second: 60, recordVisible: true }); }, // 录音结束触发 _endRecord(e) { if (this.countdown) { clearInterval(this.countdown); } if (60 - this.data.second <= 5) { $$.showToast({ title: '抱歉!录音时间过短,请重新录入' }); this.setData({ second: 60, recordVisible: false }); return false; } this.setData({ second: 60, recordVisible: false }); this._transferText(e); }, // 点击 开始说话 and 结束说话 _handleStartOrEnd() { // 结束说话 if (this.data.recordVisible) { this.recordMannager.stop(); return false; } $$.showLoading(); let that = this; wx.getSetting({ success(res) { if (res.authSetting['scope.record'] === false) { $$.hideLoading(); $$.showModal({ content: '抱歉!此功能需授权麦克风录音功能', confirmText: '跳转授权', success: (res) => { if (res.confirm) { wx.openSetting({ success(res) { if (res.authSetting['scope.record']) { $$.showToast({ title: '授权成功' }); } else { $$.showToast({ title: '授权失败' }); } }, }); } }, }); return false; } // 开始说话 if (!that.data.recordVisible) { const recordMannager = wx.getRecorderManager(); recordMannager.onStart(() => that._startRecord()); recordMannager.onStop((e) => that._endRecord(e)); recordMannager.onError((e) => { $$.showToast({ title: '抱歉!录音时间过短,请重新录入' }); that.setData({ second: 60, recordVisible: false }); }); const options = { duration: 60000, sampleRate: 16000, numberOfChannels: 1, encodeBitRate: 96000, format: 'pcm', }; recordMannager.start(options); that.recordMannager = recordMannager; $$.hideLoading(); } }, }); }, // 进入录音界面 _handleOpenRecording(e) { this.triggerEvent('onVisible', { visible: true }); this.setData({ overlayShow: true }); }, // 关闭录音界面 _handleHiddenRecording() { if (this.data.recordVisible) { return false; } this.triggerEvent('onVisible', { visible: false }); this.setData({ overlayShow: false }); }, // 关闭文字显示 _handleClosePopup() { this.setData({ popup: { visible: false } }); }, // 阻止冒泡 _catchtop() {}, }, });