hkCamara.vue 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  1. <template>
  2. <div id="divPlugin" class="video-player">
  3. <div id="divPlugin1" class="divPlugin"></div>
  4. <!-- <div id="divPlugin" class="divPlugin" ref="divPlugin" v-if="plugin">
  5. </div> -->
  6. <!-- <div class="down" v-else>
  7. <a href="http://jbfsys.oss-cn-beijing.aliyuncs.com/download/VideoWebPlugin-1564128302790.exe">下载</a>
  8. </div> -->
  9. </div>
  10. </template>
  11. <script>
  12. import { resolve } from "eslint-plugin-promise/rules/lib/promise-statics";
  13. export default {
  14. name: "videoPlayer1",
  15. props: {
  16. //从父组件传来的数据,IP、用户名、密码、端口号
  17. companyVideoData: {
  18. type: Array,
  19. },
  20. //分屏
  21. videoType: Number,
  22. },
  23. data() {
  24. return {
  25. plugin: true,
  26. zoom: window.devicePixelRatio,
  27. iWidth: "100%",
  28. iHeight: "100%",
  29. webVideo: {},
  30. iStreamType: 1,
  31. iProtocol: 1,
  32. bZeroChannel: false,
  33. xmlDocData: {},
  34. g_iWndIndex: 0, //可以不用设置这个变量,有窗口参数的接口中,不用传值,开发包会默认使用当前选择窗口
  35. videoNum: "",
  36. wndNum: 1
  37. };
  38. },
  39. created() {},
  40. beforeUnmount() {
  41. window.removeEventListener("resize", this.handleResize);
  42. },
  43. mounted() {
  44. //浏览器界面大小
  45. // console.log("1: ",document.body.clientWidth ,document.body.clientHeight)
  46. // console.log("2: ",window.screen.height , window.devicePixelRatio,window.screen.height * window.devicePixelRatio)
  47. // console.log("3: ",window.screen.width , window.devicePixelRatio,window.screen.width * window.devicePixelRatio)
  48. this.videoInitPlugin();
  49. },
  50. destroyed() {
  51. this.$router.go(0);
  52. },
  53. methods: {
  54. handleResize() {
  55. this.zoom = window.devicePixelRatio;
  56. let h
  57. let w
  58. if(document.body.clientWidth>=(document.body.clientHeight*2)){
  59. h=document.body.clientHeight
  60. w=document.body.clientHeight*(2200/1080)
  61. }else{
  62. h=document.body.clientWidth/(2200/1080)
  63. w=document.body.clientWidth
  64. }
  65. setTimeout(() => {
  66. WebVideoCtrl.I_Resize(
  67. w/2.43,
  68. h/3.05
  69. );
  70. }, 1000);
  71. },
  72. videoInitPlugin() {
  73. var iRet = WebVideoCtrl.I_CheckPluginInstall();
  74. if (iRet === -1) {
  75. alert("您还未安装过插件");
  76. return;
  77. }
  78. this.initPlugin();
  79. },
  80. //插件初始化
  81. initPlugin() {
  82. var that = this;
  83. console.log("🚀 开始初始化插件 >> ", that.g_iWndIndex);
  84. WebVideoCtrl.I_InitPlugin({
  85. bWndFull: true,
  86. iWndowType: 1,
  87. bDebugMode: true,
  88. cbSelWnd: function (xmlDoc) {
  89. that.g_iWndIndex = parseInt($(xmlDoc).find("SelectWnd").eq(0).text(), 10);
  90. },
  91. });
  92. console.log("🚀 初始化插件完成 >> ", that.g_iWndIndex);
  93. console.log("🚀 插入插件");
  94. let h
  95. let w
  96. // console.log("0: ",document.body.clientWidth ,document.body.clientHeight)
  97. if(document.body.clientWidth>=(document.body.clientHeight*2)){
  98. h=document.body.clientHeight
  99. w=document.body.clientHeight*(2200/1080)
  100. }else{
  101. h=document.body.clientWidth/(2200/1080)
  102. w=document.body.clientWidth
  103. }
  104. // console.log("1: ",w ,h)
  105. // console.log("1: ", w/2.43, h/3.05)
  106. WebVideoCtrl.I_InsertOBJECTPlugin("divPlugin1").then(() => {
  107. WebVideoCtrl.I_Resize(
  108. w/2.43,
  109. h/3.05
  110. ).then(() => {
  111. console.log("🚀 data >> ", that.companyVideoData);
  112. this.getLoginParams().then((res) => {
  113. console.log("🚀 解析完 >> ", res);
  114. this.login(res);
  115. }).then(() => {
  116. this.$emit("init", true);
  117. });
  118. })
  119. window.addEventListener("resize", that.handleResize);
  120. });
  121. console.log("🚀 插入完成");
  122. },
  123. async getLoginParams() {
  124. var loginParams = {};
  125. var that = this;
  126. for (const item of that.companyVideoData) {
  127. if (item !== undefined) {
  128. let loginParam = {}
  129. const key = JSON.stringify(item);
  130. if (!loginParams.hasOwnProperty(key)) {
  131. loginParams[key] = item;
  132. }
  133. }
  134. }
  135. return loginParams;
  136. },
  137. async login(loginParams) {
  138. var that = this;
  139. // for (const loginParam of Object.values(loginParams)) {
  140. // await that.doLogin(loginParam)
  141. // }
  142. await Promise.all(
  143. Object.values(loginParams).map(async (loginParam) => {
  144. await that.doLogin(loginParam);
  145. })
  146. )
  147. },
  148. async doLogin(loginParam) {
  149. console.log("🚀 loginPara >> ", loginParam);
  150. var that = this;
  151. WebVideoCtrl.I_Login(
  152. loginParam.ip,
  153. 1,
  154. loginParam.Port,
  155. loginParam.Username,
  156. loginParam.Password,
  157. {
  158. timeout:1000,
  159. success: function(xmlDoc) {
  160. console.log("🚀 登录成功 >> ", loginParam.ip, loginParam.Port);
  161. that.doGetChannelInfo(loginParam.ip).then((channels) => {
  162. var s = JSON.stringify(channels);
  163. console.log("tezt", JSON.stringify(channels), channels.length, channels[0], channels[0].channelId, channels[0]["channelId"])
  164. // console.log("🚀 通道列表 >> ", loginParam.ip, channels, channels.length, s[0]["channelName"]);
  165. var channelId = that.getChannelIdByName(loginParam.channelName, channels);
  166. console.log("🚀 获得id >> ", channelId);
  167. if (channelId !== null) {
  168. console.log("🚀 获得id >> ", channelId);
  169. that.startPlay(loginParam.ip, channelId);
  170. } else
  171. console.log("🚀 通道号无效 >> ", loginParam.ip, loginParam.channelName);
  172. })
  173. },
  174. error: function () {
  175. console.log("login error");
  176. },
  177. }
  178. );
  179. },
  180. getChannelIdByName(channelName, channels) {
  181. const channel = channels.find((channel) => {
  182. if (channel["channelName"] == channelName) {
  183. return channel;
  184. }
  185. });
  186. if (channel) {
  187. return channel.channelId;
  188. } else {
  189. return null;
  190. }
  191. },
  192. doGetChannelInfo(ip) {
  193. return new Promise((resolve, reject) => {
  194. var getAnalogChannelInfo = new Promise((resolve, reject) => {
  195. WebVideoCtrl.I_GetAnalogChannelInfo(ip, {
  196. success: function(xmlDoc) {
  197. let channels = [];
  198. var oChannels = $(xmlDoc).find("VideoInputChannel");
  199. $.each(oChannels, function(i) {
  200. var id = $(this).find("id").eq(0).text(),
  201. name = $(this).find("name").eq(0).text();
  202. if ("" == name) {
  203. name = "Camera " + (i < 9 ? "0" + (i + 1) : (i + 1));
  204. }
  205. channels.push({
  206. channelId: id,
  207. channelName: name,
  208. })
  209. });
  210. console.log("🚀 获取模拟通道成功 >> ", channels);
  211. resolve(channels);
  212. },
  213. error: function(oError) {
  214. console.log("🚀 获取模拟通道失败 >> ", ip, oError.errorCode, oError.errorMsg);
  215. },
  216. })
  217. });
  218. var getDigitalChannelInfo = new Promise((resolve, reject) => {
  219. WebVideoCtrl.I_GetDigitalChannelInfo(ip, {
  220. success: function(xmlDoc) {
  221. let channels = [];
  222. var oChannels = $(xmlDoc).find("InputProxyChannelStatus");
  223. $.each(oChannels, function(i) {
  224. var id = $(this).find("id").eq(0).text(),
  225. name = $(this).find("name").eq(0).text(),
  226. online = $(this).find("online").eq(0).text();
  227. if ("false" == online) {// 过滤禁用的数字通道
  228. return true;
  229. }
  230. if ("" == name) {
  231. name = "IPCamera " + (i < 9 ? "0" + (i + 1) : (i + 1));
  232. }
  233. channels.push({
  234. channelId: id,
  235. channelName: name,
  236. })
  237. });
  238. console.log("🚀 获取数字通道成功 >> ", channels);
  239. resolve(channels);
  240. },
  241. error: function(oError) {
  242. console.log("🚀 获取数字通道失败 >> ", ip, oError.errorCode, oError.errorMsg);
  243. resolve([]);
  244. }
  245. });
  246. });
  247. var getZeroChannelInfo = new Promise((resolve, reject) => {
  248. WebVideoCtrl.I_GetZeroChannelInfo(ip, {
  249. success: function(xmlDoc) {
  250. let channels = [];
  251. var oChannels = $(xmlDoc).find("ZeroVideoChannel");
  252. $.each(oChannels, function(i) {
  253. var id = $(this).find("id").eq(0).text(),
  254. name = $(this).find("name").eq(0).text();
  255. if ("" == name) {
  256. name = "Zero Channel " + (i < 9 ? "0" + (i + 1) : (i + 1));
  257. }
  258. if ("true" == $(this).find("enabled").eq(0).text()) {// 过滤禁用的零通道
  259. channels.push({
  260. channelId: id,
  261. channelName: name,
  262. })
  263. }
  264. });
  265. console.log("🚀 获取零通道成功 >> ", channels);
  266. resolve(channels);
  267. },
  268. error: function(oError) {
  269. console.log("🚀 获取零通道失败 >> ", ip, oError.errorCode, oError.errorMsg);
  270. resolve([]);
  271. }
  272. });
  273. });
  274. Promise.allSettled([getAnalogChannelInfo, getDigitalChannelInfo, getZeroChannelInfo])
  275. .then((resList) => {
  276. console.log("🚀 res >> ", resList);
  277. const fulfilledValues = resList.reduce((list, result) => {
  278. if (result.status === 'fulfilled' && result.value.length !== 0) {
  279. const value = result.value;
  280. list.push(value);
  281. }
  282. return list;
  283. }, []);
  284. resolve(fulfilledValues.flat());
  285. })
  286. .catch((error) => {
  287. console.log("🚀 获取通道失败 >> ", ip);
  288. });
  289. })
  290. },
  291. startPlay(ip, channelId) {
  292. console.log("🚀 开始播放 >> ", ip, channelId);
  293. WebVideoCtrl.I_StartRealPlay(ip, {
  294. iStreamType: 1,
  295. iChannelID: channelId,
  296. iWndIndex: 0,
  297. }).then(() => {
  298. this.$emit("init", true);
  299. console.log("🚀 采集卡麒麟 >> ", );
  300. })
  301. },
  302. clickLogin() {
  303. return new Promise((resolve) => {
  304. const uniqueArr = [];
  305. const uniqueSet = new Set();
  306. this.companyVideoData.forEach((obj) => {
  307. if (obj !== undefined) {
  308. const key = JSON.stringify(obj);
  309. if (!uniqueSet.has(key)) {
  310. uniqueSet.add(key);
  311. uniqueArr.push(obj);
  312. }
  313. }
  314. });
  315. console.log(uniqueArr);
  316. for (let index = 0; index < uniqueArr.length; index++) {
  317. this.singleLogin(uniqueArr[index], index);
  318. }
  319. // 完成登录操作后调用resolve
  320. resolve();
  321. });
  322. },
  323. singleLogin(Data, index) {
  324. setTimeout(() => {
  325. var that = this;
  326. WebVideoCtrl.I_Login(
  327. Data.ip,
  328. 1,
  329. Data.Port,
  330. Data.Username,
  331. Data.Password,
  332. {
  333. timeout: 1000,
  334. async: false,
  335. success: function (xmlDoc) {
  336. console.log("开始预览"); //不能删除
  337. let oChannels = that.getChannelInfo();
  338. that.initPlay(Data, index,oChannels);
  339. },
  340. error: function () {
  341. console.log("login error");
  342. },
  343. }
  344. );
  345. }, 100);
  346. },
  347. //获取通道
  348. getChannelInfo() {
  349. var that = this;
  350. // 模拟通道
  351. WebVideoCtrl.I_GetAnalogChannelInfo(this.szIP, {
  352. async: false,
  353. success: function (xmlDoc) {
  354. var oChannels = $(xmlDoc).find("VideoInputChannel");
  355. nAnalogChannel = oChannels.length;
  356. console.log("获取模拟通道成功!");
  357. return oChannels
  358. },
  359. error: function () {
  360. console.log("获取模拟通道失败!" + WebVideoCtrl.I_GetLastError());
  361. },
  362. });
  363. // 数字通道
  364. WebVideoCtrl.I_GetDigitalChannelInfo(that.companyVideoData.ip, {
  365. async: false,
  366. success: function (xmlDoc) {
  367. var oChannels = $(xmlDoc).find("InputProxyChannelStatus");
  368. return oChannels
  369. },
  370. error: function () {
  371. WebVideoCtrl.I_GetLastError();
  372. console.log("获取数字通道失败!" + WebVideoCtrl.I_GetLastError());
  373. },
  374. });
  375. // 零通道
  376. WebVideoCtrl.I_GetZeroChannelInfo(this.szIP, {
  377. async: false,
  378. success: function (xmlDoc) {
  379. var oChannels = $(xmlDoc).find("ZeroVideoChannel");
  380. return oChannels
  381. },
  382. error: function () {
  383. console.log("获取零通道失败!" + WebVideoCtrl.I_GetLastError());
  384. },
  385. });
  386. },
  387. //初始化视频,为了让用户进来就可以看到视频播放
  388. initPlay(data, index,oChannels) {
  389. let szIP = data.ip; //ip地址
  390. //循环16次是因为插件分屏最大为4x4(可以根据情况而定)
  391. WebVideoCtrl.I_StartRealPlay(szIP, {
  392. iStreamType: 1,
  393. iChannelID: oChannels.id,//按格式修改
  394. iWndIndex: index,
  395. });
  396. },
  397. // 点击查看具体哪个监控
  398. startRealPlay(oChannels) {
  399. let that = this;
  400. let szIP = this.companyVideoData.ip; //ip地址
  401. let iChannelID = oChannels; //播放通道号
  402. var iStreamType = 1;
  403. var oWndInfo = WebVideoCtrl.I_GetWindowStatus(that.g_iWndIndex);
  404. if (oWndInfo != null) {
  405. // 已经在播放了,先停止
  406. WebVideoCtrl.I_Stop();
  407. }
  408. WebVideoCtrl.I_StartRealPlay(szIP, {
  409. iStreamType: iStreamType,
  410. iChannelID: iChannelID,
  411. });
  412. },
  413. },
  414. //因为项目中需求,总公司要查看所有子公司的监控,所以做了个监听来切换查看各个子公司的监控
  415. watch: {
  416. companyVideoData: {
  417. handler(val, oldVal) {
  418. if (oldVal.length !== 0) {
  419. console.log("🚀 oldVal >> ", oldVal);
  420. for (const item of oldVal) {
  421. if (item !== undefined) {
  422. console.log("🚀 退出登录 >> ", item.ip);
  423. WebVideoCtrl.I_Logout(item.ip)
  424. }
  425. }
  426. for (let i = 0; i < this.wndNum; i++) {
  427. WebVideoCtrl.I_Stop(i);
  428. console.log("🚀 关闭 >> ", i);
  429. }
  430. this.getLoginParams().then((res) => {
  431. console.log("🚀 解析完 >> ", res);
  432. this.login(res);
  433. });
  434. // WebVideoCtrl.I_StopAllPlay().then(() => {
  435. // console.log("🚀 监控全关 >> ", );
  436. // this.getLoginParams().then((res) => {
  437. // console.log("🚀 解析完 >> ", res);
  438. // this.login(res);
  439. // });
  440. // })
  441. // for (let i = 0; i < this.wndNum; i++) {
  442. // WebVideoCtrl.I_Stop(i);
  443. // console.log("🚀 关闭 >> ", i);
  444. // }
  445. // this.getLoginParams().then((res) => {
  446. // console.log("🚀 解析完 >> ", res);
  447. // this.login(res);
  448. // });
  449. //
  450. }
  451. }
  452. }
  453. }
  454. // watch: {
  455. // companyVideoData: {
  456. // handler(val, oldval) {
  457. // if (oldval) {
  458. // for (let i = 0; i < 16; i++) {
  459. // WebVideoCtrl.I_Stop(i); //停止正在播放的页面
  460. // }
  461. // this.$emit("func", "");
  462. // WebVideoCtrl.I_Logout(oldval.ip);
  463. // this.clickLogin(this.companyVideoData);
  464. // setTimeout(() => {
  465. // console.log("🚀 ~ file:hkCamara method: line:92 -----");
  466. // this.$emit("init", true);
  467. // }, 2000);
  468. // }
  469. // },
  470. // immediate: true, //关键
  471. // deep: true,
  472. // },
  473. // videoType: {
  474. // handler(val, oldval) {
  475. // if (oldval) {
  476. // WebVideoCtrl.I_ChangeWndNum(this.videoType); //分屏
  477. // this.initPlay();
  478. // }
  479. // },
  480. // immediate: true, //关键
  481. // deep: true,
  482. // },
  483. // },
  484. };
  485. </script>
  486. <style scoped>
  487. .video-player {
  488. width: 120%;
  489. height: 120%;
  490. }
  491. .divPlugin {
  492. width: 100%;
  493. height: 100%;
  494. color: red;
  495. /* display: flex; */
  496. justify-content: center;
  497. align-items: center;
  498. font-size: 18px;
  499. position: fixed;
  500. /* top: 680px; */
  501. /* top: 0px; */
  502. /* right: 0px; */
  503. overflow: hidden;
  504. }
  505. .down {
  506. width: 100%;
  507. height: 100%;
  508. color: red;
  509. display: flex;
  510. justify-content: center;
  511. align-items: center;
  512. font-size: 18px;
  513. }
  514. .down a {
  515. color: red;
  516. }
  517. </style>