ChannelsModal.js 3.65 KB
// pages/tab/index/components/ChannelsModal/ChannelsModal.js
import {
  systemApi
} from '~/api/index'
import {
  IMG_URL
} from '~/config'
Component({
  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    showChannels: true
  },

  /**
   * 组件的方法列表
   */
  methods: {
    noScroll() {},
    async init(auto = true) {
      const {
        finderUserName
      } = await wx.p.api.systemApi.getMockData()
      this.setData({
        finderUserName: finderUserName || 'sph7YsAMEGQixoU'
      })
      this.getChannelsLiveInfo(auto)
    },
    getChannelsLiveInfo(auto) {
      const _this = this,
        {
          finderUserName
        } = this.data

      wx.getChannelsLiveInfo({
        finderUserName,
        success(liveInfo) {
          console.log('视频号直播信息:', liveInfo)
          if (liveInfo.status == 3) {
            _this.getChannelsLiveNoticeInfo(finderUserName, auto)
            liveInfo = null
          } else if (!auto) _this.close()
          _this.setData({
            liveInfo
          })
        },
        fail(err) {
          console.log(err)
          _this.getChannelsLiveNoticeInfo(finderUserName, auto)
        }
      })
    },
    getChannelsLiveNoticeInfo(finderUserName, auto) {
      const _this = this
      wx.getChannelsLiveNoticeInfo({
        finderUserName,
        success(liveNoticeInfo) {
          console.log('视频号预告信息:', liveNoticeInfo)
          let startTime = liveNoticeInfo.startTime * 1e3,
            month = new Date(startTime).format('M'),
            day = new Date(startTime).format('d'),
            time = new Date(startTime).format('hh:mm')
          liveNoticeInfo = {
            ...liveNoticeInfo,
            month,
            day,
            time
          }
          _this.setData({
            liveNoticeInfo
          })
          if (!auto) _this.close()
        },
        fail(err) {
          console.log('视频号预告信息:', err)
          _this.setData({
            liveNoticeInfo: null
          })
        }
      })
    },
    jumpLive() {
      const {
        liveInfo: {
          feedId,
          nonceId
        },
        finderUserName
      } = this.data,
        _this = this
      wx.openChannelsLive({
        finderUserName,
        feedId,
        nonceId,
        success(res) {
          _this.close()
        },
        fail(err) {
          if (err && err.errMsg && !err.errMsg.includes('cancel')) wx.p.wxApi.showToast(err.errMsg)
        }
      })
    },
    reserve() {
      this.close()
      const {
        noticeId
      } = this.data.liveNoticeInfo
      wx.reserveChannelsLive({
        noticeId
      })
    },
    close(e) {
      if (e) this.setData({
        animation: true
      }, setTimeout(() => {
        this.setBanner()
      }, .3e3))
      else this.setBanner()
    },
    async setBanner() {
      const v = await systemApi.getImageVersion() || Date.now()
      const {
        liveInfo,
        liveNoticeInfo,
        finderUserName
      } = this.data,
        bannerImg = `${IMG_URL}home/live/${liveInfo ? 'live' :'notice'}.png?v=${v}`,
        thisPage = wx.p.router.getPageInstance()
      let {
        bannerList
      } = thisPage.data,
        item = liveInfo || liveNoticeInfo,
        obj = {
          bannerImg,
          bannerType: 'channels',
          type: liveInfo ? 1 : 0,
          finderUserName,
          ...item,
        }
      if (!bannerList) bannerList = []
      bannerList.unshift(obj)
      thisPage.setData({
        bannerList
      })
      this.setData({
        showChannels: false
      })
    }
  },
  lifetimes: {
    ready() {
      this.init()
    }
  }
})