recorder.js 3.65 KB
/*
 * @Author: 前端 qianduan01@didihu.com.cn
 * @Date: 2024-04-08 11:28:48
 * @LastEditors: limin
 * @LastEditTime: 2024-12-06 14:44:19
 * @FilePath: \gl_tape\utils\recordManager\recorder.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import AudioManager from './AudioManager'
import Record from './Record'
const UPLOAD_STATUS = {
  // 上传状态
  success: 1,
  fail: 2
}
const AUDIO_STATUS = {
  START: 'start', //录音中
  END: 'end', //录音结束
  ABNORMAL: 'abnormal' //40分钟未触碰平板,断开录音,不提交记录
}
import { mapMutations, mapState } from 'vuex'
import utils from '@/utils/utils.js'
export default {
  data() {
    return {
      voicePath: '',
      times: 0,
      timeout: 240,
      reaction: 1,
      audioManager: null,
      record: null
    }
  },
  async mounted() {
    this.audioManager = AudioManager.getInstance()
    this.record = this.audioManager.record
    this.recovery()
  },
  watch: {
    /**
     * 监听客户信息是否发生变化
     * @param {object} val 监听客户信息
     */
    changeData: {
      handler(val) {
        console.log(val, 'val', this.audioInfo.audioStatus)
        if (!this.audioManager.isRecording) return
        const { customerInfo, booksList, receptionStatus } = val
        this.record.updatePreRecords(customerInfo, booksList, receptionStatus)
      },
      deep: true
    }
  },
  computed: {
    ...mapState('audio', ['receptionStatus', 'booksList', 'audioInfo']),
    ...mapState(['user.customerInfo']),
    changeData() {
      const customerInfo = this.$store?.state?.user?.customerInfo || {}
      const booksList = this.booksList || []
      const receptionStatus = this.receptionStatus
      return { customerInfo, booksList, receptionStatus }
    }
  },
  methods: {
    ...mapMutations('audio', ['setReceptionStatus', 'setBooks', 'setAudioInfo']),
    //开始录音
    startAudio: utils.throttle(async function () {
      if (this.audioManager.isRecording) return
      uni.$logger.local.info('开始接待')
      this.setBooks([])
      this.$store.dispatch('clearCustomerInfo')
      this.setReceptionStatus('')
      this.initTime()
      // this.setAudioInfo({ audioList: [], audioStatus: AUDIO_STATUS.START })
      this.audioManager.start()
    }, 300),

    onAudioStart() {
      if (this.audioManager.counter === 0) {
        this.record.add(this.customerInfo, this.booksList, this.receptionStatus)
      } else {
        this.record.addAudio()
      }
    },
    onAudioStop() {
      if (this.audioManager.isRecording) {
        uni.$logger.local.info('接待中,再次开启录音!')
        this.audioManager.start()
      } else {
        uni.$logger.local.info('断开录音!')
      }
    },
    onAudioError(error) {
      const message = error.message
      this.reportError(`录音异常: error: ${message}`, message)
    },
    async recovery() {
      const records = this.record.getRecords()
      if (!records.length) return
      while (records.length) {
        const record = records.shift()
        await this.record.save(record)
      }
    },
    /**
     * 结束接待
     * @params {boolean} status true 不保存记录/AUDIO_STATUS.ABNORMAL 只要断开录音
     * @returns {void}
     */
    stopAudio(status) {
      this.endWatchClick()
      this.audioManager.stop()
      const preRecord = this.record.preRecord
      //直接退出
      if (status) {
        uni.$logger.local.info('退出接待,不保存记录!')
        this.record.removeRecord(preRecord.id)
        return
      }
      this.record.save(preRecord)
    }
  }
}