webClient_initWnd.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580
  1. class VideoPlay {
  2. constructor(spaceId, websocket, socketCode, vModeObj = {}, options) {
  3. // spaceId,socketCode不能相同,vModeObj播放模式,是否轮播
  4. this.handleName = spaceId;
  5. this.socketCode = socketCode;
  6. this.vModeObj = vModeObj;
  7. this.toolL = null; // 浏览器左侧、下方、右侧条宽度
  8. this.toolH = null; // 浏览器上方的高度,放大缩小时会变化
  9. this.fullToolL = null; // 最大化时浏览器左侧、下方、右侧条宽度
  10. this.fullToolH = null; // 最大化时浏览器上方高度
  11. this.space = document.querySelector(spaceId); // 传入的存放视频区域id或者class
  12. this.spaceStartOffsetLeft = document.querySelector(spaceId).offsetLeft; // 传入的存放视频区域距离浏览器左侧的距离,iframe另计算;
  13. this.spaceStartOffsetTop = document.querySelector(spaceId).offsetTop; // //传入的存放视频区域距离浏览器顶部的距离,iframe另计算;
  14. this.space = document.querySelector(spaceId);
  15. this.websocket = websocket; // 初始化websocket
  16. this.display = true; // 防止切换页面视频闪烁
  17. this.iframetoWinX = 0; // iframe距离浏览器左边的距离
  18. this.iframetoWinY = 0; // iframe距离浏览器顶部的距离
  19. this.width = 0; // 实时存放视频区域宽度
  20. this.height = 0; // 实存放视频区域高度
  21. this.posX = 0; // 实时存放视频区域距离浏览器左边的距离
  22. this.posY = 0; // 实时存放视频区域距离浏览器顶部的距离
  23. this.scale = false; // 是否开始伸缩网页
  24. this.defaults = {
  25. // 默认配置,可以通过option覆盖
  26. protocol: defaultConfig.protocol, // 协议
  27. letLoginIp: defaultConfig.loginIp, // 环境配置
  28. letPort: defaultConfig.port, // 端口配置
  29. letUserCode: localStorage.getItem("userId"), // 用户code
  30. wsUri: "ws://localhost:1234",
  31. token: localStorage.getItem("DAHUA_token"), // token
  32. getVersion: null, // option里面的获取版本信息的回调函数通知
  33. loginSuccess: null, // 登陆成功会调通知
  34. errorInfo: null, // 异常回调通知
  35. isDefaultShow: true, // 是否默认显示视频
  36. onCreateVideoSuccess: false // 客户端被拉起通知
  37. };
  38. this.startCoinRegionPOX = 0; // 默认menu菜单距离浏览器左边的距离
  39. this.startCoinRegionPOY = 0; // 默认menu菜单距离浏览器顶部的距离
  40. this.zoom = 1; // 默认伸缩;实时存放网页伸缩倍数
  41. this.ifmToTop = 0; // iframe距离区域的距离
  42. this.menuWidth1 = 0; // 实时菜单宽度
  43. this.menuHeight1 = 0; // 实时菜单高度
  44. this.settings = Object.assign({}, this.defaults, options); // 覆盖默认配置操作
  45. this.websocketOnerror = false;
  46. this.ServerVersion = 8888888; // 模拟服务器版本号
  47. this.ClientLocalVersion = 0; // 获取本地版本号
  48. this._toolHeight = 0; // 顶部高度
  49. this.status = true; // 记录显示隐藏
  50. this.status = document.querySelector(spaceId).clientHeight !== 0;
  51. }
  52. // 初始化
  53. init() {
  54. this.calcTools();
  55. this.initClientPanel();
  56. this.winMaxMinOpt();
  57. window.addEventListener("resize", () => {
  58. this.resize();
  59. });
  60. window.onunload = () => {
  61. this.closeClient();
  62. };
  63. }
  64. // webSocket交互
  65. webSocketSend(data) {
  66. if (this.websocket == null) {
  67. return;
  68. }
  69. if (this.websocket.readyState === 1) {
  70. this.websocket.send(data);
  71. } else {
  72. setTimeout(() => {
  73. this.webSocketSend(data);
  74. }, 1000);
  75. }
  76. }
  77. // 检测浏览器的缩放状态实现代码
  78. detectZoom() {
  79. var ratio = 0;
  80. var screen = window.screen;
  81. var ua = navigator.userAgent.toLowerCase();
  82. if (window.devicePixelRatio !== undefined) {
  83. ratio = window.devicePixelRatio;
  84. } else if (~ua.indexOf("msie")) {
  85. if (screen.deviceXDPI && screen.logicalXDPI) {
  86. ratio = screen.deviceXDPI / screen.logicalXDPI;
  87. }
  88. } else if (
  89. window.outerWidth !== undefined &&
  90. window.innerWidth !== undefined
  91. ) {
  92. ratio = window.outerWidth / window.innerWidth;
  93. }
  94. if (ratio) {
  95. ratio = Math.round(ratio * 100);
  96. }
  97. return ratio;
  98. }
  99. // 实时传递给客户端的位置大小
  100. dragResize(width, height, posX, posY) {
  101. // 实时存储目前视频的大小位置;
  102. this.width = width;
  103. this.height = height;
  104. this.posX = posX;
  105. this.posY = posY;
  106. const params = {
  107. loginIp: this.settings.letLoginIp,
  108. method: "adjustSizePosition",
  109. params: {
  110. width: width,
  111. height: height,
  112. posX: this.posX,
  113. posY: this.posY,
  114. handleName: this.handleName
  115. },
  116. userCode: this.settings.letUserCode,
  117. socketCode: this.socketCode
  118. };
  119. this.webSocketSend(JSON.stringify(params));
  120. }
  121. // 设置菜单遮挡问题
  122. setCoinRegion(flag, dorpDownIdArr) {
  123. const isEmbed = flag;
  124. let menu;
  125. let menuLeftX = 0;
  126. let menuLeftY = 0;
  127. const arr = [];
  128. this.zoom = this.detectZoom() / 100; // 910add
  129. if (flag === true) {
  130. // const obj = {};
  131. dorpDownIdArr.map((item, index) => {
  132. arr.push({});
  133. // obj[index] = document.querySelector(v);
  134. menu = document.querySelector(item);
  135. // 菜单宽高
  136. arr[index]["width"] = Math.round(menu.clientWidth * this.zoom);
  137. arr[index]["height"] = Math.round(
  138. menu.clientHeight * this.zoom
  139. );
  140. // 菜单左上角相对浏览器左上角的坐标
  141. // 菜单左上角相对浏览器左上角的坐标
  142. menuLeftX = Math.round(
  143. menu.getBoundingClientRect().left * this.zoom - 2
  144. );
  145. menuLeftY = Math.round(
  146. menu.getBoundingClientRect().top * this.zoom +
  147. this._toolHeight
  148. );
  149. arr[index]["posX"] = Math.round(
  150. menuLeftX + this.iframetoWinX * this.zoom
  151. );
  152. arr[index]["posY"] = Math.round(
  153. menuLeftY + this.iframetoWinY * this.zoom
  154. );
  155. });
  156. /* Array.from(arguments).splice(1, Array.from(arguments).length - 1).forEach((v, index) => {
  157. arr.push({});
  158. obj[index] = document.querySelector(v);
  159. menu = document.querySelector(dorpDownId);
  160. // 菜单宽高
  161. arr[index]["width"] = Math.round(obj[index].clientWidth * _this.zoom);
  162. arr[index]["height"] = Math.round(obj[index].clientHeight * _this.zoom);
  163. // 菜单左上角相对浏览器左上角的坐标
  164. menuLeftX = Math.round(obj[index].getBoundingClientRect().left* _this.zoom - 2);
  165. menuLeftY = Math.round(obj[index].getBoundingClientRect().top* _this.zoom + _this._toolHeight);
  166. arr[index]["posX"] = Math.round(menuLeftX + _this.iframetoWinX * _this.zoom);
  167. if (_this.zoom == 1) { //正常比例
  168. arr[index]["posY"] = Math.round(menuLeftY + _this.iframetoWinY * _this.zoom);
  169. } else { //缩放比例
  170. if (menu) {
  171. arr[index]["posY"] = Math.round(obj[index].getBoundingClientRect().top * _this.zoom + _this.ifmToTop * _this.zoom +_this._toolHeight);
  172. //console.log(_this.startCoinRegionPOY,menu.getBoundingClientRect().top*_this.zoom,_this.ifmToTop,Math.round(window.parent.outerHeight - (window.parent.innerHeight)*_this.zoom),_this.zoom);
  173. }
  174. }
  175. }) */
  176. }
  177. // console.log(arr);
  178. const params = {
  179. loginIp: this.settings.letLoginIp,
  180. method: "setCoinRegion",
  181. params: {
  182. bCoincide: isEmbed,
  183. region: arr,
  184. handleName: this.handleName
  185. },
  186. userCode: this.settings.letUserCode,
  187. socketCode: this.socketCode
  188. };
  189. this.webSocketSend(JSON.stringify(params));
  190. }
  191. // 计算顶部距离,网页伸缩不变,这个针对不同分辨率的电脑,如果不准,第三方自己修改,只提供如下几种适配电脑。
  192. calcTools() {
  193. // const toolh = Math.round(window.outerHeight - (window.innerHeight) * this.detectZoom() / 100);
  194. // this._toolHeight = toolh < 0 ? 80 : toolh;
  195. // this.zoom = this.detectZoom() / 100;
  196. this.zoom = this.detectZoom() / 100;
  197. // console.log(this.zoom);
  198. if (this.zoom === 0.67) {
  199. // 现场笔记本
  200. this._toolHeight = 80;
  201. } else if (this.zoom === 1.5) {
  202. this._toolHeight = 110;
  203. } else if (this.zoom === 3) {
  204. this._toolHeight = 232;
  205. } else {
  206. const toolh = Math.round(
  207. window.outerHeight -
  208. (window.innerHeight * this.detectZoom()) / 100
  209. );
  210. this._toolHeight = toolh < 0 ? 90 : toolh;
  211. }
  212. }
  213. // 网页变操作化大小
  214. resize() {
  215. // console.log(33333);
  216. console.log(this.status);
  217. console.log(this.vModeObj.embedVideoMode);
  218. // if (!this.status || !this.vModeObj.embedVideoMode) {
  219. // console.log('false');
  220. // return false;
  221. // }
  222. const calcDomWidth =
  223. this.space.clientWidth || this.space.getBoundingClientRect().width;
  224. const calcDomheight =
  225. this.space.clientHeight ||
  226. this.space.getBoundingClientRect().height;
  227. const zoom = this.detectZoom() / 100;
  228. const realClientWidth = Math.round(calcDomWidth * zoom);
  229. const realClientHeight = Math.round(calcDomheight * zoom);
  230. console.log("zoom---_toolHeight---", zoom, this._toolHeight);
  231. // 防止初始化后设置隐藏hide后拖动浏览器再show显示在左上方问题
  232. if (this.space.clientWidth === 0 && this.space.clientHeight === 0) {
  233. return;
  234. }
  235. if (zoom === 1) {
  236. // 默认伸缩状态
  237. // posX:距离浏览器左顶点横轴距离;posY:距离浏览器左顶点纵轴距离
  238. let posX = this.space.getBoundingClientRect().left;
  239. let posY;
  240. if (window.outerWidth === window.innerWidth) {
  241. // 普通浏览器最大化状态
  242. if (this.fullToolH == null) {
  243. // 为空时才计算,避免上下拖动改变浏览器大小事,视频窗口抖动
  244. this.fullToolL =
  245. (window.outerWidth - window.innerWidth) / 2;
  246. // _this.fullToolH = window.outerHeight - window.innerHeight - _this.fullToolL + 10;
  247. this.fullToolH = this._toolHeight;
  248. }
  249. posY = this.space.getBoundingClientRect().top + this.fullToolH;
  250. } else if (window.outerWidth - window.innerWidth < 0) {
  251. // F11全屏窗口计算
  252. posX = this.space.getBoundingClientRect().left;
  253. posY = this.space.getBoundingClientRect().top;
  254. } else {
  255. // 浏览器非最大化状态
  256. if (this.toolH == null) {
  257. this.toolL = (window.outerWidth - window.innerWidth) / 2;
  258. this.toolH =
  259. window.outerHeight - window.innerHeight - this.toolL;
  260. }
  261. posX = this.space.getBoundingClientRect().left;
  262. posY = this.space.getBoundingClientRect().top + this.toolH + 0;
  263. }
  264. posX = Math.round(posX);
  265. posY = Math.round(posY);
  266. this.dragResize(realClientWidth, realClientHeight, posX, posY);
  267. console.log(
  268. "dragResize1-->",
  269. realClientWidth,
  270. realClientHeight,
  271. posX,
  272. posY
  273. );
  274. } else {
  275. if (
  276. window.outerHeight - window.innerHeight === 288 ||
  277. window.outerWidth - window.innerWidth < 0
  278. ) {
  279. // 最大化
  280. const longW = Math.round(
  281. this.space.getBoundingClientRect().left * zoom
  282. );
  283. const longH = Math.round(
  284. this.space.getBoundingClientRect().top * zoom
  285. );
  286. this.dragResize(
  287. realClientWidth,
  288. realClientHeight,
  289. longW,
  290. longH
  291. );
  292. console.log(
  293. "最大化dragResize2-->",
  294. realClientWidth,
  295. realClientHeight,
  296. longW,
  297. longH
  298. );
  299. } else {
  300. const longW = Math.round(
  301. this.space.getBoundingClientRect().left * zoom
  302. );
  303. const longH = Math.round(
  304. this.space.getBoundingClientRect().top * zoom +
  305. this._toolHeight
  306. );
  307. this.dragResize(
  308. realClientWidth,
  309. realClientHeight,
  310. longW,
  311. longH
  312. );
  313. console.log(
  314. "dragResize2-->",
  315. realClientWidth,
  316. realClientHeight,
  317. longW,
  318. longH
  319. );
  320. }
  321. }
  322. }
  323. // 创建视频窗口
  324. createVideoDialog(obj) {
  325. console.log(obj);
  326. const {
  327. rows = 2,
  328. cols = 2,
  329. wndSpaceing = 0,
  330. embedVideoMode = true,
  331. playerCtrlBarEnable = false,
  332. displayMode = 0,
  333. playMode = 0,
  334. playParams = {},
  335. webControlExpend = false
  336. } = obj;
  337. let playParamsObj = null;
  338. playMode === 0 ? (playParamsObj = {}) : (playParamsObj = playParams);
  339. const params = JSON.stringify({
  340. loginIp: this.settings.letLoginIp,
  341. userCode: this.settings.letUserCode,
  342. socketCode: this.socketCode,
  343. method: "CreateVideoDialog",
  344. params: {
  345. handleName: this.handleName,
  346. rows: rows,
  347. cols: cols,
  348. wndSpaceing: wndSpaceing, // 视频播放窗口间隔,默认为0
  349. embedVideoMode: embedVideoMode, // 视频窗口是否为嵌入式。true:嵌入式(列表框云台和标题栏不展示),false:弹出式(展示列表框云台和标题栏)
  350. playerCtrlBarEnable: playerCtrlBarEnable, // 视频窗口上的鼠标云台是否使能。true:展示,false:不展示
  351. displayMode: displayMode, // 窗口显示模式,用于播放控制栏0:视频监控,显示播放控制栏; 1:轮播,隐藏播放控制栏
  352. playMode: playMode, // playMode:播放模式,0: 普通模式,1:轮播模式,默认为普通播放模式
  353. playParams: playParamsObj, // playMode=0,playParams为空;playMode=1,playParams格式如下:// 轮播时间间隔,单位秒,默认为5s
  354. webControlExpend: webControlExpend
  355. }
  356. });
  357. console.log(params);
  358. this.webSocketSend(params);
  359. }
  360. //初始化窗口
  361. initClientPanel() {
  362. this.createVideoDialog(this.vModeObj);
  363. }
  364. // 实时预览视频
  365. realTimeVideo(
  366. arr = [
  367. {
  368. channelId: ""
  369. }
  370. ]
  371. ) {
  372. const params = JSON.stringify({
  373. loginIp: this.settings.letLoginIp,
  374. method: "openVideo",
  375. params: {
  376. array: arr, // [{"channelId": channelId}]
  377. handleName: this.handleName
  378. },
  379. userCode: this.settings.letUserCode,
  380. socketCode: this.socketCode
  381. });
  382. this.webSocketSend(params);
  383. this.winMaxMinOpt();
  384. }
  385. // 录像回放 //paramsArr传开始时间,结束时间,通道号
  386. playBack(paramsArr) {
  387. this.resize();
  388. const params = JSON.stringify({
  389. loginIp: this.settings.letLoginIp,
  390. method: "openRecord",
  391. params: {
  392. array: paramsArr, // [{"beginTime": "2019-12-27 00:00:00","channelId": "mA2I9l1kA1BOPTAPHT74P8","endTime": "2019-12-27 23:59:59"}]
  393. handleName: this.handleName
  394. },
  395. userCode: this.settings.letUserCode,
  396. socketCode: this.socketCode
  397. });
  398. this.webSocketSend(params);
  399. this.winMaxMinOpt();
  400. setTimeout(() => {
  401. this.resize();
  402. }, 1000);
  403. }
  404. // 关闭客户端
  405. closeClient() {
  406. const params = {
  407. loginIp: this.settings.letLoginIp,
  408. method: "logout",
  409. userCode: this.settings.letUserCode,
  410. socketCode: this.socketCode
  411. };
  412. this.webSocketSend(JSON.stringify(params));
  413. this.stopWebSocket();
  414. this.websocket = null;
  415. }
  416. // debug(message) { // 客户端调试返回信息操作
  417. // debugTextArea.value += message + "\n\n";
  418. // debugTextArea.scrollTop = debugTextArea.scrollHeight;
  419. // }
  420. sendMessage() {
  421. const msg = document.getElementById("inputText").value;
  422. const strToSend = msg;
  423. if (this.websocket != null) {
  424. document.getElementById("inputText").value = "";
  425. this.webSocketSend(strToSend);
  426. }
  427. }
  428. stopWebSocket() {
  429. if (this.websocket) {
  430. this.websocket.close();
  431. }
  432. this.websocket = null;
  433. }
  434. // 客户端显示隐藏操作;
  435. showBrower(flag) {
  436. const params = {
  437. loginIp: this.settings.letLoginIp,
  438. method: "whetherShowVideoWnd",
  439. params: {
  440. bShow: flag,
  441. handleName: this.handleName
  442. },
  443. userCode: this.settings.letUserCode,
  444. socketCode: this.socketCode
  445. };
  446. this.display = flag;
  447. this.status = flag;
  448. console.log(flag);
  449. this.resize(); // 隐藏时候不走resize,显示时候感知下位置,但是,这不能加延时,否则切换标签页有问题。
  450. this.webSocketSend(JSON.stringify(params));
  451. }
  452. // 客户端-释放窗口
  453. destroyVideoDialog() {
  454. const params = {
  455. loginIp: this.settings.letLoginIp,
  456. method: "DestroyVideoDialog",
  457. params: {
  458. handleName: this.handleName
  459. },
  460. userCode: this.settings.letUserCode,
  461. socketCode: this.socketCode
  462. };
  463. this.webSocketSend(JSON.stringify(params));
  464. }
  465. // 视频置顶操作
  466. setVideoWndOnTop(flag) {
  467. const params = {
  468. loginIp: this.settings.letLoginIp,
  469. method: "setVideoWndOnTop",
  470. params: {
  471. bOnTop: flag,
  472. handleName: this.handleName
  473. },
  474. userCode: this.settings.letUserCode,
  475. socketCode: this.socketCode
  476. };
  477. this.webSocketSend(JSON.stringify(params));
  478. }
  479. // 最大化最小化操作;
  480. winMaxMinOpt() {
  481. if (document.addEventListener) {
  482. // debugger;
  483. document.addEventListener("webkitvisibilitychange", () => {
  484. // debugger;
  485. if (document.webkitVisibilityState === "hidden") {
  486. // 最小化
  487. const temp = this.status;
  488. this.showBrower(false);
  489. this.status = temp;
  490. console.log("hidden");
  491. } else if (document.webkitVisibilityState === "visible") {
  492. // 最大化
  493. // 延时的目的:最大化的瞬间 走resize中的window.outerWidth - window.innerWidth < 0这个条件,缺少浏览器顶部栏的高度,导致高度上移
  494. console.log("visible");
  495. setTimeout(() => {
  496. this.showBrower(this.status);
  497. }, 200);
  498. }
  499. });
  500. }
  501. }
  502. // 关闭所有视频
  503. closeAllVideo() {
  504. const params = JSON.stringify({
  505. loginIp: this.settings.letLoginIp,
  506. method: "closeVideo",
  507. userCode: this.settings.letUserCode,
  508. params: {
  509. handleName: this.handleName
  510. },
  511. socketCode: this.socketCode
  512. });
  513. this.webSocketSend(params);
  514. }
  515. // 拉app视频
  516. openAppVideo(paramsArr) {
  517. const params = JSON.stringify({
  518. loginIp: this.settings.letLoginIp,
  519. method: "openAppVideo",
  520. params: {
  521. handleName: this.handleName,
  522. array: paramsArr
  523. },
  524. userCode: this.settings.letUserCode,
  525. socketCode: this.socketCode
  526. });
  527. this.webSocketSend(params);
  528. }
  529. // 设置窗口分割数
  530. setDesignDivision(rows, cols) {
  531. const params = JSON.stringify({
  532. loginIp: this.settings.letLoginIp,
  533. method: "SetDesignDivision",
  534. userCode: this.settings.letUserCode,
  535. params: {
  536. handleName: this.handleName,
  537. rows: Number(rows),
  538. cols: Number(cols)
  539. },
  540. socketCode: this.socketCode
  541. });
  542. this.webSocketSend(params);
  543. }
  544. // 设置视频双击后位置大小
  545. setDesignDivision(obj) {
  546. const { width = "0", height = "0", posX = "0", posY = "0" } = obj;
  547. const params = JSON.stringify({
  548. loginIp: this.settings.letLoginIp,
  549. method: "expendVideoCtrl",
  550. userCode: this.settings.letUserCode,
  551. params: {
  552. handleName: this.handleName,
  553. expend: true,
  554. width: width,
  555. height: height,
  556. posX: posX,
  557. posY: posY
  558. },
  559. socketCode: this.socketCode
  560. });
  561. this.webSocketSend(params);
  562. }
  563. // 通用方法第三方传方法名和参数体
  564. commonUse(method, paramsObj) {
  565. const params = JSON.stringify({
  566. loginIp: this.settings.letLoginIp,
  567. method: method,
  568. userCode: this.settings.letUserCode,
  569. params: paramsObj,
  570. socketCode: this.socketCode
  571. });
  572. this.webSocketSend(params);
  573. }
  574. }
  575. window.VideoPlay = VideoPlay;