webClient_initWnd.js 23 KB

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