webClient_initWnd.js 24 KB

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