webClient_initWnd.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  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 =
  104. Math.round(width * window.devicePixelRatio + 5 * window.devicePixelRatio);
  105. this.height = Math.round(height * window.devicePixelRatio + 5 * window.devicePixelRatio);
  106. this.posX = Math.round(
  107. posX * window.devicePixelRatio + 5 * window.devicePixelRatio,
  108. );
  109. this.posY = Math.round(
  110. posY * window.devicePixelRatio + 5 * window.devicePixelRatio,
  111. );
  112. // this.width = width;
  113. // this.height = height;
  114. // this.posX = posX;
  115. // this.posY = posY;
  116. const params = {
  117. loginIp: this.settings.letLoginIp,
  118. method: 'adjustSizePosition',
  119. params: {
  120. width: this.width,
  121. height: this.height,
  122. posX: this.posX,
  123. posY: this.posY,
  124. handleName: this.handleName,
  125. },
  126. userCode: this.settings.letUserCode,
  127. socketCode: this.socketCode,
  128. };
  129. console.log(JSON.stringify(params))
  130. this.webSocketSend(JSON.stringify(params));
  131. }
  132. // 设置菜单遮挡问题
  133. setCoinRegion(flag, dorpDownIdArr) {
  134. const isEmbed = flag;
  135. let menu;
  136. let menuLeftX = 0;
  137. let menuLeftY = 0;
  138. const arr = [];
  139. this.zoom = this.detectZoom() / 100; // 910add
  140. if (flag === true) {
  141. // const obj = {};
  142. dorpDownIdArr.map((item, index) => {
  143. arr.push({});
  144. // obj[index] = document.querySelector(v);
  145. menu = document.querySelector(item);
  146. // 菜单宽高
  147. arr[index]["width"] = Math.round(menu.clientWidth * this.zoom);
  148. arr[index]["height"] = Math.round(menu.clientHeight * this.zoom);
  149. // 菜单左上角相对浏览器左上角的坐标
  150. // 菜单左上角相对浏览器左上角的坐标
  151. menuLeftX = Math.round(menu.getBoundingClientRect().left * this.zoom - 2);
  152. menuLeftY = Math.round(menu.getBoundingClientRect().top * this.zoom + this._toolHeight);
  153. arr[index]["posX"] = Math.round(menuLeftX + this.iframetoWinX * this.zoom);
  154. arr[index]["posY"] = Math.round(menuLeftY + this.iframetoWinY * this.zoom);
  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. if (this.zoom === 0.67) { // 现场笔记本
  198. this._toolHeight = 80;
  199. } else if (this.zoom === 1.5) {
  200. this._toolHeight = 110;
  201. } else if (this.zoom === 3) {
  202. this._toolHeight = 232;
  203. } else {
  204. const toolh = Math.round(window.outerHeight - (window.innerHeight) * this.detectZoom() / 100);
  205. this._toolHeight = toolh < 0 ? 90 : toolh;
  206. }
  207. }
  208. // 网页变操作化大小
  209. resize() {
  210. if (!this.status || !this.vModeObj.embedVideoMode) {
  211. return false;
  212. }
  213. const calcDomWidth = this.space.clientWidth || this.space.getBoundingClientRect().width;
  214. const calcDomheight = this.space.clientHeight || this.space.getBoundingClientRect().height;
  215. const zoom = this.detectZoom() / 100;
  216. const realClientWidth = Math.round(calcDomWidth * zoom);
  217. const realClientHeight = Math.round(calcDomheight * zoom);
  218. console.log("zoom---_toolHeight---", zoom, this._toolHeight);
  219. // 防止初始化后设置隐藏hide后拖动浏览器再show显示在左上方问题
  220. if (this.space.clientWidth === 0 && this.space.clientHeight === 0) {
  221. return;
  222. }
  223. if (zoom === 1) {
  224. // 默认伸缩状态
  225. // posX:距离浏览器左顶点横轴距离;posY:距离浏览器左顶点纵轴距离
  226. let posX = this.space.getBoundingClientRect().left;
  227. let posY;
  228. if (window.outerWidth === window.innerWidth) {
  229. // 普通浏览器最大化状态
  230. if (this.fullToolH == null) {
  231. // 为空时才计算,避免上下拖动改变浏览器大小事,视频窗口抖动
  232. this.fullToolL = (window.outerWidth - window.innerWidth) / 2;
  233. // _this.fullToolH = window.outerHeight - window.innerHeight - _this.fullToolL + 10;
  234. this.fullToolH = this._toolHeight;
  235. }
  236. posY = this.space.getBoundingClientRect().top + this.fullToolH;
  237. } else if (window.outerWidth - window.innerWidth < 0) {
  238. // F11全屏窗口计算
  239. posX = this.space.getBoundingClientRect().left;
  240. posY = this.space.getBoundingClientRect().top;
  241. } else {
  242. // 浏览器非最大化状态
  243. if (this.toolH == null) {
  244. this.toolL = (window.outerWidth - window.innerWidth) / 2;
  245. this.toolH = window.outerHeight - window.innerHeight - this.toolL;
  246. }
  247. posX = this.space.getBoundingClientRect().left;
  248. posY = this.space.getBoundingClientRect().top + this.toolH + 0;
  249. }
  250. posX = Math.round(posX);
  251. posY = Math.round(posY);
  252. this.dragResize(realClientWidth, realClientHeight, posX, posY);
  253. console.log("dragResize1-->", realClientWidth, realClientHeight, posX, posY);
  254. } else {
  255. if (window.outerHeight - window.innerHeight === 288 || window.outerWidth - window.innerWidth < 0) { // 最大化
  256. const longW = Math.round(this.space.getBoundingClientRect().left * zoom);
  257. const longH = Math.round(this.space.getBoundingClientRect().top * zoom);
  258. this.dragResize(realClientWidth, realClientHeight, longW, longH);
  259. console.log("最大化dragResize2-->", realClientWidth, realClientHeight, longW, longH);
  260. } else {
  261. const longW = Math.round(this.space.getBoundingClientRect().left * zoom);
  262. const longH = Math.round(this.space.getBoundingClientRect().top * zoom + this._toolHeight);
  263. this.dragResize(realClientWidth, realClientHeight, longW, longH);
  264. console.log("dragResize2-->", realClientWidth, realClientHeight, longW, longH);
  265. }
  266. }
  267. }
  268. // 创建视频窗口
  269. createVideoDialog(obj) {
  270. const {
  271. rows = 2, cols = 2, wndSpaceing = 0, embedVideoMode = true, playerCtrlBarEnable = false, displayMode = 0, playMode = 0, playParams = {}, webControlExpend = false
  272. } = obj;
  273. let playParamsObj = null;
  274. playMode === 0 ? playParamsObj = {} : playParamsObj = playParams;
  275. const params = JSON.stringify({
  276. "loginIp": this.settings.letLoginIp,
  277. "userCode": this.settings.letUserCode,
  278. "socketCode": this.socketCode,
  279. "method": "CreateVideoDialog",
  280. "params": {
  281. "handleName": this.handleName,
  282. "rows": rows,
  283. "cols": cols,
  284. "wndSpaceing": wndSpaceing, // 视频播放窗口间隔,默认为0
  285. "embedVideoMode": embedVideoMode, // 视频窗口是否为嵌入式。true:嵌入式(列表框云台和标题栏不展示),false:弹出式(展示列表框云台和标题栏)
  286. "playerCtrlBarEnable": playerCtrlBarEnable, // 视频窗口上的鼠标云台是否使能。true:展示,false:不展示
  287. "displayMode": displayMode, // 窗口显示模式,用于播放控制栏0:视频监控,显示播放控制栏; 1:轮播,隐藏播放控制栏
  288. "playMode": playMode, // playMode:播放模式,0: 普通模式,1:轮播模式,默认为普通播放模式
  289. "playParams": playParamsObj, // playMode=0,playParams为空;playMode=1,playParams格式如下:// 轮播时间间隔,单位秒,默认为5s
  290. "webControlExpend": webControlExpend
  291. }
  292. });
  293. this.webSocketSend(params);
  294. }
  295. //初始化窗口
  296. initClientPanel() {
  297. this.createVideoDialog(this.vModeObj);
  298. }
  299. // 实时预览视频
  300. realTimeVideo(arr = [{
  301. "channelId": ""
  302. }]) {
  303. // debugger
  304. const params = JSON.stringify({
  305. "loginIp": this.settings.letLoginIp,
  306. "method": "openVideo",
  307. "params": {
  308. "array": arr, // [{"channelId": channelId}]
  309. "handleName": this.handleName
  310. },
  311. "userCode": this.settings.letUserCode,
  312. "socketCode": this.socketCode
  313. });
  314. this.webSocketSend(params);
  315. this.winMaxMinOpt();
  316. }
  317. // 录像回放 //paramsArr传开始时间,结束时间,通道号
  318. playBack(paramsArr) {
  319. this.resize();
  320. const params = JSON.stringify({
  321. "loginIp": this.settings.letLoginIp,
  322. "method": "openRecord",
  323. "params": {
  324. "array": paramsArr, // [{"beginTime": "2019-12-27 00:00:00","channelId": "mA2I9l1kA1BOPTAPHT74P8","endTime": "2019-12-27 23:59:59"}]
  325. "handleName": this.handleName
  326. },
  327. "userCode": this.settings.letUserCode,
  328. "socketCode": this.socketCode
  329. });
  330. this.webSocketSend(params);
  331. this.winMaxMinOpt();
  332. setTimeout(() => {
  333. this.resize();
  334. }, 1000);
  335. }
  336. // 关闭客户端
  337. closeClient() {
  338. const params = {
  339. "loginIp": this.settings.letLoginIp,
  340. "method": "logout",
  341. "userCode": this.settings.letUserCode,
  342. "socketCode": this.socketCode
  343. };
  344. this.webSocketSend(JSON.stringify(params));
  345. this.stopWebSocket();
  346. this.websocket = null;
  347. }
  348. // debug(message) { // 客户端调试返回信息操作
  349. // debugTextArea.value += message + "\n\n";
  350. // debugTextArea.scrollTop = debugTextArea.scrollHeight;
  351. // }
  352. sendMessage() {
  353. const msg = document.getElementById("inputText").value;
  354. const strToSend = msg;
  355. if (this.websocket != null) {
  356. document.getElementById("inputText").value = "";
  357. this.webSocketSend(strToSend);
  358. }
  359. }
  360. stopWebSocket() {
  361. if (this.websocket) {
  362. this.websocket.close();
  363. }
  364. this.websocket = null;
  365. }
  366. // 客户端显示隐藏操作;
  367. showBrower(flag) {
  368. const params = {
  369. "loginIp": this.settings.letLoginIp,
  370. "method": "whetherShowVideoWnd",
  371. "params": {
  372. "bShow": flag,
  373. "handleName": this.handleName
  374. },
  375. "userCode": this.settings.letUserCode,
  376. "socketCode": this.socketCode
  377. };
  378. this.display = flag;
  379. this.status = flag;
  380. this.resize(); // 隐藏时候不走resize,显示时候感知下位置,但是,这不能加延时,否则切换标签页有问题。
  381. this.webSocketSend(JSON.stringify(params));
  382. }
  383. // 客户端-释放窗口
  384. destroyVideoDialog() {
  385. const params = {
  386. "loginIp": this.settings.letLoginIp,
  387. "method": "DestroyVideoDialog",
  388. "params": {
  389. "handleName": this.handleName
  390. },
  391. "userCode": this.settings.letUserCode,
  392. "socketCode": this.socketCode
  393. };
  394. this.webSocketSend(JSON.stringify(params));
  395. }
  396. // 视频置顶操作
  397. setVideoWndOnTop(flag) {
  398. const params = {
  399. "loginIp": this.settings.letLoginIp,
  400. "method": "setVideoWndOnTop",
  401. "params": {
  402. "bOnTop": flag,
  403. "handleName": this.handleName
  404. },
  405. "userCode": this.settings.letUserCode,
  406. "socketCode": this.socketCode
  407. };
  408. this.webSocketSend(JSON.stringify(params));
  409. }
  410. // 最大化最小化操作;
  411. winMaxMinOpt() {
  412. if (document.addEventListener) {
  413. document.addEventListener("webkitvisibilitychange", () => {
  414. if (document.webkitVisibilityState === "hidden") { // 最小化
  415. const temp = this.status;
  416. this.showBrower(false);
  417. this.status = temp;
  418. console.log("hidden");
  419. } else if (document.webkitVisibilityState === "visible") { // 最大化
  420. // 延时的目的:最大化的瞬间 走resize中的window.outerWidth - window.innerWidth < 0这个条件,缺少浏览器顶部栏的高度,导致高度上移
  421. console.log("visible");
  422. setTimeout(() => {
  423. this.showBrower(this.status);
  424. }, 200);
  425. }
  426. });
  427. }
  428. }
  429. // 关闭所有视频
  430. closeAllVideo() {
  431. const params = JSON.stringify({
  432. "loginIp": this.settings.letLoginIp,
  433. "method": "closeVideo",
  434. "userCode": this.settings.letUserCode,
  435. "params": {
  436. "handleName": this.handleName
  437. },
  438. "socketCode": this.socketCode
  439. });
  440. this.webSocketSend(params);
  441. }
  442. // 拉app视频
  443. openAppVideo(paramsArr) {
  444. const params = JSON.stringify({
  445. "loginIp": this.settings.letLoginIp,
  446. "method": "openAppVideo",
  447. "params": {
  448. "handleName": this.handleName,
  449. "array": paramsArr
  450. /* [
  451. {
  452. "channelId": "AcqRGNrqA1B15042F6AMEO",
  453. "userCode": "xxxxxx",
  454. "regionId": "xxxxx",
  455. "domainId": "xxxxx",
  456. }
  457. ] */
  458. },
  459. "userCode": this.settings.letUserCode,
  460. "socketCode": this.socketCode
  461. });
  462. this.webSocketSend(params);
  463. }
  464. // 设置窗口分割数
  465. setDesignDivision(rows, cols) {
  466. const params = JSON.stringify({
  467. "loginIp": this.settings.letLoginIp,
  468. "method": "SetDesignDivision",
  469. "userCode": this.settings.letUserCode,
  470. "params": {
  471. "handleName": this.handleName,
  472. "rows": Number(rows),
  473. "cols": Number(cols)
  474. },
  475. "socketCode": this.socketCode
  476. });
  477. this.webSocketSend(params);
  478. }
  479. // 设置视频双击后位置大小
  480. setDesignDivision(obj) {
  481. const {
  482. width = "0", height = "0", posX = "0", posY = "0"
  483. } = obj;
  484. const params = JSON.stringify({
  485. "loginIp": this.settings.letLoginIp,
  486. "method": "expendVideoCtrl",
  487. "userCode": this.settings.letUserCode,
  488. "params": {
  489. "handleName": this.handleName,
  490. "expend": true,
  491. "width": width,
  492. "height": height,
  493. "posX": posX,
  494. "posY": posY,
  495. },
  496. "socketCode": this.socketCode
  497. });
  498. this.webSocketSend(params);
  499. }
  500. // 通用方法第三方传方法名和参数体
  501. commonUse(method, paramsObj) {
  502. const params = JSON.stringify({
  503. "loginIp": this.settings.letLoginIp,
  504. "method": method,
  505. "userCode": this.settings.letUserCode,
  506. "params": paramsObj,
  507. "socketCode": this.socketCode
  508. });
  509. this.webSocketSend(params);
  510. }
  511. }