LiveBox.vue 11.4 KB
<template>
  <!-- v-if="LiveProps[liveDetail.status].isLive" -->
  <div class="videoContainer">
    <div class="small-box" @click="isSmallBox = false" v-show="isSmallBox">
      <img class="icon-small-live" src="@/assets/images/icon-small-live.jpg" />
      <div class="flex small-content">
        <!-- <div v-if="LiveStatus[liveDetail.status] == 'live'" class="live-line"> -->
        <div class="live-line" v-if="LiveProps[liveDetail.status].isLive">
          <div class="side"></div>
          <div class="center side"></div>
          <div class="side"></div>
        </div>
        <img v-if="!LiveProps[liveDetail.status].isLive" :src="IMAGES.REPLAY_ICON" class="live-replayIcon" />
        <div class="text">{{ LiveProps[liveDetail.status].status }}</div>
      </div>
    </div>
    <!-- @click="openFullscreen" -->
    <!-- @touchmove.stop.prevent 禁止使用滚动 -->
    <div :style="videoStyle" id="live-box" :class="['live-box', { videoFullscreen: videoFullscreen }]" v-show="!isSmallBox">
      <div class="sprite-icon icon-live-close" v-show="!videoFullscreen && !isSmallBox" @click="isSmallBox = true"></div>

      <div :class="{ videoShort: isAction }">
        <div class="mask">
          <div class="status flex">
            <div v-if="LiveProps[liveDetail.status].isLive" class="live-line">
              <div class="side"></div>
              <div class="center side"></div>
              <div class="side"></div>
            </div>
            <!-- 回放图标 -->
            <img v-if="!LiveProps[liveDetail.status].isLive" :src="IMAGES.REPLAY_ICON" class="live-replayIcon" />
            <div class="text">{{ LiveProps[liveDetail.status].text }}</div>
          </div>
          <template v-if="isShowGif">
            <img class="coverGif" :src="LiveProps[liveDetail.status].covergif" alt="" />
          </template>
        </div>
        <div class="fullscreen-btn" @click="openFullscreen(false)">
          <img :src="IMAGES.FULL_SCREEN" />
        </div>
        <div class="poster-btn" @click="showKf = true">
          <img :src="detail.floatingMaskDiagram" />
        </div>
        <video id="player-container-id" preload="auto" playsinline webkit-playsinline></video>
        <group-modal v-model="showKf" :detail="detail" />
      </div>

      <div v-show="videoFullscreen">
        <!-- IM -->
        <im v-if="LiveProps[liveDetail.status].isLive" :liveDetail="liveDetail" :isAction="isAction" class="Im"></im>
        <action :bgBlack="true" class="BgAction" v-show="isAction && !LiveProps[liveDetail.status].isLive"></action>
      </div>
    </div>
  </div>
</template>
<script type="text/ecmascript-6">
import { wxConfig } from '@/api'
const IMAGES = Object.freeze({
  // COVER_LIVE: 'https://wimg.588ku.com/gif620/20/04/23/79a7882dfb0c59497976fd8b3bfe60ef.gif',
  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"
})
const TCPlayerConfig = Object.freeze({
  autoplay: true, // 自动播放
  loop: true, // 循环播放
  muted: true, // 是否静音
  controlBar: { // 控制条配置
    fullscreenToggle: false // 隐藏全屏按钮
  },
  webrtcConfig: { // webrtcConfig 参数列表
    showLog: true // 打印webrtc日志
  }
})
const LiveProps = Object.freeze({
  '0': { isShowBox: false },
  '1': { status: '直播', text: '正在直播', isLive: true, covergif: IMAGES.COVER_LIVE, isShowBox: true },
  '2': { status: '回放', text: '直播回放', isLive: false, covergif: IMAGES.COVER_REPLAY, isShowBox: true, icon: IMAGES.REPLAY_ICON }
})
const PLAYERDOMID = 'player-container-id'
export default {
  name: 'LiveBox',
  data() {
    return {
      player: null,
      videoFullscreen: false,
      showKf: false,
      isShowGif: false,
      videoStyle: '',
      isSmallBox: false, //是否显示小容器直播
      LiveProps,
      IMAGES
    }
  },
  computed: {
    isAction() {
      const { activityStatus, buyFlag } = this.detail
      return activityStatus != 3 && buyFlag != 2 //TODO CONSTANT
    }
  },
  props: {
    detail: {
      type: Object,
      default: () => { }
    },
    liveDetail: {
      type: Object,
      default: () => {
        return {
          // 直播状态 0:未开始;1:直播中;2:已结束;
          status: 1,
          // 直播播放地址(status=1生效)
          playPath: '',
          // 回放列表
          liveBroadcastRecordingFileList: []
        }
      }
    }
  },
  mounted() {
    this.createPlayerCover()
    this.createPlayer()
    this.play()
  },
  methods: {
    createPlayerCover() {
      !this.IsIOS && (this.isShowGif = true)
    },
    createPlayer() {
      this.player = TCPlayer(PLAYERDOMID, TCPlayerConfig)
    },
    play() {
      wxConfig.ready(() => {
        wx.getNetworkType({
          success: (result) => {
            // console.log('====', { result });
            this.player.ready(() => { // 设置播放器初始化完成后的回调
              this.fixHls()
              const src = this.LiveProps[this.liveDetail.status].isLive ? this.liveDetail.playPath : (this.liveDetail.liveBroadcastRecordingFileList || [])[0]?.videoUrl
              this.player.src(src)
              this.player.poster()
              this.player.play() // 播放视频
              // this.player.on('error', function (event) {
              //   // 从回调参数 event 中获取事件状态码及相关数据
              //   console.log(event, 'error-----------')
              // })
              this.draggable(document.getElementById('live-box'), this.openFullscreen, 'icon-live-close')
            })
          }
        })
      })
    },

    controlBarStyle() {
      var [controlBar] = document.getElementsByClassName('vjs-control-bar')
      controlBar.style.bottom = this.isAction ? '10vh' : '0'
      controlBar.style.display = this.LiveProps[this.liveDetail.status].isLive ? 'none' : 'auto'
    },

    fixHls() {
      if (Hls.isSupported()) {
        this.video = document.getElementById(PLAYERDOMID)
        this.hls = new Hls()
        // bind them together
        this.hls.attachMedia(this.video)
        this.dealTcPlayerErr14()
      }
    },
    // 打开关闭全屏
    openFullscreen(status = true) {
      // console.log('openFullscreen')
      this.isShowGif = false
      const [live] = document.getElementsByClassName('live-box')
      const [volumePanel] = document.getElementsByClassName('vjs-volume-panel ')
      const [volume] = volumePanel.getElementsByClassName('vjs-mute-control'),
        [body] = document.getElementsByTagName('body'),
        bodyClass = body.className
      volume.click()
      this.player.play()
      this.videoFullscreen = status
      live.id = status ? '' : 'live-box'
      body.className = status ? 'van-overflow-hidden' : bodyClass.replace('van-overflow-hidden', '')
      this.controlBarStyle()
      this.$emit('handleLiveFull', { isFull: this.videoFullscreen })
    },
    // 处理tcplayer错误码14
    dealTcPlayerErr14() {
      this.hls.on(Hls.Events.ERROR, function (event, data) {
        if (data.fatal) {
          switch (data.type) {
            case Hls.ErrorTypes.NETWORK_ERROR:
              // try to recover network error
              // console.log('fatal network error encountered, try to recover')
              this.hls.startLoad()
              break
            case Hls.ErrorTypes.MEDIA_ERROR:
              // console.log('fatal media error encountered, try to recover')
              this.hls.recoverMediaError()
              break
            default:
              // cannot recover
              this.hls.destroy()
              break
          }
        }
      })
    },
    changeGifStatus() {
      this.isShowGif = !this.isShowGif
    },
    continueVideo() {
      this.player.play()
    }
  }
}
</script>
<style scoped lang="scss">
.small-box {
  position: fixed;
  top: 18%;
  right: 0;
  z-index: 7;
  // position: relative;

  width: 52px;
  height: 21px;
  font-size: 12px;
  font-weight: 600;
  color: #ffffff;
  .icon-small-live {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    border-radius: 10px 0 0 10px;
    // object-fit: cover;
  }
  .small-content {
    position: relative;
    width: 100%;
    height: 100%;
    z-index: 9;
    line-height: 21px;
    // padding-left: 5px;
    // padding: ;
    justify-content: center;
    // overflow: hidden;
    border-radius: 0 0 10px 10px;
    // padding: ;
    text-align: right;
    .live-replayIcon {
      position: absolute;
      left: 7px;
      top: 5px;
      display: inline-block;
      width: 10px;
      height: 10px;
      z-index: 9;
    }
    .text {
      z-index: 40;
      margin-left: 15px;
      font-size: 12px;
    }
  }
}
.live-box {
  box-sizing: border-box;
  position: fixed;
  top: 23%;
  left: calc(98% - 86px);
  z-index: 7;
  width: 82.5px;
  height: 150px;
  border-radius: 4px;
  overflow: hidden;
  user-select: auto;
  // transition: width 0.1s ease, height 0.1s ease;
  .icon-live-close {
    position: absolute;
    top: 2px;
    right: 2px;
    // left: -100px;
    z-index: 3;
  }
  .mask {
    width: 100%;
    height: 100%;
    position: absolute;
    left: 0;
    top: 0;
    z-index: 2;
    border: 2px solid #f95d35;
    border-radius: 4px;
    box-sizing: border-box;

    .coverGif {
      object-fit: contain;
      height: 126px;
    }
    .status {
      position: absolute;
      left: -2px;
      bottom: -2px;
      width: 104%;
      height: 18px;
      background: linear-gradient(291deg, #f6542b 0%, #ff6c47 100%), linear-gradient(270deg, rgba(255, 57, 0, 0) 0%, #ff3900 33%, #fd466f 100%);
      font-size: 12px;
      font-weight: 500;
      color: #ffffff;
      box-sizing: border-box;
      padding: 0 10px;
      .live-replayIcon {
        display: inline-block;
        width: 10px;
        height: 10px;
      }
      .text {
        margin-left: auto;
      }
    }
  }
  #player-container-id {
    width: 100%;
    height: 100%;
    video {
      object-fit: cover !important;
    }
  }
  .fullscreen-btn {
    display: none;
    position: absolute;
    right: 14px;
    top: 28px;
    width: 54px;
    height: 48px;
    z-index: 2;
  }
  .poster-btn {
    display: none;
    position: absolute;
    left: 17px;
    top: 17px;
    width: 76px;
    height: 115px;
    z-index: 2;
  }
  /deep/.vjs-control-bar {
    opacity: 0;
  }
}
.videoFullscreen {
  position: relative;
  width: 100vw !important;
  height: 100vh !important;
  left: 0 !important;
  top: 0 !important;
  right: 0 !important;
  bottom: 0 !important;
  border: none !important;
  background: #000000;
  overflow-y: hidden;
  .videoShort {
    // height: calc(100vh - 13.333vh) !important;
    height: 100%;
  }
  .mask {
    display: none;
  }
  .fullscreen-btn,
  .poster-btn {
    display: block;
  }

  /deep/.vjs-control-bar {
    opacity: 1 !important;
    // bottom: 10vh;
    background: none !important;
    // background-color: rgba(0, 0, 0, 0.3);
  }
  .BgAction {
    // 80%,80%, rgba(0, 0, 0, 0.4),
    background: linear-gradient(0deg, rgba(0, 0, 0, 0.6), rgba(225, 225, 225, 0)); /* 标准的语法(必须放在最后) */
  }
}
.Im {
  position: absolute;
  bottom: 10px;
  left: 0;
  width: 100%;
}
</style>