detail.js 7.51 KB
// packageReception/pages/detail/detail.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    typeLists: ['对话', '质检'],
    activeIndex: 0,
    status: 'pause',
    currentTime: '00:00', //当前音频的播放位置
    isOperate: true,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getReceptionBatchIdList(options)
  },
  async getReceptionBatchIdList(params) {
    const data = await wx.p.api.receptionApi.getReceptionBatchIdList(params)
    if (data && data.length) {
      this.setData({
        customerList: data,
        record: data[0]
      }, () => {
        this.setAudio()
      })
      this.getReceptionDetail()
    }
    wx.p.wxApi.getViewInfo('.reception-wrap', ({
      height
    }) => {
      let wrapHeight = `${height * 2}rpx`
      this.setData({
        receptionWrapStyle: `height:calc(100% - ${wrapHeight} - constant(safe-area-inset-bottom));height:calc(100% - ${wrapHeight} - env(safe-area-inset-bottom))`
      })
    })
  },
  changeDetail(e) {
    let {
      item
    } = e.currentTarget.dataset, {
      record
    } = this.data
    if (!item || item.batchId == record.batchId) return
    wx.showLoading({
      mask: true
    })
    this.qualityAudio.destroy()
    this.qualityRef().reset()
    this.setData({
      record: item,
      qualityData: [],
      status: 'pause',
      isOperate: true,
      currentTime: '00:00',
    }, () => {
      this.setAudio()
    })
    this.getReceptionDetail()
  },
  async getReceptionDetail() {
    try {
      wx.showLoading({
        mask: true
      })
      const {
        record: {
          batchId
        }
      } = this.data,
        data = await wx.p.api.receptionApi.getReceptionDetail({
          batchId
        })
      if (!data) throw '获取数据失败'
      data.map(item => {
        item.offset = Number(item.offset)
        item.formatScore = `${item.score || item.score === 0 ? item.score : '-'}/${item.sumScore || item.sumScore === 0 ? item.sumScore : '-'}`
        let hitTextList = JSON.parse(item.hitText),
          matchedClass = 'no-hit',
          matchedText = '意图不匹配'
        if (hitTextList && hitTextList.length) {
          item.hitTextList = hitTextList.map(e => e.replace(/\n/g, '<br>'))
          matchedClass = 'hit'
          matchedText = '意图匹配'
        }
        item.matchedClass = matchedClass
        item.matchedText = matchedText
        let [scriptRec] = JSON.parse(item.scriptRec)
        if (scriptRec) scriptRec = scriptRec.replace(/\n/g, '<br>')
        if (item.conversationDetailDTOList && item.conversationDetailDTOList.length) {
          item.conversationDetailDTOList.map(e => {
            e.time = this.formatTime(e.mergedOffset)
            e.text_id = `text_${e.mergedOffset}`
            if (e.mergedOffset == item.offset && !item.matched) {
              e.scriptRec = scriptRec
              item.matched = true
            }
          })
          if (item.conversationDetailDTOList.findIndex(e => e.scriptRec) == -1) {
            /**时间戳都对不上时 话术推荐加到最后 */
            item.conversationDetailDTOList[item.conversationDetailDTOList.length - 1].scriptRec = scriptRec
          }
        } else item.conversationDetailDTOList = [{
          scriptRec: scriptRec || '暂无建议'
        }]
      })
      this.setData({
        qualityData: data
      })
    } catch (error) {
      console.log(error)
      if (error) wx.p.wxApi.showToast(error)
    }
  },
  formatTime(seconds) {
    let minutes = Math.floor(seconds / 60);
    let remainingSeconds = Math.floor(seconds % 60); // 直接取整  

    minutes = minutes.toString().padStart(2, '0');
    remainingSeconds = remainingSeconds.toString().padStart(2, '0');

    return `${minutes}:${remainingSeconds}`;
  },
  navbarReady(e) {
    let {
      height
    } = e.detail
    this.setData({
      layoutStyle: `padding-top:${height}px`
    })
  },
  changeType(e) {
    let {
      index
    } = e.currentTarget.dataset
    this.setData({
      activeIndex: index
    })
  },
  onHandle(e) {
    let {
      isOperate
    } = this.data;
    if (isOperate) {
      this.play()
    } else {
      this.onPause()
    }
    this.setData({
      isOperate: !isOperate
    })
  },
  //音频暂停
  onPause() {
    this.dialogueRef().onPause()
    this.qualityAudio.pause()

  },
  //播放
  play() {
    const {
      openId
    } = this.data.userInfo
    wx.p.api.receptionApi.addCustomerDynamicRecord({
      customerOpenId: openId,
      goodsId: 1,
      goodsType: 1
    })
    if (!this.data.showPlayControl) {
      return wx.showToast({
        title: '音频暂不能播放,稍等...',
        icon: 'none'
      })
    }
    const {
      paused
    } = this.qualityAudio
    if (paused) this.qualityAudio.play()
  },
  //跳转到指定位置
  seek(time) {
    this.qualityAudio.seek(time)
  },
  setAudio() {
    const {
      record
    } = this.data
    this.qualityAudio = wx.createInnerAudioContext({
      useWebAudioImplement: true
    })
    this.qualityAudio.src = record.recordingUrl
    this.qualityAudio.onPause(() => {
      this.setData({
        status: 'pause'
      })
      this.setKeepScreenOn()
    })
    //监听音频的播放事件
    this.qualityAudio.onPlay(() => {
      this.setData({
        status: 'start'
      })
      this.setKeepScreenOn(true)
    })
    this.qualityAudio.onCanplay(() => {
      this.setData({
        showPlayControl: true
      })
    })
    this.qualityAudio.onTimeUpdate(this.onTimeUpdate)
    this.qualityAudio.onEnded(() => {
      this.setData({
        status: 'ending',
        currentTime: this.formatTime(this.qualityAudio.duration),
        isOperate: true
      })
      this.dialogueRef().setData({
        isPlayIng: false
      })
      this.setKeepScreenOn()
    })
  },
  onTimeUpdate() {
    this.setData({
      currentTime: this.formatTime(this.qualityAudio.currentTime)
    })
    this.dialogueRef().onTimeUpdate()
    this.qualityRef().onTimeUpdate()
  },
  dialogueRef() {
    return this.selectComponent('#dialogue')
  },
  qualityRef() {
    return this.selectComponent('#quality')
  },
  format(seconds) {
    seconds = Math.floor(seconds / 1000)
    let formattedTime = ''
    if (seconds < 60) return seconds + '秒'
    else {
      let hours = Math.floor(seconds / 3600),
        minutes = Math.floor((seconds % 3600) / 60),
        remainingSeconds = seconds % 60
      if (hours > 0) formattedTime += hours + '小时'
      if (minutes > 0 || hours > 0) formattedTime += minutes + '分'
      if (remainingSeconds > 0) formattedTime += remainingSeconds + '秒'
      return formattedTime
    }
  },
  //屏幕是否常亮
  setKeepScreenOn(keepScreenOn = false) {
    wx.setKeepScreenOn({
      keepScreenOn
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {
    this.qualityAudio.stop()
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {
    this.qualityAudio.stop()
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },
})