webClient_initWnd.js 22 KB

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