materialPage.js 4.71 KB
// packageStudy/pages/sceneMarket/materialPage/materialPage.js
import {
  getMaterialById,
  enterPageTrack,
  leavePageTrack
} from '../request'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    showMenuBtn: true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getMaterialById(options.id)
    this.enterPageTrack(options.id)
  },
  async getMaterialById(id) {
    try {
      const {
        code,
        data,
        message
      } = await getMaterialById({
        id
      })
      if (code == 0) {
        wx.setNavigationBarTitle({
          title: data.title,
        })
        this.setData({
          materialType: data.type
        })
        if (data.type == 3) {
          let detailInfo = [],
            imgLength = 0
          data.richTextInfoList?.map(e => {
            let imgList = []
            if (e.content) e.content = this.highlightText(e.content)
            if (e.pictureList && e.pictureList.length) {
              imgLength += e.pictureList.length
              e.pictureList.map(i => {
                imgList.push({
                  // visible: false,
                  visible: true,
                  url: i
                })
              })
              e.imgList = imgList
            }
            if (e.level == 1) detailInfo.push({
              ...e,
              children: []
            })
          })
          data.richTextInfoList?.map(e => {
            if (e.level == 2) {
              let index = detailInfo.findIndex(item => item.id == e.parentId)
              if (index > -1) detailInfo[index].children.push(e)
            }
          })
          this.setData({
            detailInfo
          })
        } else {
          this.setData({
            materialUrl: data?.detailInfos[0]?.image || ''
          })
        }
        console.log(data)
      } else throw message

    } catch (error) {
      console.log(error)
      wx.$util.showToast(error)
    }
  },
  highlightText(text) {
    text = text.replace(/<\/?br\s*\/?>|\n/g, '<br>')
    const matches = text.match(/#/g)
    if (!matches) return text
    const spanCount = matches.length
    let lastSpan = spanCount % 2 === 1,
      parts = [],
      currentIndex = 0
    for (let i = 0; i < spanCount; i++) {
      const matchIndex = text.indexOf('#', currentIndex)
      parts.push(text.substring(currentIndex, matchIndex))
      if (i === spanCount - 1 && lastSpan) parts.push('')
      else {
        if (i % 2 === 0) parts.push('<span style="color:#FF8D1A;font-weight: 500;">')
        else parts.push('</span>')
      }
      currentIndex = matchIndex + 1
    }
    parts.push(text.substring(currentIndex))
    return parts.join('')
  },
  contentScroll() {
    this.setData({
      showMenuBtn: false
    })
  },
  clickMenu() {
    const {
      showMenuBtn
    } = this.data
    if (!showMenuBtn) {
      this.setData({
        showMenuBtn: true
      })
      return
    }
    this.setData({
      showMenuPopup: true
    })
  },
  onClose() {
    this.setData({
      showMenuPopup: false
    })
  },
  menuClick(e) {
    const {
      id
    } = e.currentTarget.dataset
    this.setData({
      contentId: `case-${id}`
    })
    this.onClose()
  },
  enterPageTrack(goodsId) {
    const userInfo = wx.$util.getUserInfo()
    const params = {
      goodsId,
      goodsType: 0,
      merchantId: userInfo.default4SStoreInfo.merchantId,
      merchantName: userInfo.default4SStoreInfo.merchantName
    }
    enterPageTrack(params).then(({
      data
    }) => {
      this.trackId = data
    })
  },
  onPlay(item) {
    console.log(item)
    const {
      id
    } = item.currentTarget.dataset.item
    this.selectAllComponents('.audio').map(e => {
      if (`audio-${id}` != e.id) e.stop()
    })
    this.selectComponent(`#audio-${id}`).data.innerAudioContext[`key${id}`].play()
    // Object.keys(this.$refs).map(key => {
    //   if (`audio-${item.id}` != key) this.$refs[key][0]?.stop()
    // })
    // let innerAudioContext = this.$refs[`audio-${item.id}`][0]?.innerAudioContext[`key${item.id}`]
    // if (innerAudioContext) innerAudioContext.play()
    console.log()
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

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

  },

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

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {
    if (this.trackId) leavePageTrack({
      id: this.trackId
    })
  },

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

  },

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

  },

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

  }
})