index.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  1. <template>
  2. <div class="camera-container">
  3. <div class="left-area">
  4. <cameraTree :selected="selected" :list="listArr" :draggable="true" @detail-node="onDetail" @on-click="onClick">
  5. <template #icon="{ item }">
  6. <template v-if="item.isFolder">
  7. <SvgIcon v-if="item.expanded" size="18" name="file-open" />
  8. <SvgIcon v-else size="18" name="file-close" />
  9. </template>
  10. <treeIcon class="iconfont" :title="item.title" v-else />
  11. </template>
  12. <template #operation="{ type }">
  13. <!-- <i class="iconfont icon-eyeoutlined"></i> -->
  14. <span style="color:#ccc;font-size:12px">详情</span>
  15. </template>
  16. </cameraTree>
  17. </div>
  18. <div class="right-area" v-if="addrList.length > 0">
  19. <div class="vent-flex-row-wrap camera-box">
  20. <div v-for="(item, index) in addrList" :key="index" class="player-box">
  21. <div class="player-name">{{ item.name }}</div>
  22. <div style="padding-top:3px">
  23. <template v-if="item.addr.startsWith('rtsp://')">
  24. <video :id="`video${index}`" muted autoplay></video>
  25. <div class="click-box" @dblclick="goFullScreen(`video${index}`)"></div>
  26. </template>
  27. <template v-else>
  28. <div :id="'player' + index"></div>
  29. </template>
  30. </div>
  31. </div>
  32. </div>
  33. <div class="pagination">
  34. <Pagination v-model:current="current" v-model:page-size="pageSize" :total="total" @change="onChange" />
  35. </div>
  36. </div>
  37. <div class="camera-box" v-else>
  38. <Empty />
  39. </div>
  40. </div>
  41. </template>
  42. <script lang="ts" setup>
  43. import { onMounted, onUnmounted, ref, reactive } from 'vue';
  44. import { useRouter } from 'vue-router';
  45. import { Pagination, Empty } from 'ant-design-vue';
  46. import { list, cameraAddr, getCameraDevKind, getDevice, getVentanalyCamera } from './camera.api'
  47. import Player, { I18N } from 'xgplayer';
  48. import ZH from 'xgplayer/es/lang/zh-cn'
  49. import HlsPlugin from 'xgplayer-hls';
  50. import FlvPlugin from 'xgplayer-flv';
  51. import 'xgplayer/dist/index.min.css';
  52. import cameraTree from './common/cameraTree.vue';
  53. import { SvgIcon } from '/@/components/Icon';
  54. import treeIcon from './common/Icon/treeIcon.vue';
  55. //当前选中树节点
  56. let selected = reactive<any>({
  57. id: null,
  58. pid: null,
  59. title: '',
  60. isFolder: false,
  61. });
  62. //tree菜单列表
  63. let listArr = reactive<any[]>([]);
  64. let searchParam = reactive({
  65. devKind: '',
  66. strType: '',
  67. })
  68. I18N.use(ZH)
  69. let router = useRouter(); //路由
  70. const pageSize = ref(8)
  71. const current = ref(1)
  72. const total = ref(0)
  73. const playerList = ref([])
  74. const webRtcServerList = <any[]>[]
  75. let addrList = ref<{ name: string, addr: string }[]>([])
  76. async function getCameraDevKindList() {
  77. let res = await getCameraDevKind()
  78. if (res.length != 0) {
  79. listArr.length = 0
  80. listArr.push({
  81. pid: 'root',
  82. isFolder: true,
  83. expanded: true,
  84. title: '全部',
  85. id: 0,
  86. children: []
  87. })
  88. res.forEach(el => {
  89. el.pid = 0
  90. el.isFolder = true,
  91. el.expanded = false,
  92. el.title = el.itemText
  93. el.id = el.subDictId
  94. el.children = []
  95. listArr[0].children.push(el)
  96. })
  97. selected.id = listArr[0].id;
  98. selected.pid = listArr[0].pid;
  99. selected.title = listArr[0].title;
  100. selected.isFolder = listArr[0].isFolder;
  101. }
  102. }
  103. //点击目录
  104. async function onClick(node) {
  105. if(selected.title === node.title && selected.id === node.id) return
  106. current.value = 1
  107. selected.id = node.id;
  108. selected.pid = node.pid;
  109. selected.title = node.title;
  110. selected.isFolder = node.isFolder;
  111. if (node.pid != 'root') {
  112. if (node.isFolder) {
  113. let res = await getDevice({ devicetype: node.itemValue })
  114. if (res.msgTxt.length != 0) {
  115. res.msgTxt[0].datalist.forEach(el => {
  116. el.pid = node.id
  117. el.isFolder = false
  118. el.title = el.strinstallpos
  119. el.id = el.deviceID
  120. })
  121. listArr[0].children.forEach(v => {
  122. if (v.id == node.id) {
  123. v.children = res.msgTxt[0].datalist
  124. }
  125. })
  126. }
  127. searchParam.devKind = node.itemValue
  128. searchParam.strType = ''
  129. await getVideoAddrs()
  130. } else {
  131. getVideoAddrsSon(node.deviceID)
  132. }
  133. } else {
  134. searchParam.devKind = ''
  135. searchParam.strType = ''
  136. await getVideoAddrs()
  137. }
  138. getVideo()
  139. };
  140. //点击详情跳转
  141. function onDetail(node) {
  142. console.log(node, '详情-------------')
  143. switch (node.deviceType) {
  144. case 'gate_qd':
  145. router.push('/monitorChannel/monitor-gate?id=' + node.deviceID)
  146. break;
  147. case 'pump_over':
  148. router.push('/monitorChannel/gasPump-home?id=' + node.deviceID)
  149. break;
  150. case 'pump_under':
  151. router.push('/monitorChannel/gasPump-home?id=' + node.deviceID)
  152. break;
  153. }
  154. }
  155. async function getVideoAddrs() {
  156. clearCamera();
  157. playerList.value = []
  158. let res = await list({ ...searchParam, pageSize: pageSize.value, pageNo: current.value })
  159. total.value = res['total'] || 0
  160. if (res.records.length != 0) {
  161. const cameraList = <{ name: string, addr: string }[]>[]
  162. const cameras = res.records
  163. for (let i = 0; i < cameras.length; i++) {
  164. const item = cameras[i];
  165. if (item['devicekind'] === 'toHKRtsp') {
  166. // 从海康平台接口获取视频流
  167. try {
  168. const data = await cameraAddr({ cameraCode: item['addr'] });
  169. if (data) {
  170. cameraList.push({ name: item['name'], addr: data['url'] });
  171. }
  172. // cameraList.push({
  173. // name: item['name'],
  174. // // addr: 'http://219.151.31.38/liveplay-kk.rtxapp.com/live/program/live/hnwshd/4000000/mnf.m3u8'
  175. // addr: 'https://demo.unified-streaming.com/k8s/features/stable/video/tears-of-steel/tears-of-steel.mp4/.m3u8',
  176. // });
  177. } catch (error) {
  178. }
  179. } else {
  180. if (item['addr'].includes('0.0.0.0')) {
  181. item['addr'] = item['addr'].replace('0.0.0.0', window.location.hostname)
  182. }
  183. cameraList.push({ name: item['name'], addr: item['addr'] });
  184. }
  185. }
  186. addrList.value = cameraList
  187. }
  188. }
  189. async function getVideoAddrsSon(Id) {
  190. clearCamera();
  191. playerList.value = []
  192. let res = await getVentanalyCamera({ deviceid: Id })
  193. console.log(res, 'xin---------------')
  194. if (res.records.length != 0) {
  195. const cameraList = <{ name: string, addr: string }[]>[]
  196. const cameras = res.records
  197. for (let i = 0; i < cameras.length; i++) {
  198. const item = cameras[i];
  199. if (item['devicekind'] === 'toHKRtsp') {
  200. // 从海康平台接口获取视频流
  201. try {
  202. const data = await cameraAddr({ cameraCode: item['addr'] });
  203. if (data && data['url']) {
  204. cameraList.push({ name: item['name'], addr: data['url'] });
  205. }
  206. // cameraList.push({
  207. // name: item['name'],
  208. // // addr: 'http://219.151.31.38/liveplay-kk.rtxapp.com/live/program/live/hnwshd/4000000/mnf.m3u8'
  209. // addr: 'https://demo.unified-streaming.com/k8s/features/stable/video/tears-of-steel/tears-of-steel.mp4/.m3u8',
  210. // });
  211. } catch (error) {
  212. }
  213. } else {
  214. if (item['addr'].includes('0.0.0.0')) {
  215. item['addr'] = item['addr'].replace('0.0.0.0', window.location.hostname)
  216. }
  217. cameraList.push({ name: item['name'], addr: item['addr'] });
  218. }
  219. }
  220. addrList.value = cameraList
  221. }
  222. }
  223. function onChange(page) {
  224. current.value = page;
  225. getVideoAddrs().then(() => {
  226. getVideo()
  227. })
  228. }
  229. function getVideo() {
  230. const ip = VUE_APP_URL.webRtcUrl;
  231. for (let i = 0; i < addrList.value.length; i++) {
  232. const item = addrList.value[i]
  233. if (item.addr.startsWith('rtsp://')) {
  234. const dom = document.getElementById('video' + i) as HTMLVideoElement
  235. dom.muted = true;
  236. dom.volume = 0
  237. const webRtcServer = new window['WebRtcStreamer'](dom, location.protocol + ip)
  238. webRtcServerList.push(webRtcServer)
  239. webRtcServer.connect(item.addr)
  240. } else {
  241. setNoRtspVideo('player' + i, item.addr)
  242. }
  243. }
  244. }
  245. function setNoRtspVideo(id, videoAddr) {
  246. const fileExtension = videoAddr.split('.').pop();
  247. if (fileExtension === 'flv') {
  248. const player = new Player({
  249. lang: 'zh',
  250. id: id,
  251. url: videoAddr,
  252. width: 354,
  253. height: 245,
  254. poster: '/src/assets/images/vent/noSinge.png',
  255. plugins: [FlvPlugin],
  256. fluid: true,
  257. autoplay: true,
  258. isLive: true,
  259. playsinline: false,
  260. screenShot: true,
  261. whitelist: [''],
  262. ignores: ['time'],
  263. closeVideoClick: true,
  264. customConfig: {
  265. isClickPlayBack: false
  266. },
  267. flv: {
  268. retryCount: 3, // 重试 3 次,默认值
  269. retryDelay: 1000, // 每次重试间隔 1 秒,默认值
  270. loadTimeout: 10000, // 请求超时时间为 10 秒,默认值
  271. fetchOptions: {
  272. // 该参数会透传给 fetch,默认值为 undefined
  273. mode: 'cors'
  274. },
  275. targetLatency: 10, // 直播目标延迟,默认 10 秒
  276. maxLatency: 20, // 直播允许的最大延迟,默认 20 秒
  277. disconnectTime: 10, // 直播断流时间,默认 0 秒,(独立使用时等于 maxLatency)
  278. maxJumpDistance: 10,
  279. }
  280. });
  281. playerList.value.push(player)
  282. }
  283. if (fileExtension === 'm3u8') {
  284. let player
  285. if (document.createElement('video').canPlayType('application/vnd.apple.mpegurl')) {
  286. // 原生支持 hls 播放
  287. player = new Player({
  288. lang: 'zh',
  289. id: id,
  290. url: videoAddr,
  291. width: 354,
  292. height: 245,
  293. isLive: true,
  294. autoplay: true,
  295. autoplayMuted: true,
  296. cors: true,
  297. poster: '/src/assets/images/vent/noSinge.png',
  298. hls: {
  299. retryCount: 3, // 重试 3 次,默认值
  300. retryDelay: 1000, // 每次重试间隔 1 秒,默认值
  301. loadTimeout: 10000, // 请求超时时间为 10 秒,默认值
  302. fetchOptions: {
  303. // 该参数会透传给 fetch,默认值为 undefined
  304. mode: 'cors'
  305. },
  306. targetLatency: 10, // 直播目标延迟,默认 10 秒
  307. maxLatency: 20, // 直播允许的最大延迟,默认 20 秒
  308. disconnectTime: 10, // 直播断流时间,默认 0 秒,(独立使用时等于 maxLatency)
  309. maxJumpDistance: 10,
  310. }
  311. })
  312. } else if (HlsPlugin.isSupported()) { // 第一步
  313. player = new Player({
  314. lang: 'zh',
  315. id: id,
  316. url: videoAddr,
  317. width: 354,
  318. height: 245,
  319. isLive: true,
  320. autoplay: true,
  321. autoplayMuted: true,
  322. plugins: [HlsPlugin], // 第二步
  323. poster: '/src/assets/images/vent/noSinge.png',
  324. hls: {
  325. retryCount: 3, // 重试 3 次,默认值
  326. retryDelay: 1000, // 每次重试间隔 1 秒,默认值
  327. loadTimeout: 10000, // 请求超时时间为 10 秒,默认值
  328. fetchOptions: {
  329. // 该参数会透传给 fetch,默认值为 undefined
  330. mode: 'cors'
  331. },
  332. targetLatency: 10, // 直播目标延迟,默认 10 秒
  333. maxLatency: 20, // 直播允许的最大延迟,默认 20 秒
  334. disconnectTime: 10, // 直播断流时间,默认 0 秒,(独立使用时等于 maxLatency)
  335. maxJumpDistance: 10,
  336. }
  337. })
  338. }
  339. playerList.value.push(player)
  340. }
  341. }
  342. function goFullScreen(domId) {
  343. const videoDom = document.getElementById(domId) as HTMLVideoElement
  344. if (videoDom.requestFullscreen) {
  345. videoDom.requestFullscreen()
  346. videoDom.play()
  347. } else if (videoDom.mozRequestFullscreen) {
  348. videoDom.mozRequestFullscreen()
  349. videoDom.play()
  350. } else if (videoDom.webkitRequestFullscreen) {
  351. videoDom.webkitRequestFullscreen()
  352. videoDom.play()
  353. } else if (videoDom.msRequestFullscreen) {
  354. videoDom.msRequestFullscreen()
  355. videoDom.play()
  356. }
  357. }
  358. function clearCamera() {
  359. const num = webRtcServerList.length
  360. for (let i = 0; i < num; i++) {
  361. webRtcServerList[i].disconnect()
  362. webRtcServerList[i] = null
  363. }
  364. for (let i = 0; i < playerList.value.length; i++) {
  365. const player = playerList.value[i]
  366. if (player.destroy) player.destroy()
  367. }
  368. playerList.value = []
  369. }
  370. onMounted(async () => {
  371. await getVideoAddrs()
  372. // getTreeList()
  373. getCameraDevKindList()
  374. getVideo()
  375. })
  376. onUnmounted(() => {
  377. clearCamera()
  378. })
  379. </script>
  380. <style lang="less">
  381. .camera-container {
  382. position: relative;
  383. width: calc(100% - 30px);
  384. height: calc(100% - 84px);
  385. display: flex;
  386. margin: 15px;
  387. justify-content: space-between;
  388. align-items: center;
  389. .left-area {
  390. width: 15%;
  391. height: 100%;
  392. padding: 20px;
  393. border: 1px solid #99e8ff66;
  394. background: #27546e1a;
  395. box-shadow: 0px 0px 20px 7px rgba(145, 233, 254, 0.7) inset;
  396. -moz-box-shadow: 0px 0px 20px 7px rgba(145, 233, 254, 0.7) inset;
  397. -webkit-box-shadow: 0px 0px 50px 1px rgb(149 235 255 / 5%) inset;
  398. box-sizing: border-box;
  399. // lxh
  400. .iconfont {
  401. color: #fff;
  402. font-size: 12px;
  403. margin-left: 5px;
  404. }
  405. }
  406. .right-area {
  407. width: 85%;
  408. height: 100%;
  409. padding: 0px 0px 0px 15px;
  410. box-sizing: border-box;
  411. .camera-box {
  412. height: calc(100% - 60px);
  413. display: flex;
  414. justify-content: flex-start;
  415. align-items: flex-start;
  416. flex-wrap: wrap;
  417. overflow-y: auto;
  418. .player-box {
  419. width: 375px;
  420. height: 272px;
  421. padding: 10px;
  422. background: url('/@/assets/images/vent/camera_bg.png');
  423. background-size: 100% 100%;
  424. position: relative;
  425. margin: 10px;
  426. .player-name {
  427. font-size: 14px;
  428. position: absolute;
  429. top: 15px;
  430. right: 15px;
  431. color: #fff;
  432. background-color: hsla(0, 0%, 50%, .5);
  433. border-radius: 2px;
  434. padding: 1px 5px;
  435. max-width: 120px;
  436. overflow: hidden;
  437. white-space: nowrap;
  438. text-overflow: ellipsis;
  439. z-index: 999;
  440. }
  441. .click-box {
  442. position: absolute;
  443. width: 100%;
  444. height: 100%;
  445. top: 0;
  446. left: 0;
  447. }
  448. }
  449. }
  450. .pagination {
  451. width: 100%;
  452. height: 60px;
  453. display: flex;
  454. justify-content: center;
  455. align-items: center;
  456. }
  457. }
  458. }
  459. :deep(video) {
  460. width: 100% !important;
  461. height: 100% !important;
  462. object-fit: cover !important;
  463. }
  464. </style>