timeout.js 2.27 KB
/*
 * @Author: 前端 qianduan01@didihu.com.cn
 * @Date: 2024-04-08 11:28:48
 * @LastEditors: limin
 * @LastEditTime: 2025-12-25 09:19:58
 * @FilePath: \simgle_app\mixin\timeout.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
const TIMER_DURATION = 40 * 60 * 1000 //40分钟
const TIMER_INTERVAL = 1 * 60 * 1000 //1分钟检一次
const TIMER_TIMEOUT = TIMER_DURATION / TIMER_INTERVAL
export default {
  methods: {
    //结束提示
    stopRecorder() {
      if (typeof this.setReceptionStatus === 'function') {
        this.setReceptionStatus(0)
      }
      if (this.record && typeof this.record.withError === 'function') {
        this.record.withError()
      }
      if (typeof this.stop === 'function') {
        this.stop()
      }
      uni.$logger.local.info(`==${TIMER_DURATION / 60000} 分钟未触碰平板断开录音 ==`)
    },
    //监听点击事件
    initTime() {
      uni.$logger.local.info('== 触碰平板开始计时 ==')
      this.endWatchClick()
      this.__counter = 0
      this.__timer = null
      this.__timer = setInterval(() => {
        uni.$logger.local.info('== 未触碰平板计时 ==' + this.__counter + '分钟' + '== __timer:' + this.__timer)
        if (this.__counter >= TIMER_TIMEOUT) {
          //关闭录音
          uni.$logger.local.info(`== ${TIMER_DURATION / 60000} 分钟未触碰平板 == __timer:${this.__timer} __counter:${this.__counter}分钟`)
          this.endWatchClick()
          this.stopRecorder()
          return
        }
        this.__counter++
      }, TIMER_INTERVAL)
    },
    endWatchClick() {
      if (this.__timer) {
        clearInterval(this.__timer)
        uni.$logger.local.info('== 停止监听未触碰平板事件 == __timer:' + this.__timer + ' __counter:' + this.__counter)
      }
      this.__timer = null
      this.__counter = 0
    },
    handleTouchStart() {
      if (this.__counter > 0) {
        uni.$logger.local.info(`== ${this.__counter}分钟后触碰平板,重置计数器  == __timer:${this.__timer}`)
      }
      this.__counter = 0
    }
  },
  beforeDestroy() {
    if (this.__timer) {
      clearInterval(this.__timer)
    }
    this.__timer = null
    this.__counter = 0
  }
}