Explorar o código

首页视频轮播

dedsudiyu hai 1 día
pai
achega
b3bdaf4280

+ 144 - 52
src/components/LabStats/videoStats.vue

@@ -3,10 +3,11 @@
     <div class="corner-deco tl"></div><div class="corner-deco tr"></div>
     <div class="corner-deco bl"></div><div class="corner-deco br"></div>
     <div class="panel-title">实验室视频监控</div>
-    <div class="video-content">
-      <webRTC v-if="webRtcType" class="video-for-min-box"></webRTC>
-      <mpegts-video class="video-for-min-box" :videoProps="item" v-for="(item,index) in mpegtsList" :key="'mpegts_'+index"></mpegts-video>
-      <videoForMod class="video-for-min-box" v-for="(item,index) in modList" :key="'video_'+index" :deviceNo="item.deviceNo"></videoForMod>
+    <div class="video-content" v-if="videoType">
+      <!--<webRTC v-if="webRtcType" class="video-for-min-box"></webRTC>-->
+      <!--<mpegts-video class="video-for-min-box" :videoProps="item" v-for="(item,index) in mpegtsList" :key="'mpegts_'+index"></mpegts-video>-->
+      <!--<videoForMod class="video-for-min-box" v-for="(item,index) in modList" :key="'video_'+index" :deviceNo="item.deviceNo"></videoForMod>-->
+      <videoForCarousel class="video-for-min-box-for" v-for="(item,index) in videoList" :key="'video_'+index" :deviceNo="item.deviceNo"></videoForCarousel>
     </div>
   </div>
 </template>
@@ -14,10 +15,11 @@
   import { getVideoList,laboratoryLabScreenScreenConfigGet,iotDeviceList,laboratorySignInCheckGetKunYunToken } from '@/api/index'
   import videoForMod from '@/components/videoForMod/videoForMod.vue'
   import mpegtsVideo from '@/components/mpegtsVideo/mpegtsVideo.vue'
+  import videoForCarousel from '@/components/videoForMod/videoForCarousel/videoForCarousel.vue'
   import webRTC from './webRTC.vue'
   export default {
     name: 'videoStats',
-    components: { videoForMod,mpegtsVideo,webRTC },
+    components: { videoForMod,mpegtsVideo,webRTC,videoForCarousel },
     data () {
       return {
         width:470,
@@ -27,15 +29,25 @@
         pollTimer:null,
         page:1,
         webRtcType:false,
+        //轮播
+        videoList:[],
+        videoType:false,
+        videoQueryParams:{},
+        switchTimer: null,    // 1s 延迟切换定时器(setTimeout)
+        videoLoading: false,  // 请求防重入标识
       }
     },
     created(){
 
     },
     mounted(){
-      this.getData();
-      this.pollTimer = setInterval(this.getData, 5 * 60 * 1000)
-      // this.getVideoData();
+      //指定
+      // this.getData();
+      // this.pollTimer = setInterval(this.getData, 5 * 60 * 1000)
+      //轮播
+      this.getVideoData();
+      // this.pollTimer = setInterval(this.getVideoData, 5 * 60 * 1000)
+      this.pollTimer = setInterval(this.getVideoData, 30 * 1000)
     },
     methods:{
       //指定视频
@@ -129,65 +141,126 @@
 
         }
       },
-      //刷新视屏
-      getVideoData(data) {
-        let obj = {
-          page: 1,
+      //轮播视频
+      // 轮播视频:保留上次 page,实现轮播推进
+      getVideoData() {
+        const page = this.videoQueryParams.page || 1; // 关键:不重置为 1
+        this.videoQueryParams = {
+          page,
           pageSize: 4,
           passageway: '',
-          protocol: window.location.href.indexOf('https') !== -1 ? 'wss' : 'ws',
+          protocol: window.location.protocol === 'https:' ? 'wss' : 'ws',
           streamType: 1,
           source: 4,
-          subIds: data,
+          subIds: [],
         };
-        this.$set(this, 'videoQueryParams', obj);
-        this.$nextTick(() => {
-          this.videoInitialize()
-        })
+        this.videoInitialize();
       },
       async videoInitialize() {
-        let self = this;
-        self.$set(self, 'videoType', false);
-        self.$set(self, 'videoList', []);
+        if (this.videoLoading) return;
+        this.videoLoading = true;
+
+        const TARGET = this.videoQueryParams.pageSize || 4;
+        const MAX_LOOP = 20;
+        const list = [];
+
+        let page = this.videoQueryParams.page || 1;
+        let total = 0;
+        let loop = 0;
+        let wrapped = false;
+        let lastPage = page; // 记录本轮实际请求到的最后一个 page
+
         try {
-          // const res = await getVideoList(this.videoQueryParams)
-
-          // 假数据开始
-          let res = {
-            data: {
-              total: 100,
-              records: [
-                {streamUrl: '1', deviceNo: '1'},
-                {streamUrl: '2', deviceNo: '2'},
-                {streamUrl: '3', deviceNo: '3'},
-                {streamUrl: '4', deviceNo: '4'},
-              ],
+          while (list.length < TARGET && loop < MAX_LOOP) {
+            loop++;
+
+            const res = await getVideoList({
+              ...this.videoQueryParams,
+              page,
+              pageSize: TARGET,
+            });
+            const records = (res && res.data && res.data.records) || [];
+            total = (res && res.data && res.data.total) || 0;
+            lastPage = page; // 记录本轮请求到的 page
+
+            if (total === 0) break;
+            if (records.length === 0 && wrapped) break;
+
+            for (const item of records) {
+              if (list.length >= TARGET) break;
+              list.push({
+                width: this.width,
+                height: this.height,
+                url: item.streamUrl,
+                cameraIndexCode: item.deviceNo,
+                deviceNo: item.deviceNo,
+              });
             }
-          };
-          // 假数据结束
 
-          let list = [];
-          for(let i=0;i<res.data.records.length;i++){
-            list.push(
-                {
-                  width: self.width, //(宽度:非必传-默认600)
-                  height: self.height, //(高度:非必传-默认338)
-                  url: res.data.records[i].streamUrl,
-                  cameraIndexCode: res.data.records[i].deviceNo,
-                }
-            )
+            if (list.length >= TARGET) break;
+
+            const totalPage = Math.max(Math.ceil(total / TARGET), 1);
+            page++;
+            if (page > totalPage) {
+              page = 1;
+              wrapped = true;
+            }
+          }
+
+          // —— 一条数据都没拿到 ——
+          if (list.length === 0) {
+            // 首次加载:让播放器容器渲染出来,避免一直黑屏(空态由模板/子组件处理)
+            // 后续轮播:保持旧视频继续播放,不切换
+            if (this.videoList.length === 0) {
+              this.videoType = true;
+            }
+            return;
           }
-          this.$set(this,'videoList',list)
-          this.$nextTick(()=>{
-            setTimeout(function(){
-              self.$set(self, 'videoType', true);
-            },1000);
-          })
+
+          // —— 先写数据、再切换 ——
+          this.videoType = false;
+          this.videoList = list;
+
+          this.$nextTick(() => {
+            if (this.switchTimer) clearTimeout(this.switchTimer);
+            this.switchTimer = setTimeout(() => {
+              this.videoType = true;
+              this.switchTimer = null;
+
+              // —— 渲染完成后,再统一刷新 page,供下次轮播使用 ——
+              const totalPage = Math.max(Math.ceil(total / TARGET), 1);
+              let nextPage;
+              if (lastPage >= totalPage) {
+                nextPage = 1;          // 到达总数末尾,回到开头
+              } else {
+                nextPage = lastPage + 1; // 继续往后推进
+              }
+              this.videoQueryParams.page = nextPage;
+            }, 1000);
+          });
         } catch (e) {
-          console.error('VideoGrid:', e)
+          console.error('VideoGrid:', e);
+          // 首次加载失败也不至于一直黑屏(后续失败则保持旧视频)
+          if (this.videoList.length === 0) {
+            this.videoType = true;
+          }
+        } finally {
+          this.videoLoading = false;
         }
       },
     },
+    beforeDestroy() {
+      // 清理轮播轮询定时器
+      if (this.pollTimer) {
+        clearInterval(this.pollTimer);
+        this.pollTimer = null;
+      }
+      // 清理 1s 延迟切换定时器
+      if (this.switchTimer) {
+        clearTimeout(this.switchTimer);
+        this.switchTimer = null;
+      }
+    },
   }
 </script>
 <style scoped lang="scss">
@@ -232,5 +305,24 @@
     .video-for-min-box:nth-child(4){
       /*margin:10px 0 0 0!important;*/
     }
+    .video-for-min-box-for{
+      display: inline-block!important;
+      overflow: hidden!important;
+      border-radius:4px!important;
+      height:280px!important;
+      width:470px!important;
+    }
+    .video-for-min-box-for:nth-child(1){
+      margin:0 10px 0 0!important;
+    }
+    .video-for-min-box-for:nth-child(2){
+
+    }
+    .video-for-min-box-for:nth-child(3){
+      margin:10px 10px 0 0!important;
+    }
+    .video-for-min-box-for:nth-child(4){
+      margin:10px 0 0 0!important;
+    }
   }
 </style>

+ 206 - 0
src/components/videoForMod/videoForCarousel/H5PlayerVideo.vue

@@ -0,0 +1,206 @@
+<template>
+  <div class="H5PlayerVideo">
+    <div :id="videoData.id" :ref="videoData.id"></div>
+    <!--<p class="el-icon-full-screen full-screen-button" @click="fullScreen"></p>-->
+    <!--<p class="full-failed-button" v-if="fullScreenType" @click="outFullScreen"></p>-->
+  </div>
+</template>
+
+<script>
+  export default {
+    name: 'H5PlayerVideo',
+    props: {
+      videoProps: {}
+    },
+    data() {
+      return {
+        fullScreenType:false,
+        videoCover: localStorage.getItem('fileBrowseEnvironment') + localStorage.getItem('videoCover'),
+        videoData: {},
+      }
+    },
+    created() {
+      this.$set(this, 'videoData', {
+        id: 'video_' + this.generateRandomString(),
+        width: this.videoProps.width ? this.videoProps.width : 600,
+        height: this.videoProps.height ? this.videoProps.height : 338,
+        url: this.videoProps.url
+      })
+    },
+    mounted() {
+      this.$nextTick(() => {
+        this.initH5Player(1)
+        let type = this.videoData.url.indexOf('wss') !== -1?1:0;
+        this.initPlayer(this.videoData.url, 0,type)
+      })
+    },
+    methods: {
+      //全屏
+      fullScreen() {
+        if(this.$parent.$parent.stopTime){
+          this.$parent.$parent.stopTime(this.videoProps.cameraIndexCode);
+        }else if(this.$parent.stopTime){
+          this.$parent.stopTime(this.videoProps.cameraIndexCode);
+        }else{
+          this.myPlugin.JS_FullScreenDisplay(true).then(
+            (res) => {
+              // console.info('JS_FullScreenDisplay success');
+              // do you want...
+            },
+            (err) => {
+              // console.info('JS_FullScreenDisplay failed');
+              // do you want...
+            }
+          );
+        }
+      },
+      //退出全屏
+      outFullScreen() {
+        let self = this;
+        this.myPlugin.JS_FullScreenDisplay(false).then(
+          (res) => {
+            console.info('JS_FullScreenDisplay success');
+            // do you want...
+          },
+          (err) => {
+            console.info('JS_FullScreenDisplay failed');
+            // do you want...
+          }
+        );
+        self.$set(self,'fullScreenType',false);
+      },
+      initH5Player(split) {
+        this.myPlugin = new window.JSPlugin({
+          szId: this.videoData.id, //需要英文字母开头,唯一性,必填
+          szBasePath: '/h5player/', // 必填,与h5player.min.js的引用目录一致 填写的是pulblic下的路径!!!
+          bSupporDoubleClickFull: true,//是否支持双击全屏,默认true
+          openDebug: true,
+          oStyle: {
+            borderSelect: '#000'
+          },
+          // 当容器div#play_window有固定宽高时,可不传iWidth和iHeight,窗口大小将自适应容器宽高
+          iWidth: this.videoData.width,
+          iHeight: this.videoData.height,
+          // 分屏播放,默认最大分屏4*4
+          iMaxSplit: split,
+          iCurrentSplit: split
+        })
+        let iWndNum = 1;
+        let InterruptTime = 5;
+        this.myPlugin.JS_SetInterruptTime(iWndNum, InterruptTime).then(
+          () => {
+            // console.info('JS_SetInterruptTime success');
+            // do you want...
+          },
+          (err) => {
+            // console.info('JS_SetInterruptTime failed');
+            // do you want...
+          }
+        );
+        // 事件回调绑定
+        this.myPlugin.JS_SetWindowControlCallback({
+          windowEventSelect: function (iWndIndex) {  //插件选中窗口回调
+            // console.log('windowSelect callback: ', iWndIndex)
+          },
+          pluginErrorHandler: function (iWndIndex, iErrorCode, oError) {  //插件错误回调
+            // console.log('pluginError callback: ', iWndIndex, iErrorCode, oError)
+          },
+          windowEventOver: function (iWndIndex) {  //鼠标移过回调
+            //console.log(iWndIndex);
+          },
+          windowEventOut: function (iWndIndex) {  //鼠标移出回调
+            //console.log(iWndIndex);
+          },
+          windowEventUp: function (iWndIndex) {  //鼠标mouseup事件回调
+            //console.log(iWndIndex);
+          },
+          windowFullCcreenChange: function (bFull) {  //全屏切换回调
+            // console.log('fullScreen callback: ', bFull)
+          },
+          firstFrameDisplay: function (iWndIndex, iWidth, iHeight) {  //首帧显示回调
+            // console.log('firstFrame loaded callback: ', iWndIndex, iWidth, iHeight)
+          },
+          performanceLack: function () {  //性能不足回调
+            // console.log('performanceLack callback: ')
+          }
+        })
+      },
+      initPlayer(url, index,type) {
+        this.myPlugin.JS_Play(url,
+          {
+            playURL: url, // 流媒体播放时必传
+            mode: type?type:0 // 解码类型:0=普通模式; 1=高级模式 默认为0
+            // ...
+          },
+          index //当前窗口下标
+        ).then(
+          () => {
+            console.info('JS_Play success')
+            // do you want...
+          },
+          (err) => {
+            console.info('JS_Play failed:', err)
+            // do you want...
+          }
+        )
+      },
+      //生成随机ID
+      generateRandomString() {
+        let chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
+        let randomString = ''
+        for (let i = 0; i < 24; i++) {
+          let randomIndex = Math.floor(Math.random() * chars.length)
+          randomString += chars.charAt(randomIndex)
+        }
+        return randomString
+      },
+      videoOff() {
+        this.myPlugin.JS_StopRealPlayAll().then(
+          () => {
+            console.info('JS_StopRealPlayAll success')
+            // do you want...
+          },
+          (err) => {
+            console.info('JS_StopRealPlayAll failed')
+            // do you want...
+          }
+        )
+      }
+    },
+    beforeDestroy() {
+      let self = this
+      self.videoOff()
+    }
+  }
+</script>
+
+<style scoped lang="scss">
+  .H5PlayerVideo {
+    margin: 0;
+    padding: 0;
+    display: inline-block;
+    * {
+      margin: 0;
+      padding: 0;
+    }
+    position: relative;
+    .full-screen-button {
+      /*background: url("../../assets/icon_0.png");*/
+      /*background-size: 100%;*/
+      color:#fff;
+      position: absolute;
+      font-size: 20px;
+      height: 30px;
+      width: 30px;
+      line-height: 30px;
+      text-align: center;
+      top: 0;
+      right: 0;
+      z-index: 1;
+      cursor: pointer;
+    }
+    .full-screen-button:hover{
+      color:#00b7ee;
+    }
+  }
+</style>

+ 81 - 0
src/components/videoForMod/videoForCarousel/videoForCarousel.vue

@@ -0,0 +1,81 @@
+<!-- 视频组件外科 -->
+<template>
+    <div class="videoForCarousel">
+        <H5PlayerVideo v-if="videoData" :videoProps="videoData"></H5PlayerVideo>
+    </div>
+</template>
+<script>
+    import { iotCameraGetPreviewURLs } from '@/api/index'
+    import H5PlayerVideo from './H5PlayerVideo.vue'
+    export default {
+        name: 'videoForCarousel',
+        components: {
+            H5PlayerVideo,
+        },
+        props:{
+            deviceNo:{},
+        },
+        data() {
+            return {
+                width:470,
+                height:280,
+                videoData:null,
+                //全屏视频参数
+                fullVideoProps:{},
+                fullVideoType:false,
+            }
+        },
+        created() {
+
+        },
+        mounted() {
+            console.log('deviceNo',this.deviceNo)
+            if(this.deviceNo){
+                this.iotCameraGetPreviewURLs();
+            }
+        },
+        methods: {
+            async iotCameraGetPreviewURLs() {
+                try {
+                    let obj = {
+                        streamType:1,
+                        cameraIndexCode:this.deviceNo,
+                        protocol: window.location.href.indexOf('https') !== -1 ? 'wss' : 'ws',
+                    }
+                    const response = await iotCameraGetPreviewURLs(obj);
+                    if(response.code == 200){
+                        this.$set(this, 'videoData', {
+                            width: this.width,
+                            height: this.height,
+                            url: response.data,
+                        })
+                        this.$forceUpdate();
+                    }
+                } catch (error) {
+                    this.$message.error(error.message);
+                    // 不再向上抛出,错误被吞掉
+                }
+            },
+            //全屏开启-关闭轮播
+            stopTime(cameraIndexCode){
+                this.$set(this,'fullVideoProps',{cameraIndexCode:cameraIndexCode});
+                this.$set(this,'fullVideoType',true);
+                document.body.classList.add('video-fullscreen-active');
+            },
+            //全屏关闭-开启轮播
+            outFullScreen(){
+                let self = this;
+                this.$set(this,'fullVideoType',false);
+                this.$set(this,'fullVideoProps',{});
+                document.body.classList.remove('video-fullscreen-active');
+            },
+        }
+    }
+</script>
+<style scoped lang="scss">
+    .videoForCarousel {
+        display: inline-block;
+        width:470px;
+        height:280px;
+    }
+</style>