LiveBox.js 4.82 KB
// pages/crowdfunding/components/LiveBox/LiveBox.js
const IMAGES = Object.freeze({
  COVER_LIVE: 'https://img02.glsx.com.cn/weapp/resource/scrm-mobile/crowdfunding/COVER_LIVE.gif',
  COVER_REPLAY: 'https://img02.glsx.com.cn/weapp/resource/scrm-mobile/crowdfunding/COVER_REPLAY.gif',
  REPLAY_ICON: 'https://img02.glsx.com.cn/weapp/resource/scrm-mobile/crowdfunding/replayIcon.png',
  FULL_SCREEN: 'https://img02.glsx.com.cn/weapp/resource/scrm-mobile/crowdfunding/fullscreen.png',
  GROUP: "https://img02.glsx.com.cn/weapp/resource/scrm-mobile/crowdfunding/gift.gif"
})
import environment from '~/utils/request/environment'
var that
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    navHeight: {
      type: String,
      value: 0
    },
    detail: {
      type: Object,
      value: () => {}
    },
    liveDetail: {
      type: Object,
      value: () => {
        return {
          // 直播状态 0:未开始;1:直播中;2:已结束;
          status: 0,
          // 直播播放地址(status=1生效)
          playPath: '',
          // 回放列表
          liveBroadcastRecordingFileList: []
        }
      },
      observer: function (n) {
        that = this
        if (n?.status) {
          this.init()
        }
      }
    },
    mediaLiveObject: {
      type: Object,
      observer(n) {
        if (n.liveBroadcastMediaUrl) {
          this.setData({
            mediaLive: true,
            isAction: true,
            ['liveDetail.status']: n.liveBroadcastStateFlag
          })
        }
      }
    },
    haveIM: {
      type: Boolean,
      value: true
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    x: 280,
    y: 180,
    // 小容器直播条
    isSmallBox: false,
    // 全屏
    videoFullscreen: false,
    showKf: false,
    // 按钮
    isAction: false,
    videoContext: {},
    playConf: {
      autoplay: true, // 自动播放
      muted: true, // 是否静音
      mode: 'RTC'
    },
    playUrl: '',
    IMAGES: IMAGES,
    reloadLive: false,
    LiveProps: Object.freeze({
      0: {
        isShowBox: false
      },
      1: {
        status: '直播',
        text: '正在直播',
        isLive: true,
        covergif: IMAGES.COVER_LIVE,
        isShowBox: true,
        gifAttr: 'liveBroadcastRoomCover'
      },
      2: {
        status: '回放',
        text: '直播回放',
        isLive: false,
        covergif: IMAGES.COVER_REPLAY,
        isShowBox: true,
        icon: IMAGES.REPLAY_ICON,
        gifAttr: 'liveBroadcastRoomEndCover'
      }
    })
  },

  /**
   * 组件的方法列表
   */
  methods: {
    createContext: function () {
      const isLive = this.data.LiveProps[this.data.liveDetail.status].isLive
      const ctx = isLive ? 'live-player' : ''
      this.setData({
        videoContext: wx.createLivePlayerContext(ctx)
      })
    },
    init() {
      const liveDetail = this.data.liveDetail
      const fileList = liveDetail.liveBroadcastRecordingFileList ? liveDetail.liveBroadcastRecordingFileList.map(item => item.videoUrl) : []
      this.setData({
        playUrl: this.data.LiveProps[liveDetail.status].isLive ? liveDetail.pullRtmpUrl : (liveDetail.liveBroadcastRecordingFileList || [])[0]?.videoUrl,
        liveBroadcastRecordingFileList: fileList,
        isAction: this.data.detail.activityStatus != 3 && this.data.detail.buyFlag != 2
      })
      this.createContext()
    },
    handleSetData(e) {
      const {
        attr,
        val
      } = e.currentTarget.dataset
      this.setData({
        [attr]: val
      })
    },
    handleKf() {
      this.setData({
        showKf: !this.data.showKf
      })
    },
    // h5授权登陆
    h5Login() {
      wx.$router.getPageInstance().jumpAuthUserInfo('imFlag')
    },
    handleFullscreen(e) {
      const {
        val
      } = e.currentTarget.dataset
      this.setData({
        videoFullscreen: val,
        ['playConf.muted']: !val
      })
      this.triggerEvent('handleLiveFull', {
        isFull: val
      })
    },
    changeView: wx.$util.debounce(e => {
      let {
        source,
        x,
        y
      } = e.detail
      if (source != 'touch') return
      that.setData({
        x: Math.abs(x),
        y: Math.abs(y)
      })
    }, 100),
    onHide() {
      if (this.data.liveDetail.status == 1) {
        this.selectComponent('#im')?.destroyIm()
      }
    },
    onShow() {
      if (this.data.liveDetail.status == 1) {
        this.selectComponent('#im')?.imOnShow()
      }
    },
    liveError(e) {
      let {
        detail: {
          errCode
        }
      } = e
      console.log(errCode)
      // 10001:未开麦克风和相机
      if (errCode == 10001) {
        this.setData({
          ['playConf.mode']: 'live',
          reloadLive: true
        })
        setTimeout(() => {
          this.setData({
            reloadLive: false
          }, 100)
        })
      }
    }
  }
})