vue3引入海康监控视频组件并实现非分屏需求一个页面同时预览多个监控视频(3)-接口分页篇(最终版)
index.html
<script src="static/haiKangWeb3.0/jquery-1.7.1.min.js"></script><script type="text/javascript" id="videonode" src="static/haiKangWeb3.0/webVideoCtrl.js"></script>
父组件index.vue
<template><div class="main-content" ><!--@wheel="handleScroll"--><div class="video-content"v-loading="loading"id="style-1"element-loading-text="加载中..."element-loading-svg-view-box="-10, -10, 50, 50"element-loading-background="rgba(122, 122, 122, 0.8)"ref="scrollContainer"><divv-for="(item, index) in cameraList":key="index"class="canvas-contaniner":loading="loading"><div class="videobox" v-if="item.status == 1"><videoMonitorings :hkvInfo="item" class="video" :videoId="item.id" :key="index" /></div><div v-else-if="item.status == 0" class="videobox">暂无...</div></div><!-- 分页 --><div class="paginationBox"><el-paginationclass="flex-auto ml-2":total="total"v-model:current-page="pageNum":current-page="pageNum":page-size="pageSize"layout="pager"backgroundsize="large"@update:current-page="onCurrentChange"/></div></div></div>
</template>
<script setup lang="ts">
import { ref, onMounted, onBeforeUnmount,computed,} from "vue";import {listApCamera,
} from "@/api/videoManage/videoConfiguration";import videoMonitorings from "./videoMonitorings.vue";import { message } from "@/utils/message";const loading = ref(false);const pageNum = ref(1) //当前页码const pageSize = ref(4) //每页数量const total = ref(0);const scrollContainer = ref(null);/** @以上hkList配置中 注意 loadingTime 和 id;其中loadingTime 建议和上一个时间间隔4S以上,id是动态加载生成填充的DOM树结构;* 如果间隔时间不够 或DOM的ID一样 可能出现画面加载不出来、画面覆盖重叠等情况;通过配置这2个参数可以避免这样多个摄像头只需要 增加 hkList的配置项即可;* */const dataListRef = ref([{ip: "", //IP地址port: "", //端口号username: "", //用户名password: "", //密码loadingTime: 1000, //间隔时长status:1,width: "828",height: "385",id: "divPlugin1",},{ip: "", //IP地址port: "", //端口号username: "",password: "",loadingTime: 5000,status:1,width: "828",height: "385",id: "divPlugin2",},{ip: "", //IP地址port: "", //端口号username: "",password: "",loadingTime: 9000,status:1,width: "828",height: "385",id: "divPlugin3",},{ip: "", //IP地址port: "", //端口号username: "",password: "",loadingTime: 13000,status:1,width: "828",height: "385",id: "divPlugin4",},]);const cameraList = ref([]);const videoParams = ref({})onMounted(async () => {onSearch();// window.addEventListener('scroll', handleScroll);});function onCurrentChange(page) {pageNum.value = page;sessionStorage.setItem('pageNumber', pageNum.value);onSearch()
}//滚动事件
// function handleScroll(event) {
// const container = document.querySelector('.video-content');
// const { scrollTop, scrollHeight, clientHeight } = container;
// if (scrollTop + clientHeight >= scrollHeight && !loading.value){
// event.preventDefault(); //阻止默认滚动事件
// const deltaY = event.deltaY || event.detail || event.wheelDeltaY;
// if (deltaY < 0 &&!loading.value) {
// if(pageNum.value == 1 || pageNum.value == 0){
// return false;
// }
// else if(pageNum.value>2){
// pageNum.value = pageNum.value-1;
// sessionStorage.setItem('pageNumber',pageNum.value-1);
// onSearch()
// }
// }
// else if(deltaY>0 &&!loading.value){
// sessionStorage.setItem('pageNumber', pageNum.value);
// onSearch()
// }
// }
// }async function onSearch() {loading.value = true;if(sessionStorage.getItem('pageNumber')){pageNum.value = sessionStorage.getItem('pageNumber')}cameraList.value = [];const { data } = await listApCamera({pageSize:pageSize.value, pageNum:pageNum.value});cameraList.value = [...data.list].map(item=>{return{ip: item.ip, //IP地址port:item.port, //端口号username:item.username, //用户名password: item.password, //密码loadingTime: item.loadingTime,//睡眠加载时长status:item.status, //状态width: "836",height: "385",id: `divPlugin${item.id}`,}})total.value = data.total;pageNum.value = data.currentPage;console.log('cameraList.value=====>>>>>',cameraList.value);// message(`当前为第${pageNum.value}页`, {// type: "success"// });// pageNum.value++setTimeout(() => {loading.value = false;}, 500);
}
onBeforeUnmount(() => {sessionStorage.setItem('pageNumber',1);// window.removeEventListener('scroll', handleScroll);
});</script>
<style lang="scss" scoped>.main-content {margin: 10px 10px 0 !important;overflow: auto;
}//定义滚动条轨道 内阴影+圆角*/
//#style-1::-webkit-scrollbar-track {
// -webkit-box-shadow: inset 0 0 6px rgba(0, 0, 0, 0.3);
// border-radius: 10px;
// background-color: rgba(0, 0, 0, 0.52);
//
//}
//#style-1::-webkit-scrollbar
//{
// width: 12px;
// background-color: #0F65E87C
//}
//
//#style-1::-webkit-scrollbar-thumb
//{
// border-radius: 20px;
// -webkit-box-shadow: inset 0 0 6px rgba(0,0,0,.3);
// background-color: #555;
//}.video-content {display: flex;flex-wrap: wrap;position: relative;width: 100%;height: 85vh;//overflow-y: scroll;//border: 1px solid red;.canvas-contaniner {width: 48.5%!important;height: 49.9%!important;position: relative;display: flex;justify-content: center;align-items: center;padding: 2px 1px;margin-right: 2px;overflow: hidden;}.videobox {display: flex !important;justify-content: center;align-items: center !important;width: 100% !important;height: 100% !important;font-size: 30px;color: #fff;position: relative !important;background: black;overflow: hidden;.video {width: 100% !important;height: 100% !important;object-fit: fill;background-color: #000;}}.paginationBox{display: flex!important;flex-direction: column!important;position: absolute;right: 0px;top: auto;//align-items: center;}::v-deep(.el-pager){display: flex;flex-direction: column;li{margin-bottom: 8px;}}.loading {display: flex;justify-content: center;align-items: center;width: 100%;height: 100%;font-size: 30px;color: #fff;position: absolute;background: black;}
}
</style>
子组件
videoMonitorings.vue
<template><div class="video"><div :id="videoId" class="plugin"/></div>
</template><script>
import {message} from "@/utils/message";
import {reactive,onMounted,onBeforeUnmount,onUnmounted,nextTick,ref,computed,watch
} from "vue";export default {name: "index",props: {videoId: {type: String},hkvInfo: {type: Object || Array,require: true},},setup(props, {emit}) {const hkvInfo = ref({});const videoId = ref();const channels = ref([]);const contentWidth = ref(0)const contentHeight = ref(0)watch(props.hkvInfo, (newVal) => {if (newVal) {console.log('newVal', newVal)hkvInfo.value = newValvideoId.value = newVal.id;let t = newVal.loadingTime || 1000setTimeout(() => {initS();}, t)} else {initS();}}, {deep: true,immediate: true,})const initS = () => {//初始化WebVideoCtrl.I_InitPlugin({bWndFull: true, //是否支持单窗口双击全屏,默认支持 true:支持 false:不支持iWndowType: 1,// 画面分割数 1 就是1*1 2就是 2*2cbSelWnd: function (xmlDoc) {//此属性是窗口分割切换窗口时触发// clickStartRealPlay();// console.log("当前选择的窗口编号是1");},cbInitPluginComplete: function () {WebVideoCtrl.I_InsertOBJECTPlugin(videoId.value).then(() => {// 检查插件是否更新WebVideoCtrl.I_CheckPluginVersion().then((bFlag) => {if (bFlag) {let str = "检测到新的插件版本,双击开发包目录里的HCWebSDKPlugin.exe升级!"message(str, {type: "warning"});}});},() => {window.open('../../../../public/VideoWebPlugin/HCWebSDKPlugin.exe');let str1 = "插件初始化失败,请确认是否已安装插件;如果未安装,请双击开发包目录里的HCWebSDKPlugin.exe安装!";message(str1, {type: "warning"});});},});setTimeout(() => {handleResize();setVideoWindowResize(videoId.value, contentWidth.value, contentHeight.value);clickLogin();}, 1500);}// const getVideoStyle = computed(() => {// return {// width:contentWidth.value,// height:contentHeight.value// };// });//重置窗口大小const handleResize = () =>{contentWidth.value = document.getElementById(videoId.value).offsetWidth;contentHeight.value = document.getElementById(videoId.value).offsetHeight;WebVideoCtrl.I_Resize(contentWidth.value,contentHeight.value,)}// 根据设备宽高 和 windowchange 设置窗口大小const setVideoWindowResize = (pid, width, height) => {const mapContainer = document.getElementById(pid);// 初始化获取不到值if (mapContainer && (!mapContainer.style.width || !mapContainer.style.height)) {mapContainer.style.width = `${width}px`;mapContainer.style.height = `${height}px`;console.log('mapContainer',mapContainer)}}// 登录const clickLogin = () => {let szIP = hkvInfo.value.ip,szPort = hkvInfo.value.port,szUsername = hkvInfo.value.username,szPassword = hkvInfo.value.password;const iRet = WebVideoCtrl.I_Login(szIP, 1, szPort, szUsername, szPassword, {timeout: 3000,success: function (xmlDoc) {console.log('xmlDoc',xmlDoc)setTimeout(function () {setTimeout(function () {getChannelInfo();}, 1000);getDevicePort();}, 10);},error: function (err) {console.log('hkvInfo',hkvInfo)console.log("登录-err===>", err);},});if (iRet === -1) {console.log(szIP +"_"+ szPort + " 已登录过!");// 登录过直接进行预览clickStartRealPlay();}}const getDevicePort = () => {let szDeviceIdentify = hkvInfo.value.ip;if (null == szDeviceIdentify) {return;}WebVideoCtrl.I_GetDevicePort(szDeviceIdentify).then((oPort) => {console.log("获取端口", oPort);});}//获取通道const getChannelInfo = () => {let szDeviceIdentify = hkvInfo.value.ip + '_' + hkvInfo.value.port;if (null == szDeviceIdentify) {return;}console.log('szDeviceIdentify==============>', szDeviceIdentify);// 模拟通道WebVideoCtrl.I_GetAnalogChannelInfo(szDeviceIdentify, {success: function (xmlDoc) {clickStartRealPlay();},error: function (oError) {console.log(szDeviceIdentify + "模拟通道", oError.errorCode, oError.errorMsg);}});// 数字通道WebVideoCtrl.I_GetDigitalChannelInfo(szDeviceIdentify, {success: function (xmlDoc) {clickStartRealPlay();},error: function (oError) {console.log(szDeviceIdentify + " 数字通道", oError.errorCode, oError.errorMsg);}});}// 开始预览const clickStartRealPlay = (iStreamType) => {let g_iWndIndex = 0;const oWndInfo = WebVideoCtrl.I_GetWindowStatus(g_iWndIndex) || null;const szDeviceIdentify = hkvInfo.value.ip + '_' + hkvInfo.value.port,iChannelID = 1,bZeroChannel = false;if ("undefined" === typeof iStreamType) {iStreamType = 1;}if (null == szDeviceIdentify) {return;}const startRealPlay = function () {WebVideoCtrl.I_StartRealPlay(szDeviceIdentify, {iStreamType: iStreamType,iChannelID: iChannelID,bZeroChannel: bZeroChannel,success: function () {WebVideoCtrl.I_Logout(szDeviceIdentify);console.log(szDeviceIdentify, "开始预览!");},error: function (oError) {console.log(szDeviceIdentify + "开始预览失败!");},});};//已经在播放了,先停止if (oWndInfo != null) {WebVideoCtrl.I_Stop({success: function () {startRealPlay();}});} else {startRealPlay();}}// 停止预览const clickStopRealPlay = () => {WebVideoCtrl.I_Stop({success: function () {console.log("停止预览成功!");},error: function (oError) {console.log("停止预览失败!");},});}//登出设备const loginOut = () => {const szDeviceIdentify = hkvInfo.value.ip + "_" + hkvInfo.value.port;WebVideoCtrl.I_Logout(szDeviceIdentify);}//停止全部播放const stopAllPlay = () => {WebVideoCtrl.I_StopAllPlay();}//销毁插件const breakDom = () => {WebVideoCtrl.I_DestroyPlugin()}const viewReload = () => {window.location.reload();}onMounted(() => {window.addEventListener("resize", handleResize);});onBeforeUnmount(() => {loginOut();stopAllPlay();window.removeEventListener(viewReload());window.removeEventListener("resize", handleResize);});onUnmounted(() => {setTimeout(() => {breakDom();}, 100)})return {loginOut,stopAllPlay,breakDom,hkvInfo,contentWidth,handleResize,videoId,// getVideoStyle,contentHeight,initS,viewReload,channels,clickLogin,clickStopRealPlay,clickStartRealPlay,setVideoWindowResize,getChannelInfo,getDevicePort,};}};
</script><style scoped lang="scss">
.video {width: 100%;height: 100%;.plugin{width: 100%!important;height: 100%!important;z-index: 1!important;}
}</style>