index.vue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  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" :class="addrList.length == 1 ? 'camera-box1' : '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(4)
  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. // case 'window_normal'://自动风窗
  154. // router.push('/monitorChannel/gasPump-home?id=' + node.deviceID)
  155. // break;
  156. // case 'gate_xinjianfuxiejin'://自动风门
  157. // router.push('/monitorChannel/gasPump-home?id=' + node.deviceID)
  158. // break;
  159. }
  160. }
  161. async function getVideoAddrs() {
  162. clearCamera();
  163. playerList.value = []
  164. let res = await list({ ...searchParam, pageSize: pageSize.value, pageNo: current.value })
  165. total.value = res['total'] || 0
  166. if (res.records.length != 0) {
  167. const cameraList = <{ name: string, addr: string }[]>[]
  168. const cameras = res.records
  169. for (let i = 0; i < cameras.length; i++) {
  170. const item = cameras[i];
  171. if (item['devicekind'] === 'toHKRtsp') {
  172. // 从海康平台接口获取视频流
  173. try {
  174. const data = await cameraAddr({ cameraCode: item['addr'] });
  175. if (data) {
  176. cameraList.push({ name: item['name'], addr: data['url'] });
  177. }
  178. // cameraList.push({
  179. // name: item['name'],
  180. // // addr: 'http://219.151.31.38/liveplay-kk.rtxapp.com/live/program/live/hnwshd/4000000/mnf.m3u8'
  181. // addr: 'https://demo.unified-streaming.com/k8s/features/stable/video/tears-of-steel/tears-of-steel.mp4/.m3u8',
  182. // });
  183. } catch (error) {
  184. }
  185. } else {
  186. if (item['addr'].includes('0.0.0.0')) {
  187. item['addr'] = item['addr'].replace('0.0.0.0', window.location.hostname)
  188. }
  189. cameraList.push({ name: item['name'], addr: item['addr'] });
  190. }
  191. }
  192. addrList.value = cameraList
  193. }
  194. }
  195. async function getVideoAddrsSon(Id) {
  196. clearCamera();
  197. playerList.value = []
  198. let res = await getVentanalyCamera({ deviceid: Id })
  199. if (res.records.length != 0) {
  200. const cameraList = <{ name: string, addr: string }[]>[]
  201. const cameras = res.records
  202. for (let i = 0; i < cameras.length; i++) {
  203. const item = cameras[i];
  204. if (item['devicekind'] === 'toHKRtsp') {
  205. // 从海康平台接口获取视频流
  206. try {
  207. const data = await cameraAddr({ cameraCode: item['addr'] });
  208. if (data && data['url']) {
  209. cameraList.push({ name: item['name'], addr: data['url'] });
  210. }
  211. // cameraList.push({
  212. // name: item['name'],
  213. // // addr: 'http://219.151.31.38/liveplay-kk.rtxapp.com/live/program/live/hnwshd/4000000/mnf.m3u8'
  214. // addr: 'https://demo.unified-streaming.com/k8s/features/stable/video/tears-of-steel/tears-of-steel.mp4/.m3u8',
  215. // });
  216. } catch (error) {
  217. }
  218. } else {
  219. if (item['addr'].includes('0.0.0.0')) {
  220. item['addr'] = item['addr'].replace('0.0.0.0', window.location.hostname)
  221. }
  222. cameraList.push({ name: item['name'], addr: item['addr'] });
  223. }
  224. }
  225. addrList.value = cameraList
  226. }
  227. }
  228. function onChange(page) {
  229. current.value = page;
  230. getVideoAddrs().then(() => {
  231. getVideo()
  232. })
  233. }
  234. function getVideo() {
  235. const ip = VUE_APP_URL.webRtcUrl;
  236. for (let i = 0; i < addrList.value.length; i++) {
  237. const item = addrList.value[i]
  238. if (item.addr.startsWith('rtsp://')) {
  239. const dom = document.getElementById('video' + i) as HTMLVideoElement
  240. dom.muted = true;
  241. dom.volume = 0
  242. const webRtcServer = new window['WebRtcStreamer'](dom, location.protocol + ip)
  243. webRtcServerList.push(webRtcServer)
  244. webRtcServer.connect(item.addr)
  245. } else {
  246. setNoRtspVideo('player' + i, item.addr)
  247. }
  248. }
  249. }
  250. function setNoRtspVideo(id, videoAddr) {
  251. const fileExtension = videoAddr.split('.').pop();
  252. if (fileExtension === 'flv') {
  253. const player = new Player({
  254. lang: 'zh',
  255. id: id,
  256. url: videoAddr,
  257. width: 589,
  258. height: 330,
  259. poster: '/src/assets/images/vent/noSinge.png',
  260. plugins: [FlvPlugin],
  261. fluid: true,
  262. autoplay: true,
  263. isLive: true,
  264. playsinline: true,
  265. screenShot: true,
  266. whitelist: [''],
  267. ignores: ['time'],
  268. closeVideoClick: true,
  269. customConfig: {
  270. isClickPlayBack: false
  271. },
  272. flv: {
  273. retryCount: 3, // 重试 3 次,默认值
  274. retryDelay: 1000, // 每次重试间隔 1 秒,默认值
  275. loadTimeout: 10000, // 请求超时时间为 10 秒,默认值
  276. fetchOptions: {
  277. // 该参数会透传给 fetch,默认值为 undefined
  278. mode: 'cors'
  279. },
  280. targetLatency: 10, // 直播目标延迟,默认 10 秒
  281. maxLatency: 20, // 直播允许的最大延迟,默认 20 秒
  282. disconnectTime: 10, // 直播断流时间,默认 0 秒,(独立使用时等于 maxLatency)
  283. maxJumpDistance: 10,
  284. }
  285. });
  286. playerList.value.push(player)
  287. }
  288. if (fileExtension === 'm3u8') {
  289. let player
  290. if (document.createElement('video').canPlayType('application/vnd.apple.mpegurl')) {
  291. // 原生支持 hls 播放
  292. player = new Player({
  293. lang: 'zh',
  294. id: id,
  295. url: videoAddr,
  296. width: 589,
  297. height: 330,
  298. isLive: true,
  299. autoplay: true,
  300. autoplayMuted: true,
  301. cors: true,
  302. poster: '/src/assets/images/vent/noSinge.png',
  303. hls: {
  304. retryCount: 3, // 重试 3 次,默认值
  305. retryDelay: 1000, // 每次重试间隔 1 秒,默认值
  306. loadTimeout: 10000, // 请求超时时间为 10 秒,默认值
  307. fetchOptions: {
  308. // 该参数会透传给 fetch,默认值为 undefined
  309. mode: 'cors'
  310. },
  311. targetLatency: 10, // 直播目标延迟,默认 10 秒
  312. maxLatency: 20, // 直播允许的最大延迟,默认 20 秒
  313. disconnectTime: 10, // 直播断流时间,默认 0 秒,(独立使用时等于 maxLatency)
  314. maxJumpDistance: 10,
  315. }
  316. })
  317. } else if (HlsPlugin.isSupported()) { // 第一步
  318. player = new Player({
  319. lang: 'zh',
  320. id: id,
  321. url: videoAddr,
  322. width: 589,
  323. height: 330,
  324. isLive: true,
  325. autoplay: true,
  326. autoplayMuted: true,
  327. plugins: [HlsPlugin], // 第二步
  328. poster: '/src/assets/images/vent/noSinge.png',
  329. hls: {
  330. retryCount: 3, // 重试 3 次,默认值
  331. retryDelay: 1000, // 每次重试间隔 1 秒,默认值
  332. loadTimeout: 10000, // 请求超时时间为 10 秒,默认值
  333. fetchOptions: {
  334. // 该参数会透传给 fetch,默认值为 undefined
  335. mode: 'cors'
  336. },
  337. targetLatency: 10, // 直播目标延迟,默认 10 秒
  338. maxLatency: 20, // 直播允许的最大延迟,默认 20 秒
  339. disconnectTime: 10, // 直播断流时间,默认 0 秒,(独立使用时等于 maxLatency)
  340. maxJumpDistance: 10,
  341. }
  342. })
  343. }
  344. playerList.value.push(player)
  345. }
  346. }
  347. function goFullScreen(domId) {
  348. const videoDom = document.getElementById(domId) as HTMLVideoElement
  349. if (videoDom.requestFullscreen) {
  350. videoDom.requestFullscreen()
  351. videoDom.play()
  352. } else if (videoDom.mozRequestFullscreen) {
  353. videoDom.mozRequestFullscreen()
  354. videoDom.play()
  355. } else if (videoDom.webkitRequestFullscreen) {
  356. videoDom.webkitRequestFullscreen()
  357. videoDom.play()
  358. } else if (videoDom.msRequestFullscreen) {
  359. videoDom.msRequestFullscreen()
  360. videoDom.play()
  361. }
  362. }
  363. function clearCamera() {
  364. const num = webRtcServerList.length
  365. for (let i = 0; i < num; i++) {
  366. webRtcServerList[i].disconnect()
  367. webRtcServerList[i] = null
  368. }
  369. for (let i = 0; i < playerList.value.length; i++) {
  370. const player = playerList.value[i]
  371. if (player.destroy) player.destroy()
  372. }
  373. playerList.value = []
  374. }
  375. onMounted(async () => {
  376. await getVideoAddrs()
  377. // getTreeList()
  378. getCameraDevKindList()
  379. getVideo()
  380. })
  381. onUnmounted(() => {
  382. clearCamera()
  383. })
  384. </script>
  385. <style lang="less">
  386. .camera-container {
  387. position: relative;
  388. width: calc(100% - 30px);
  389. height: calc(100% - 84px);
  390. display: flex;
  391. margin: 15px;
  392. justify-content: space-between;
  393. align-items: center;
  394. .left-area {
  395. width: 15%;
  396. height: 100%;
  397. padding: 20px;
  398. border: 1px solid #99e8ff66;
  399. background: #27546e1a;
  400. box-shadow: 0px 0px 20px 7px rgba(145, 233, 254, 0.7) inset;
  401. -moz-box-shadow: 0px 0px 20px 7px rgba(145, 233, 254, 0.7) inset;
  402. -webkit-box-shadow: 0px 0px 50px 1px rgb(149 235 255 / 5%) inset;
  403. box-sizing: border-box;
  404. // lxh
  405. .iconfont {
  406. color: #fff;
  407. font-size: 12px;
  408. margin-left: 5px;
  409. }
  410. }
  411. .right-area {
  412. width: 85%;
  413. height: 100%;
  414. padding: 0px 0px 0px 15px;
  415. box-sizing: border-box;
  416. .camera-box {
  417. width: 100%;
  418. height: calc(100% - 60px);
  419. display: flex;
  420. justify-content: space-around;
  421. align-items: flex-start;
  422. flex-wrap: wrap;
  423. overflow-y: auto;
  424. }
  425. .camera-box1 {
  426. width: 100%;
  427. height: calc(100% - 60px);
  428. display: flex;
  429. justify-content: flex-start;
  430. align-items: flex-start;
  431. flex-wrap: wrap;
  432. overflow-y: auto;
  433. }
  434. .player-box {
  435. width: 626px;
  436. height: 370px;
  437. padding: 17px 18px;
  438. background: url('/@/assets/images/vent/camera_bg.png');
  439. background-size: 100% 100%;
  440. position: relative;
  441. margin: 10px;
  442. .player-name {
  443. font-size: 14px;
  444. position: absolute;
  445. top: 15px;
  446. right: 15px;
  447. color: #fff;
  448. background-color: hsla(0, 0%, 50%, .5);
  449. border-radius: 2px;
  450. padding: 1px 5px;
  451. max-width: 120px;
  452. overflow: hidden;
  453. white-space: nowrap;
  454. text-overflow: ellipsis;
  455. z-index: 999;
  456. }
  457. .click-box {
  458. position: absolute;
  459. width: 100%;
  460. height: 100%;
  461. top: 0;
  462. left: 0;
  463. }
  464. }
  465. .pagination {
  466. width: 100%;
  467. height: 60px;
  468. display: flex;
  469. justify-content: center;
  470. align-items: center;
  471. }
  472. }
  473. }
  474. :deep(video) {
  475. width: 100% !important;
  476. height: 100% !important;
  477. object-fit: cover !important;
  478. }
  479. </style>