receptionDetail.js 6.94 KB
// packageEquity/pages/receptionDetail/receptionDetail.js
import reportPageTrackMixin from '~/mixins/reportPageTrackMixin'
Page({
  behaviors: [reportPageTrackMixin],

  /**
   * 页面的初始数据
   */
  data: {
    navBarHeight: 113.5,
    typeLists: [
      "对话",
      "质检",
      "攻防"
    ],
    avgDefence: '',
    qualityAudio: '',
    activeIndex: 0,
    status: 'pause',
    currentTime: '00:00', //当前音频的播放位置
    isOperate: true,
    track_event_code: 'reception_detail', //埋点动作类型
  },
  
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },
  back() {
    this.qualityAudio.pause()
    wx.$router.back()

  },
  onHandle(e) {
    let {
      isOperate
    } = this.data;
    if (isOperate) {
      this.play()
    } else {
      this.onPause()
    }
    this.setTrackData(isOperate ? 'play_audio' : 'pause_audio', {
      mergedOffset: this.qualityAudio.currentTime
    })
    this.setData({
      isOperate: !isOperate
    })
  },
  //音频暂停
  onPause() {
    const {
      activeIndex
    } = this.data
    if (activeIndex == 0) this.dialogueRef().onPause()
    if (activeIndex == 1) this.qualityRef().clearData()
    this.qualityAudio.pause()

  },
  //播放
  play() {
    const {
      paused
    } = this.qualityAudio
    if (paused) this.qualityAudio.play()
  },
  practiceHandle(stage) {
    let {
      tag,
      batchId
    } = this.data.record;
    let _this = this;
    let regex = new RegExp('环节', 'g')
    stage = stage.replace(regex, '')
    this.setTrackData('quick_exercise')
    wx.showModal({
      title: '温馨提示',
      content: '是否前往AI趣练练习该对话环节',
      success(res) {
        if (res.confirm) {
          _this.onPause()
          console.log(tag, stage)
          if (tag && stage) {
            wx.navigateTo({
              url: `/packageEquity/pages/aiCheck/chat/chat?stage=${stage}&roleName=${tag}`
            })
          } else {
            wx.navigateTo({
              url: `/packageEquity/pages/aiCheck/scene/scene?stage=${stage}&roleName=${tag}`
            })
          }

        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  //跳转到指定位置
  seek(time) {
    this.qualityAudio.seek(time)
  },
  setAudio() {
    this.qualityAudio = wx.createInnerAudioContext({
      useWebAudioImplement: true
    })
    this.qualityAudio.src = this.data.record.fileUrl
    this.qualityAudio.onPause(() => {
      this.setData({
        status: 'pause'
      })
      this.setKeepScreenOn()
    })
    //监听音频的播放事件
    this.qualityAudio.onPlay(() => {
      this.setData({
        status: 'start'
      })
      this.setKeepScreenOn(true)
    })
    this.qualityAudio.onTimeUpdate(this.onTimeUpdate)
    this.qualityAudio.onEnded(() => {
      this.setData({
        status: 'ending',
        currentTime: this.formatTime(this.qualityAudio.duration)
      })
      this.dialogueRef().setData({
        isPlayIng: false
      })
      this.setKeepScreenOn()
    })
  },
  onTimeUpdate() {
    const {
      activeIndex
    } = this.data
    this.setData({
      currentTime: this.formatTime(this.qualityAudio.currentTime)
    })
    if (activeIndex == 0) this.dialogueRef().onTimeUpdate()
    if (activeIndex == 1) this.qualityRef().onTimeUpdate()
  },
  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}`;
  },
  onRoute(options) {
    if (Object.keys(options).length) {
      options.staticDate = new Date(options.staticDate).format('MM月dd日')
      this.track_businessParam = {
        batchId: options.batchId
      } //埋点业务参数
      
      this.setData({
        record: options
      }, () => {
        this.setAudio()
      })
    }

  },
  changeType(e) {
    let {
      index
    } = e.currentTarget.dataset
    this.setTrackData({
      0: 'dialogue',
      1: 'quality_inspection',
      2: 'offense_and_defense'
    } [index])
    this.setData({
      activeIndex: index
    })
    if (index != 2) {
      this.setData({
        avgDefence: ''
      })
    }
  },
  navbarReady(e) {
    let navBarHeight = e.detail.height;
    this.setData({
      navBarHeight,
    });
    wx.$util.getViewInfo('.reception-wrap', ({
      height
    }) => {
      this.setData({
        receptionWrapHeight: `${height * 2}rpx`
      })
    })
  },
  dialogueRef() {
    return this.selectComponent('#dialogue')
  },
  qualityRef() {
    return this.selectComponent('#quality')
  },
  share(e) {
    let userInfo = wx.$util.getUserInfo() || {}
    const {
      record
    } = this.data,
      receptionStatus = {
        1: '成交',
        2: '战败',
        3: '未知'
      } [record.orderStatus], {
        endTime,
        totalDuration,
        fileUrl,
        startTime
      } = record
    // 门店 - 接待开始时间 - 接待时长 - 员工标记的接待结果 - 接待录音mp3地址
    let text = [record.merchantName, wx.$util.tsFormatTime(startTime, 'Y年M月D日 h:m:s'), wx.$util.tsFormatTime(endTime, 'Y年M月D日 h:m:s'), this.format(totalDuration), receptionStatus, fileUrl].join(' - ')
    wx.$util.setCopyData(text)
    this.setTrackData('copy_content', {
      context: text,
    })
    // wx.$util.showToast('复制成功')
  },
  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() {
    this.onPageShow()
  },

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

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

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

  },

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

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  /**设置埋点数据 */
  setTrackData(code, params = {}) {
    let {
      record
    } = this.data
    wx.$track.setTrackData(0, code, {
      ...params,
      batchId: record.batchId
    })
  }
})