Commit ef32cf43 by liimin

refactor(recorder): 优化录音功能和记录管理

- 重构了 recorder 组件中的部分方法,提高了代码可读性和维护性
- 新增 clearSavedFiles 方法,用于清除已保存的录音文件
- 优化了录音启动和停止逻辑,提高了用户体验
- 调整了数据初始化和更新方法,确保数据一致性
1 parent 374fb370
/* /*
* @Author: 前端 qianduan01@didihu.com.cn * @Author: 前端 qianduan01@didihu.com.cn
* @Date: 2024-12-26 10:03:54 * @Date: 2024-12-26 10:03:54
* @LastEditors: yangming@didihu.com.cn yangming@didihu.com.cn * @LastEditors: limin
* @LastEditTime: 2025-04-15 17:59:20 * @LastEditTime: 2025-08-12 10:32:25
* @FilePath: \gl_tape\mixin\record\recorder.js * @FilePath: \gl_tape\mixin\record\recorder.js
* @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
*/ */
...@@ -12,12 +12,12 @@ import utils from '@/utils/utils.js' ...@@ -12,12 +12,12 @@ import utils from '@/utils/utils.js'
import { storageApi } from '@/utils/cache' import { storageApi } from '@/utils/cache'
import permision from '@/js_sdk/wa-permission/permission.js' import permision from '@/js_sdk/wa-permission/permission.js'
export default { export default {
provide () { provide() {
return { return {
getRecorder: () => this.record getRecorder: () => this.record
} }
}, },
async mounted () { async mounted() {
this.audioManager = new AudioManager(this.onAudioStart, this.onAudioStop, this.onError) this.audioManager = new AudioManager(this.onAudioStart, this.onAudioStop, this.onError)
this.initDto() this.initDto()
this.record = new TempRecord(this.tempRecord, this.triggleTempNumber) this.record = new TempRecord(this.tempRecord, this.triggleTempNumber)
...@@ -35,23 +35,23 @@ export default { ...@@ -35,23 +35,23 @@ export default {
// deep: true // deep: true
// }, // },
customerKey: { customerKey: {
handler () { handler() {
this.handler(this.customerInfo, this.updateCustomerInfo) this.handler(this.customerInfo, this.updateCustomerInfo)
}, },
deep: true deep: true
}, },
booksList: { booksList: {
handler (val) { handler(val) {
this.handler(val, this.updateBookList) this.handler(val, this.updateBookList)
} }
}, },
receptionStatus: { receptionStatus: {
handler (val) { handler(val) {
this.handler(val, this.updateReceptionStatus) this.handler(val, this.updateReceptionStatus)
} }
}, },
receptionKey: { receptionKey: {
handler (val) { handler(val) {
this.handler(this.receptionTrack, this.updateMaterialPointList) this.handler(this.receptionTrack, this.updateMaterialPointList)
} }
} }
...@@ -64,28 +64,28 @@ export default { ...@@ -64,28 +64,28 @@ export default {
...mapMutations('audio', ['setReceptionStatus', 'setBooks', 'setPerRecordNumber']), ...mapMutations('audio', ['setReceptionStatus', 'setBooks', 'setPerRecordNumber']),
...mapMutations(['clearCustomerInfo']), ...mapMutations(['clearCustomerInfo']),
//开始录音 //开始录音
startAudio: utils.throttle(async function () { startAudio: utils.throttle(async function (isInitData = true) {
const isPermission = await this.checkBeforeStart() const isPermission = await this.checkBeforeStart()
if (!isPermission) return if (!isPermission) return
uni.$logger.local.info('〖接待管理Recorder〗 == 开始接待 ') uni.$logger.local.info('〖接待管理Recorder〗 == 开始接待 ')
this.initReception() isInitData && this.initReception()
this.initTime()
this.audioManager.start() this.audioManager.start()
}, 300), }, 300),
initReception () { initReception() {
this.setBooks([]) this.setBooks([])
this.clearCustomerInfo() this.clearCustomerInfo()
this.setReceptionStatus('') this.setReceptionStatus('')
this.initTime()
}, },
async onAudioStart () { async onAudioStart() {
if (this.audioManager.counter === 0) { if (this.audioManager.counter === 0) {
await this.record.add() await this.record.add()
} else { } else {
this.record.addAudio() this.record.addAudio()
} }
}, },
async onAudioStop () { async onAudioStop() {
if (this.audioManager.isRecording) { if (this.audioManager.isRecording) {
uni.$logger.local.info('〖接待管理Recorder〗 == 接待中,再次开启录音! ') uni.$logger.local.info('〖接待管理Recorder〗 == 接待中,再次开启录音! ')
this.audioManager.start() this.audioManager.start()
...@@ -93,7 +93,7 @@ export default { ...@@ -93,7 +93,7 @@ export default {
uni.$logger.local.info('〖接待管理Recorder〗 == 断开录音! ') uni.$logger.local.info('〖接待管理Recorder〗 == 断开录音! ')
} }
}, },
onAudioError (error) { onAudioError(error) {
const message = error.message const message = error.message
this.reportError(`录音异常: error: ${message}`, message) this.reportError(`录音异常: error: ${message}`, message)
}, },
...@@ -102,38 +102,40 @@ export default { ...@@ -102,38 +102,40 @@ export default {
* @params {boolean} status true 不保存记录/AUDIO_STATUS.ABNORMAL 只要断开录音 * @params {boolean} status true 不保存记录/AUDIO_STATUS.ABNORMAL 只要断开录音
* @returns {void} * @returns {void}
*/ */
async stopAudio (isNotSave = false) { async stopAudio(isNotSave = false) {
uni.$logger.local.info('〖接待管理Recorder〗 == 结束接待! ') uni.$logger.local.info('〖接待管理Recorder〗 == 结束接待! ')
this.stop() this.stop()
await this.saveAndClearRecord(isNotSave) await this.saveAndClearRecord(isNotSave)
}, },
async saveAndClearRecord (isNotSave = false) { async saveAndClearRecord(isNotSave = false) {
if (isNotSave) { if (isNotSave) {
uni.$logger.local.info('〖接待管理Recorder〗 == 不保存记录! ') uni.$logger.local.info('〖接待管理Recorder〗 == 不保存记录! ')
return this.record.clearLocalRecord() this.record.clearSavedFiles()
this.record.clearLocalRecord()
return
} }
this.record.setReceptionEndTime() //设置接待结束时间 this.record.setReceptionEndTime() //设置接待结束时间
//如果不需要保存记录 //如果不需要保存记录
await this.record.save() await this.record.save()
}, },
stop () { stop() {
this.endWatchClick() this.endWatchClick()
this.record.clearLoopTimer() this.record.clearLoopTimer()
if (this.audioManager.isRecording) { if (this.audioManager.isRecording) {
this.audioManager.stop() this.audioManager.stop()
} }
}, },
handler (params, fn) { handler(params, fn) {
if (this.audioManager.isRecording || this.record?.currentRecord?.endReasonType == 1) { if (this.audioManager.isRecording || this.record?.currentRecord?.endReasonType == 1) {
fn.call(this, params) fn.call(this, params)
} }
}, },
reset () { reset() {
this.audioManager = null this.audioManager = null
this.record = null this.record = null
this.tempRecord = null this.tempRecord = null
}, },
initDto () { initDto() {
const { shopMerchantId, phone, sceneType, uuid, name, merchantId, channelType, merchantName } = storageApi.getUserInfo() const { shopMerchantId, phone, sceneType, uuid, name, merchantId, channelType, merchantName } = storageApi.getUserInfo()
const { deviceId: appCode } = uni.getDeviceInfo() const { deviceId: appCode } = uni.getDeviceInfo()
const dto = new RecordDto({ const dto = new RecordDto({
...@@ -146,12 +148,12 @@ export default { ...@@ -146,12 +148,12 @@ export default {
merchantId: shopMerchantId, merchantId: shopMerchantId,
merchantName, merchantName,
bussinessType: sceneType == 0 ? 1 : 2, // sceneType 0延保 1精品。 bussinessType 1.延保 2.轻改 bussinessType: sceneType == 0 ? 1 : 2, // sceneType 0延保 1精品。 bussinessType 1.延保 2.轻改
channelType: channelType || 1,//1问界 2 比亚迪 3 其他 channelType: channelType || 1 //1问界 2 比亚迪 3 其他
}) })
this.sceneType = sceneType this.sceneType = sceneType
this.tempRecord = dto this.tempRecord = dto
}, },
updateCustomerInfo (customerInfo = {}) { updateCustomerInfo(customerInfo = {}) {
const { customerName, phoneNumber, salesUuid, carModel, staffName: salesName, carBrand, carBrandId, oneCarBrand, oneCarBrandId } = customerInfo const { customerName, phoneNumber, salesUuid, carModel, staffName: salesName, carBrand, carBrandId, oneCarBrand, oneCarBrandId } = customerInfo
this.record.updatePreRecords({ this.record.updatePreRecords({
customerName, customerName,
...@@ -165,7 +167,7 @@ export default { ...@@ -165,7 +167,7 @@ export default {
oneCarBrandId oneCarBrandId
}) })
}, },
updateBookList (booksList) { updateBookList(booksList) {
const receptionCustomerPaySourceList = booksList.map(item => { const receptionCustomerPaySourceList = booksList.map(item => {
return { return {
id: item.id, id: item.id,
...@@ -175,7 +177,7 @@ export default { ...@@ -175,7 +177,7 @@ export default {
}) })
this.record.updatePreRecords({ receptionCustomerPaySourceList }) this.record.updatePreRecords({ receptionCustomerPaySourceList })
}, },
updateMaterialPointList (receptionTrack) { updateMaterialPointList(receptionTrack) {
const { merchantId, staffUuid, staffName } = this.tempRecord const { merchantId, staffUuid, staffName } = this.tempRecord
let receptionCustomerMaterialPointList = [] let receptionCustomerMaterialPointList = []
Object.keys(receptionTrack).map(key => { Object.keys(receptionTrack).map(key => {
...@@ -189,17 +191,17 @@ export default { ...@@ -189,17 +191,17 @@ export default {
}) })
this.record.updatePreRecords({ receptionCustomerMaterialPointList }) this.record.updatePreRecords({ receptionCustomerMaterialPointList })
}, },
updateReceptionStatus (status) { updateReceptionStatus(status) {
this.record.updatePreRecords({ receptionStatus: status }) this.record.updatePreRecords({ receptionStatus: status })
}, },
getRecorder () { getRecorder() {
return this.record return this.record
}, },
async triggleTempNumber () { async triggleTempNumber() {
const _records = await this.record.getRecords() const _records = await this.record.getRecords()
this.setPerRecordNumber(_records.length) this.setPerRecordNumber(_records.length)
}, },
async isPermission (hintText = '为了记录语音通话,请打开录音权限') { async isPermission(hintText = '为了记录语音通话,请打开录音权限') {
const result = await permision.requestAndroidPermission('android.permission.RECORD_AUDIO') const result = await permision.requestAndroidPermission('android.permission.RECORD_AUDIO')
await helpler.sleep(0) await helpler.sleep(0)
if (result !== 1) { if (result !== 1) {
...@@ -210,13 +212,13 @@ export default { ...@@ -210,13 +212,13 @@ export default {
} }
return result === 1 return result === 1
}, },
async checkBeforeStart () { async checkBeforeStart() {
const isPermission = await this.isPermission() const isPermission = await this.isPermission()
const isRecording = this.audioManager.isRecording const isRecording = this.audioManager.isRecording
return isPermission && !isRecording return isPermission && !isRecording
} }
}, },
beforeDestroy () { beforeDestroy() {
this.stop() this.stop()
this.reset() this.reset()
} }
......
...@@ -3,8 +3,8 @@ ...@@ -3,8 +3,8 @@
* @version: 1.0.0 * @version: 1.0.0
* @Author: limin * @Author: limin
* @Date: 2025-01-02 18:49:17 * @Date: 2025-01-02 18:49:17
* @LastEditors: 前端 qianduan01@didihu.com.cn * @LastEditors: limin
* @LastEditTime: 2025-04-11 15:59:20 * @LastEditTime: 2025-08-12 10:23:15
* @FilePath: \gl_tape\utils\recordManager\TempRecord.js * @FilePath: \gl_tape\utils\recordManager\TempRecord.js
*/ */
import utils from '../utils' import utils from '../utils'
...@@ -91,6 +91,17 @@ class TempRecord { ...@@ -91,6 +91,17 @@ class TempRecord {
record = null record = null
uni.$logger.local.info('〖接待记录Record〗 == 置空接待记录') uni.$logger.local.info('〖接待记录Record〗 == 置空接待记录')
} }
/**
* 清除已保存的录音文件
* @param {Object} options - 配置参数
* @param {Array} options.recordUrlList - 录音文件列表,默认为当前记录的recordUrlList
*/
clearSavedFiles({ recordUrlList = [] } = this.record) {
// 遍历录音文件列表并删除每个文件
recordUrlList.forEach(item => {
this.removeFile(item.file)
})
}
async __analysisUrlList(recordUrlList) { async __analysisUrlList(recordUrlList) {
if (!recordUrlList?.length) return [] if (!recordUrlList?.length) return []
try { try {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!