dialogue.js 9.61 KB
// packageEquity/pages/receptionDetail/components/dialogue/dialogue.js
let thisPage = null
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    recordData: Object,
    batchId: {
      type: Number,
      observer: function (n) {
        if (this.IntersectionObserver) {
          this.IntersectionObserver.disconnect()
          this.IntersectionObserver = null
        }


        this.setData({
          flag: false,
          progress: 0,
          dialogueList: []
        })
        const data = {
          batchId: n,
        };
        this.findConversationDetailByBatchId(data)
      }
    },
    totalDuration: {
      type: Number,
      value: 0
    },
    qualityData: {
      type: Array,
      observer: function (n) {
        if (n.length) this.findCbByBatchId(n)
      }
    }
  },
  lifetimes: {
    ready() {},
    detached() {
      this.IntersectionObserver?.disconnect()
    }

  },
  /**
   * 组件的初始数据
   */
  data: {
    progress: 0,
    dialogueList: [],
  },

  /**
   * 组件的方法列表
   */
  methods: {
    findCbByBatchId(data) {
      let {
        recordingDuration: totalDuration
      } = this.data.recordData,
        margin = 20
      wx.p.wxApi.getViewInfo('.axis-bar', ({
        height: containerHeight,
        top: barTop
      }) => {
        if (containerHeight == 0) {
          return setTimeout(() => {
            this.findCbByBatchId()
          }, 100)
        }
        this.barTop = barTop
        this.containerHeight = containerHeight
        let axisList = []
        data?.sort((a, b) => a.offset - b.offset).forEach(e => {
          const {
            offset: mergedOffset,
          } = e
          e.top = mergedOffset / totalDuration * containerHeight
          e.docTop = mergedOffset / totalDuration * containerHeight
          e.text_id = `text_${mergedOffset}`
          e.mergedOffset = mergedOffset
          if (mergedOffset != null) axisList.push(e)
        })
        axisList.map((e, index) => {
          // 检查并调整 top 值,避免重叠
          for (let i = 0; i < index; i++) {
            if (e.top < axisList[i].top + margin &&
              e.top + margin > axisList[i].top) {
              // 如果重叠,将当前元素的 top 设置为前一个元素的底部加上 margin
              e.top = axisList[i].top + margin;
            }
          }
        })
        this.setData({
          axisList
        })
      }, this)
    },
    getAudioObject() {
      thisPage = wx.p.router.getPageInstance()
      if (thisPage.qualityAudio) this.innerAudioContext = thisPage.qualityAudio
      else setTimeout(() => {
        this.getAudioObject()
      }, 100)
    },
    onTimeUpdate() {
      const {
        dialogueList
      } = this.data, {
          paused
        } = this.innerAudioContext, {
          mergedOffset
        } = this.setProgress(),
        item = dialogueList.find(e => e.mergedOffset == mergedOffset)
      if (!paused) this.setData({
        isPlayIng: true
      })
      if (this.data.isScrollling) return
      if (item && item.id) this.setTopId(item, true)
    },

    setTopId(item, scroll = false) {
      const {
        dialogueList
      } = this.data
      let centerIndex = dialogueList.findIndex(e => e.text_id == item.text_id)
      centerIndex -= 3
      if (centerIndex < 0) centerIndex = 0
      wx.nextTick(() => {
        this.setData({
          activeId: item.text_id,
        })
      })
      if (scroll) this.setData({
        toId: dialogueList[centerIndex].text_id
      })
    },
    setProgress() {
      const {
        duration,
        currentTime
      } = this.innerAudioContext,
        mergedOffset = Math.ceil(currentTime)
      this.setData({
        progress: mergedOffset / duration * 100
      })
      return {
        currentTime,
        mergedOffset
      }
    },
    toDefenceDetail(e) {
      let {
        item,
      } = e.currentTarget.dataset
      this.setData({
        toId: item.text_id
      })
      this.setData({
        isScrollling: true
      })
      this.touchend()
    },
    seekPlay(e) {
      let {
        item,
        click,
        scroll
      } = e.currentTarget.dataset, {
        activeId
      } = this.data
      if (!item) return
      thisPage.seek(item.mergedOffset)
      this.setTopId(item, scroll)
      if (click && item.text_id == activeId) {
        const {
          paused
        } = this.innerAudioContext
        if (paused) thisPage.play()
        else thisPage.onPause()
        thisPage.setData({
          isOperate: !paused
        })
      } else {
        thisPage.play()
        thisPage.setData({
          isOperate: false
        })
      }
    },
    formatTime(num) {
      let minutes = Math.floor(num / 60) || '0',
        seconds = num % 60 || '0'
      seconds = seconds < 10 ? `0${seconds}` : seconds
      return `${minutes}:${seconds}`
    },
    relativeToViewport() {
      if (this.IntersectionObserver) return
      this.IntersectionObserver = this.createIntersectionObserver({
        observeAll: true
      })
      this.IntersectionObserver.relativeToViewport({
        bottom: 1000,
        top: 0,
      }).observe('.item', res => {
        const {
          index
        } = res.dataset, {
            dialogueList
          } = this.data, {
            originalList
          } = this,
          item = dialogueList[index]
        if (!item?.visible && res.intersectionRatio > 0) {
          this.setData({
            [`dialogueList[${index}]`]: originalList[index]
          })
        }
      })
    },
    /**对话列表 */
    async findConversationDetailByBatchId(params) {
      const dialogueList = await wx.p.api.receptionApi.findConversationDetailByBatchId(params)
      this.getAudioObject()
      dialogueList?.map(e => {
        e.visible = true
        e.time = this.formatTime(e.mergedOffset)
        e.text_id = `text_${e.mergedOffset}`
        if (e?.recordingCheckResultList && e.recordingCheckResultList.length) {
          e.recordingCheckResultList.map(i => {
            i.showCheckDetail = false
            let [scriptRec] = JSON.parse(i.scriptRec)
            if (scriptRec) i.scriptRec = scriptRec.replace(/\n/g, '<br>')
            else i.scriptRec = ''
          })
        }
      })
      this.setData({
        flag: true,
      })
      this.originalList = dialogueList
      this.setDialogueData(dialogueList)
    },
    setDialogueData(dialogueList) {
      wx.p.wxApi.showLoading('加载中...')
      const batchSize = 100
      let batch = []
      // 遍历对话列表,累积数据
      for (let i = 0; i < dialogueList.length; i++) {
        batch.push({
          visible: false,
          text_id: dialogueList[i].text_id,
          id: dialogueList[i].id,
          mergedOffset: dialogueList[i].mergedOffset
        });
        // 当累积的数据达到批量大小时,执行 setData 更新
        if ((i + 1) % batchSize === 0 || i === dialogueList.length - 1) {
          this.setData({
            dialogueList: [...this.data.dialogueList, ...batch]
          });
          batch = []; // 清空累积的数据,准备下一批
        }
      }

      // // 确保最后不足100条的数据也被更新
      if (batch.length > 0) {
        this.setData({
          dialogueList: [...this.data.dialogueList, ...batch]
        })
      }
      // // 更新完成后清除加载提示
      wx.hideLoading();
      this.relativeToViewport();
    },
    /** 查询质检侧边栏 (质检使用id匹配,攻防用偏移量匹配) */
    switchDetail(e) {
      const {
        index,
        value,
        ind,
        key
      } = e.currentTarget.dataset
      let target = `dialogueList[${index}]`
      if (ind > -1) target = `dialogueList[${index}].recordingCheckResultList[${ind}]`
      this.setData({
        [`${target}.${key}`]: !value
      })
    },
    touchstart() {
      this.setData({
        isScrollling: true
      })
      this.clearTimer()
    },
    touchend() {
      this.touchTimer = setTimeout(() => {
        this.setData({
          isScrollling: false
        })
        this.clearTimer()
      }, 3e3)
    },
    clearTimer() {
      if (this.touchTimer) {
        clearTimeout(this.touchTimer)
        this.touchTimer = null
      }
    },
    onPause() {
      this.setData({
        isPlayIng: false
      })
    },
    barSeek(e) {
      function findNearest(arr, target) {
        let closest = arr[0];
        let minDiff = Math.abs(target - arr[0]);

        for (let i = 1; i < arr.length; i++) {
          const diff = Math.abs(target - arr[i]);
          if (diff < minDiff) {
            minDiff = diff;
            closest = arr[i];
          }
        }

        return closest;
      }
      let [touches] = e.touches, {
        barTop,
        containerHeight
      } = this, {
        duration
      } = this.innerAudioContext, {
        dialogueList
      } = this.data
      if (touches) {
        let {
          clientY
        } = touches,
        progress = (clientY - barTop) / containerHeight * 100,
          offsetList = dialogueList.map(i => i.mergedOffset),
          mergedOffset = findNearest(offsetList, duration * progress / 100),
          item = dialogueList.find(i => i.mergedOffset == mergedOffset)
        this.seekPlay({
          currentTarget: {
            dataset: {
              item,
              scroll: true
            }
          }
        }, false)
      }
    },

  }
})