videoBox.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  1. <template>
  2. <div class="video-wall-container">
  3. <transition-group
  4. name="grid-fade"
  5. tag="div"
  6. class="video-grid"
  7. :style="gridStyle"
  8. >
  9. <div
  10. v-for="(item, index) in currentBoxList"
  11. :key="item.boxId"
  12. :id="'childBox_' + item.boxId"
  13. class="video-cell"
  14. :class="{
  15. 'is-active': selectedMonitor && selectedMonitor.boxId === item.boxId,
  16. 'is-dragging': draggedItem && draggedItem.boxId === item.boxId,
  17. 'is-recording': item.recording
  18. }"
  19. draggable="true"
  20. @click="setActiveBox(item)"
  21. @dblclick="changeToOne(item)"
  22. @dragstart="handleDragStart(item, $event)"
  23. @dragover="handleDragOver($event)"
  24. @drop="handleDrop(item, $event)"
  25. @dragend="handleDragEnd($event)"
  26. >
  27. <div class="video-wrapper">
  28. <!-- :video-src='active===1 ? item.mainUrl : item.auxiliaryUrl'-->
  29. <webrtc-player
  30. v-if="(item.code === 'H264' || item.code !== 'H265') && item.mainUrl"
  31. :video-src="active===1 ? item.mainUrl : item.auxiliaryUrl"
  32. :video-id="item.boxId"
  33. :camera-name="item.name"
  34. ref='player'
  35. @destroy="close"
  36. class="player-instance"
  37. />
  38. <!-- :video-src='active===1 ? item.mainUrl : item.auxiliaryUrl'-->
  39. <jessibucaBox
  40. v-else-if="item.code === 'H265' && item.mainUrl"
  41. :video-src="active===1 ? item.mainUrl : item.auxiliaryUrl"
  42. :video-id="item.boxId"
  43. :camera-name="item.name"
  44. ref='player'
  45. @destroy="close"
  46. class="player-instance"
  47. ></jessibucaBox>
  48. <!-- <video-player-->
  49. <!-- v-else-if="item.hls"-->
  50. <!-- class="video-player-box"-->
  51. <!-- style="width: 100%; height: 90%"-->
  52. <!-- ref="videoPlayer"-->
  53. <!-- :options="{-->
  54. <!-- playbackRates: [0.7, 1.0, 1.5, 2.0], // 播放速度-->
  55. <!-- autoplay: true, // 禁止自动播放(浏览器限制)-->
  56. <!-- muted: true, // 静音-->
  57. <!-- controls: true, // 显示控制栏-->
  58. <!-- preload: 'auto',-->
  59. <!-- techOrder: ['html5', 'hls'], // 优先用 HLS 插件解析-->
  60. <!-- width: '100%',-->
  61. <!-- height: '100%',-->
  62. <!-- sources: [-->
  63. <!-- {-->
  64. <!-- type: 'application/x-mpegURL', // m3u8 对应的 MIME 类型-->
  65. <!-- src: 'http://10.168.1.232:8889/38/SIS-1766510219282/whep'-->
  66. <!-- }-->
  67. <!-- ],-->
  68. <!-- notSupportedMessage: '当前浏览器不支持播放此视频'-->
  69. <!-- }"-->
  70. <!-- />-->
  71. <div v-else class="empty-state">
  72. <i class="el-icon-video-camera"></i>
  73. <span>无信号</span>
  74. </div>
  75. </div>
  76. <div class="video-overlay">
  77. <span class="video-title">{{ item.name || `通道 ${item.boxId}` }}</span>
  78. <div class="status-indicators">
  79. <i v-if="item.recording" class="el-icon-loading recording-icon" title="录像中"></i>
  80. </div>
  81. </div>
  82. </div>
  83. </transition-group>
  84. </div>
  85. </template>
  86. <script>
  87. import player from '@/components/Jessibuca'
  88. import webrtcPlayer from "@/components/WebrtcPlayer";
  89. import jessibucaBox from './jessibucaBox.vue'
  90. import { mapGetters } from 'vuex'
  91. export default {
  92. name: 'VideoWall',
  93. components: { player, webrtcPlayer, jessibucaBox },
  94. data() {
  95. return {
  96. draggedItem: null,
  97. // 防抖定时器
  98. timer: null,
  99. }
  100. },
  101. computed: {
  102. ...mapGetters(['boxList', 'active', 'lastActive', 'selectedTreeData', 'selectedMonitor', 'boxListIndex']),
  103. // 获取当前页面需要渲染的列表
  104. currentBoxList() {
  105. // 逻辑保持不变,根据 active 数量截取
  106. // 注意:这里假设你的 boxListIndex 和 active 逻辑是分页用的
  107. // 如果 boxList 长度固定为24,这里可能需要根据分页逻辑调整 slice
  108. // 此处沿用你原本的逻辑: boxList.slice(boxListIndex)
  109. // 但通常建议精确截取: boxList.slice(startIndex, startIndex + pageSize)
  110. // 修正建议:根据 active 数量,精确切分当前页数据,避免v-for渲染过多隐藏元素
  111. let count = 0;
  112. switch (this.active) {
  113. case 1:
  114. count = 1;
  115. break;
  116. case 4:
  117. count = 4;
  118. break;
  119. case 9:
  120. count = 9;
  121. break;
  122. case 16:
  123. count = 16;
  124. break;
  125. case 36:
  126. count = 36;
  127. break;
  128. case 64:
  129. count = 64;
  130. break;
  131. default:
  132. count = this.active;
  133. }
  134. // 假设 boxListIndex 是起始索引
  135. return this.boxList.slice(this.boxListIndex, this.boxListIndex + count);
  136. },
  137. // [核心优化] 使用 CSS Grid 动态计算样式
  138. gridStyle() {
  139. let cols = 1;
  140. switch (this.active) {
  141. case 1:
  142. cols = 1;
  143. break;
  144. case 4:
  145. cols = 2;
  146. break;
  147. case 9:
  148. cols = 3;
  149. break;
  150. case 16:
  151. cols = 4;
  152. break;
  153. case 36:
  154. cols = 6;
  155. break;
  156. case 64:
  157. cols = 8;
  158. break;
  159. default:
  160. cols = Math.ceil(Math.sqrt(this.active));
  161. }
  162. return {
  163. 'grid-template-columns': `repeat(${cols}, 1fr)`,
  164. 'grid-template-rows': `repeat(${cols}, 1fr)`
  165. }
  166. }
  167. },
  168. methods: {
  169. // --- 业务逻辑保持原有不变,仅优化部分写法 ---
  170. stopRecordings(boxesToStop, shouldSave) {
  171. if (!this.$refs.player || this.$refs.player.length === 0) return;
  172. boxesToStop.forEach(box => {
  173. const playerComponent = this.$refs.player.find(p => p.videoId === box.boxId);
  174. if (playerComponent && typeof playerComponent.stopRecording === 'function') {
  175. playerComponent.stopRecording(shouldSave);
  176. }
  177. });
  178. },
  179. closeAllVideos() {
  180. this.boxList.forEach(item => {
  181. if (item.mainUrl || item.auxiliaryUrl) {
  182. this.setActiveBox(item);
  183. Object.assign(item, {
  184. mainUrl: '', auxiliaryUrl: '', code: '', cameraId: '',
  185. name: '', rtc: '', rtsp: '', recording: false
  186. });
  187. }
  188. });
  189. this.$store.commit('updateSelectedMonitor', {});
  190. this.$emit('clearTreeSelected');
  191. this.$message.success('所有预览已关闭');
  192. },
  193. changeToOne(item) {
  194. if (this.timer) {
  195. clearTimeout(this.timer);
  196. this.timer = null;
  197. }
  198. // 简单的防录像中断逻辑
  199. if (this.boxList.some(b => b.recording) && this.active !== 1) {
  200. this.$message.warning('正在录像中,切换分屏可能会中断录像!');
  201. // 这里看业务需求是否强制return
  202. // return
  203. }
  204. if (this.lastActive !== 0) {
  205. // 恢复多屏
  206. this.$store.commit('updateBoxListIndex', 0)
  207. this.$store.commit('updateActive', this.lastActive)
  208. this.$store.commit('updateLastActive', 0)
  209. } else {
  210. if (this.active === 1) return;
  211. // 切换单屏
  212. this.$store.commit('updateLastActive', this.active)
  213. // 计算正确的 index (原逻辑)
  214. const newIndex = item.boxId - 1;
  215. // 注意:这里要确保 boxId 和 索引 的对应关系是强绑定的
  216. this.$store.commit('updateBoxListIndex', newIndex)
  217. this.$store.commit('updateActive', 1)
  218. }
  219. },
  220. close(boxId) {
  221. // 1. 深拷贝一份当前的 boxList,避免直接修改 Vuex 数据
  222. let newBoxList = JSON.parse(JSON.stringify(this.boxList));
  223. // 2. 在拷贝的数据中找到对应的窗口并清空数据
  224. const index = newBoxList.findIndex(item => item.boxId === boxId);
  225. if (index !== -1) {
  226. // 保留 boxId,清空其他业务数据
  227. newBoxList[index] = {
  228. boxId: boxId, // 核心ID不能丢
  229. mainUrl: '',
  230. auxiliaryUrl: '',
  231. code: '',
  232. cameraId: '',
  233. name: '',
  234. rtc: '',
  235. rtsp: '',
  236. recording: false
  237. };
  238. }
  239. // 3. 通过 Vuex 提交更新(这会立即触发视图重绘)
  240. this.$store.commit('updateBoxList', newBoxList);
  241. // 4. 清理选中状态和红框
  242. this.showRedBorder = false;
  243. // 5. 如果需要,通知父组件清除树列表的选中状态
  244. this.$emit('clearTreeSelected');
  245. },
  246. setActiveBox(item) {
  247. console.log(this.$store.state.video)
  248. this.$store.commit('updateSelectedMonitor', item);
  249. if (item.cameraId) {
  250. this.$emit('setCurrentKeyTree', item.cameraId);
  251. } else {
  252. this.$emit('clearTreeSelected');
  253. }
  254. },
  255. // --- 拖拽逻辑 (保持核心逻辑,移除DOM操作样式的部分,改用数据驱动class) ---
  256. handleDragStart(item, event) {
  257. if (item.recording) {
  258. this.$message.warning('无法拖拽正在录像的窗口!');
  259. event.preventDefault();
  260. return;
  261. }
  262. this.draggedItem = item;
  263. event.dataTransfer.effectAllowed = 'move';
  264. // 这里的 opacity 通过 css class .is-dragging 控制,不需要 js 操作 style
  265. },
  266. handleDragOver(event) {
  267. event.preventDefault();
  268. event.dataTransfer.dropEffect = 'move';
  269. },
  270. handleDragEnd() {
  271. this.draggedItem = null;
  272. },
  273. handleDrop(targetItem, event) {
  274. event.preventDefault();
  275. const sourceItem = this.draggedItem;
  276. if (!sourceItem || sourceItem.boxId === targetItem.boxId) return;
  277. if (targetItem.recording) {
  278. this.$message.warning('无法拖拽到正在录像的窗口!');
  279. return;
  280. }
  281. // 深拷贝并交换
  282. const newBoxList = JSON.parse(JSON.stringify(this.boxList));
  283. const sourceIndex = newBoxList.findIndex(b => b.boxId === sourceItem.boxId);
  284. const targetIndex = newBoxList.findIndex(b => b.boxId === targetItem.boxId);
  285. if (sourceIndex !== -1 && targetIndex !== -1) {
  286. const { boxId: sId, ...sData } = newBoxList[sourceIndex];
  287. const { boxId: tId, ...tData } = newBoxList[targetIndex];
  288. newBoxList[sourceIndex] = { ...tData, boxId: sId };
  289. newBoxList[targetIndex] = { ...sData, boxId: tId };
  290. this.$store.commit('updateBoxList', newBoxList);
  291. // 交换后如果其中一个是选中状态,需要更新选中项的引用
  292. if (this.selectedMonitor && (this.selectedMonitor.boxId === sId || this.selectedMonitor.boxId === tId)) {
  293. const activeId = this.selectedMonitor.boxId === sId ? tId : sId; // 逻辑可能需要根据业务调整,这里仅做提示
  294. // 简单做法:交换后重置选中或者保持ID选中
  295. }
  296. }
  297. this.draggedItem = null;
  298. }
  299. }
  300. }
  301. </script>
  302. <style lang="scss" scoped>
  303. /* 定义变量方便主题切换 */
  304. $bg-color: #1a1a1a;
  305. $cell-bg: #000000;
  306. $border-color: #333333;
  307. $active-color: #409eff; // 选中色,也可以换成亮青色 #00ffff
  308. $text-color: #eeeeee;
  309. .video-wall-container {
  310. width: 100%;
  311. height: 100%;
  312. background-color: $bg-color;
  313. padding: 2px; /* 微小的外边距 */
  314. box-sizing: border-box;
  315. overflow: hidden;
  316. }
  317. .video-grid {
  318. display: grid;
  319. width: 100%;
  320. height: 100%; /* 撑满容器 */
  321. /* 核心:使用 gap 代替 margin,更整齐 */
  322. gap: 2px;
  323. /* 保持 16:9 比例的核心逻辑,如果想铺满屏幕可移除 aspect-ratio */
  324. // aspect-ratio: 16/9;
  325. // margin: 0 auto;
  326. }
  327. .video-cell {
  328. position: relative;
  329. background-color: $cell-bg;
  330. border: 1px solid $border-color;
  331. overflow: hidden;
  332. display: flex;
  333. flex-direction: column;
  334. cursor: pointer;
  335. transition: all 0.2s ease-in-out;
  336. /* 确保子元素绝对定位基于此 */
  337. z-index: 1;
  338. /* 拖拽样式 */
  339. &.is-dragging {
  340. opacity: 0.4;
  341. border: 2px dashed #999;
  342. }
  343. /* 选中样式:外发光+高亮边框 */
  344. &.is-active {
  345. border: 1px solid $active-color;
  346. z-index: 10; /* 选中时层级提高,防止边框被遮挡 */
  347. box-shadow: 0 0 0 1px rgba($active-color, 0.5) inset, 0 0 8px rgba($active-color, 0.6);
  348. }
  349. /* 录像中样式 */
  350. &.is-recording {
  351. &::after {
  352. content: '';
  353. position: absolute;
  354. top: 6px;
  355. right: 6px;
  356. width: 8px;
  357. height: 8px;
  358. border-radius: 50%;
  359. background-color: #f56c6c;
  360. animation: blink 1s infinite;
  361. z-index: 20;
  362. }
  363. }
  364. &:hover {
  365. border-color: lighten($border-color, 20%);
  366. .video-overlay {
  367. opacity: 1;
  368. transform: translateY(0);
  369. }
  370. }
  371. }
  372. .video-wrapper {
  373. width: 100%;
  374. height: 100%;
  375. position: relative;
  376. .player-instance {
  377. width: 100%;
  378. height: 100%;
  379. object-fit: fill; /* 根据需求改为 contain */
  380. display: block;
  381. }
  382. }
  383. .empty-state {
  384. width: 100%;
  385. height: 100%;
  386. display: flex;
  387. flex-direction: column;
  388. justify-content: center;
  389. align-items: center;
  390. color: #555;
  391. i {
  392. font-size: 48px;
  393. margin-bottom: 10px;
  394. }
  395. span {
  396. font-size: 14px;
  397. }
  398. }
  399. /* 悬浮遮罩层 - 类似 Youtube/Bilibili 效果 */
  400. .video-overlay {
  401. position: absolute;
  402. bottom: 0;
  403. left: 0;
  404. width: 100%;
  405. padding: 5px 10px;
  406. background: linear-gradient(to top, rgba(0, 0, 0, 0.8), rgba(0, 0, 0, 0));
  407. color: white;
  408. display: flex;
  409. justify-content: space-between;
  410. align-items: center;
  411. z-index: 5;
  412. /* 默认稍微隐藏,hover时完全显示 */
  413. opacity: 0.8;
  414. transition: opacity 0.3s;
  415. .video-title {
  416. font-size: 12px;
  417. text-shadow: 0 1px 2px rgba(0, 0, 0, 1);
  418. white-space: nowrap;
  419. overflow: hidden;
  420. text-overflow: ellipsis;
  421. }
  422. }
  423. /* 录像红点闪烁 */
  424. @keyframes blink {
  425. 0% {
  426. opacity: 1;
  427. }
  428. 50% {
  429. opacity: 0.4;
  430. }
  431. 100% {
  432. opacity: 1;
  433. }
  434. }
  435. /* 网格切换动画 */
  436. .grid-fade-enter-active, .grid-fade-leave-active {
  437. transition: all 0.3s;
  438. }
  439. .grid-fade-enter, .grid-fade-leave-to {
  440. opacity: 0;
  441. transform: scale(0.9);
  442. }
  443. </style>