WXdraw.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625
  1. // 蒙版
  2. var canvas_bak = document.getElementById('box');
  3. var winHeight = window.screen.height - window.innerHeight;
  4. var vowidth = window.screen.width;
  5. var topwinHeightDraw = window.screen.height - window.innerHeight + 30; //计算title top 头部
  6. var numse = window.screen.height; // -winHeight
  7. //计算title top 头部
  8. if (numse <= 70) {
  9. var voheight = window.screen.height - winHeight - 34 - 20;
  10. } else {
  11. var voheight = window.screen.height - topwinHeightDraw - 20;
  12. }
  13. //画笔大小
  14. var resolving = 1; // 1: 竖屏;2:横屏;
  15. var url = window.location.href;
  16. url = url.split('/');
  17. var parameters = GetRequest();
  18. var videoWidth, videoHeight;
  19. var isControl = false; // 是否是观看模式
  20. changIsControl(true);
  21. var isAuth = parameters['authPhone']; // 是否是获取的云手机
  22. var wsss;
  23. var errorTime = 0;
  24. var first = true;
  25. function changIsControl(value) {
  26. isControl = value;
  27. // if(){}
  28. $('#open-set-phone-size-dialog-btn').attr('hidden', !value);
  29. }
  30. function throttle(fn, delay) {
  31. var flag = true;
  32. return () => {
  33. if (!flag) return;
  34. flag = false;
  35. errorTime += delay;
  36. timer = setTimeout(() => {
  37. fn();
  38. flag = true;
  39. }, delay);
  40. };
  41. }
  42. const throttleDoConnectDirectives = throttle(() => {
  43. doConnectDirectives();
  44. }, 100);
  45. function doConnectDirectives() {
  46. videoWidth = Number(resolvingPower) ? Number(resolvingPower) : 720;
  47. videoHeight = videoWidth === 720 ? 1280 : 1920;
  48. wsss = new WebSocket(cUrl);
  49. wsss.binaryType = 'arraybuffer';
  50. wsss.onopen = function () {
  51. // 获取虚拟场景状态
  52. errorTime = 0;
  53. var pings = { type: 'getVsStatus' };
  54. wsss.send(JSON.stringify(pings));
  55. var bitRate = {
  56. data: {
  57. bitRate: 1243000,
  58. },
  59. type: 'bitRate',
  60. };
  61. wsss.send(JSON.stringify(bitRate));
  62. // 进入发起询问
  63. var pings2 = {
  64. type: 'forwardMsg',
  65. data: {
  66. code: '3000',
  67. username,
  68. desc: '询问是否有在控制', // 可选
  69. },
  70. };
  71. wsss.send(JSON.stringify(pings2));
  72. wsss.send(
  73. JSON.stringify({
  74. type: 'getPhoneSize',
  75. }),
  76. );
  77. };
  78. wsss.onerror = function (e) {
  79. // console.log('🚀 ~ file: WXdraw.js ~ line 82 ~ onerror ~ e', e);
  80. wsss.close(1006);
  81. // throttle(doConnectDirectives, 100);
  82. // if (errorTime > 1000) {
  83. // quit();
  84. // }
  85. };
  86. wsss.onclose = function (e) {
  87. // console.log('🚀 ~ file: WXdraw.js ~ line 93 ~ onclose ~ e', e);
  88. // new WebSocket(e.)
  89. // doConnectDirectives();
  90. if (e.code === 1006) {
  91. // 异常关闭,重连
  92. throttleDoConnectDirectives();
  93. // doConnectDirectives();
  94. // throttle(doConnectDirectives, 100);
  95. if (errorTime > 1000) {
  96. quit();
  97. }
  98. }
  99. };
  100. wsss.onmessage = function (res) {
  101. var result = typeof res.data === 'string' ? JSON.parse(res.data) : res.data;
  102. console.log(
  103. '🚀 ~ file: WXdraw.js ~ line 78 ~ doConnectDirectives ~ result',
  104. result,
  105. );
  106. if (result.type === 'cutting') {
  107. if (result.data.status === 0) {
  108. $.toast('复制成功', 'text');
  109. } else {
  110. $.toast(result.msg, 'text');
  111. }
  112. return;
  113. }
  114. if (result.type === 'forwardMsgRep') {
  115. // 当前云机无其他终端在线,获得控制权
  116. changIsControl(true);
  117. }
  118. if (result.type === 'forwardMsg') {
  119. /**
  120. * @type {boolean} isControl 当前是否拥有控制权,初始化时为false
  121. * @type {string} isAuth 当前云机类型 - huo: 获取的云机,none: 自己的云机
  122. * @type {string} username 当前登录的双子星账号username
  123. */
  124. if (result.data.username !== username) {
  125. switch (String(result.data.code)) {
  126. case '3000': {
  127. if (isControl) {
  128. // 回复有控制
  129. wsss.send(
  130. JSON.stringify({
  131. type: 'forwardMsg',
  132. data: {
  133. code: '4000',
  134. username,
  135. desc: '有控制', // 可选
  136. },
  137. }),
  138. );
  139. return;
  140. }
  141. // 回复有观看
  142. wsss.send(
  143. JSON.stringify({
  144. type: 'forwardMsg',
  145. data: {
  146. code: '4100',
  147. username,
  148. desc: '有观看', // 可选
  149. },
  150. }),
  151. );
  152. return;
  153. }
  154. case '4000': {
  155. // 当前是获取方
  156. if (isAuth === 'huo' && isControl) {
  157. $.confirm(
  158. '授权方已收回控制权,您进入观看屏幕模式',
  159. function () {
  160. //点击确认后的回调函数
  161. changIsControl(false);
  162. },
  163. function () {
  164. changIsControl(false);
  165. //点击取消后的回调函数
  166. quit();
  167. },
  168. );
  169. return;
  170. }
  171. if (!isControl && isAuth === 'shou') {
  172. // 当前是授权方切没有控制权
  173. $.confirm(
  174. '当前云手机正在授控,是否请求获取云手机控制权?',
  175. function () {
  176. //点击确认后的回调函数
  177. wsss.send(
  178. JSON.stringify({
  179. type: 'forwardMsg',
  180. data: {
  181. code: '5000',
  182. username,
  183. desc: '控制权限收回', // 可选
  184. },
  185. }),
  186. );
  187. changIsControl(true);
  188. },
  189. function () {
  190. //点击取消后的回调函数
  191. changIsControl(false);
  192. },
  193. );
  194. return;
  195. }
  196. }
  197. case '5000': {
  198. // if (result.data.username === username) {
  199. // changIsControl(true);
  200. // return;
  201. // }
  202. if (isAuth === 'huo' && isControl) {
  203. $.confirm(
  204. '授权方已收回控制权,您进入观看屏幕模式',
  205. function () {
  206. //点击确认后的回调函数
  207. changIsControl(false);
  208. },
  209. function () {
  210. changIsControl(false);
  211. //点击取消后的回调函数
  212. quit();
  213. },
  214. );
  215. return;
  216. }
  217. return;
  218. }
  219. default: {
  220. return;
  221. }
  222. }
  223. }
  224. return;
  225. }
  226. if (result.type === 'payInitiateEvent') {
  227. var url = window.location.href;
  228. url = url.split('/');
  229. var baseUrl = url[0] + '//' + url[2];
  230. $.ajax({
  231. url: baseUrl + '/api/pay/third/order',
  232. data: JSON.stringify({
  233. orderNum: result.data.orderNum,
  234. orderAmount: result.data.orderAmount,
  235. appKey: result.data.appKey,
  236. }),
  237. type: 'POST',
  238. dataType: 'json',
  239. contentType: 'application/json;charset=UTF-8',
  240. success: function (res) {
  241. if (result.data.payType === 1) {
  242. // 微信
  243. if (window.__wxjs_environment === 'miniprogram') {
  244. // 小程序
  245. // copyUrl(result.data.payUrl);
  246. } else {
  247. window.location.href = result.data.payUrl;
  248. }
  249. } else {
  250. window.location.href = result.data.payUrl;
  251. }
  252. },
  253. });
  254. return;
  255. }
  256. if (result.type === 'getPhoneSize' || result.type === 'setPhoneSize') {
  257. // console.log(result);
  258. if (
  259. window.currentPhoneSize &&
  260. (window.currentPhoneSize.width !==
  261. Math.min(result.data.width, result.data.height) ||
  262. window.currentPhoneSize.height !==
  263. Math.max(result.data.width, result.data.height) ||
  264. window.currentPhoneSize.dpi !== result.data.dpi)
  265. ) {
  266. // 获取到的分辨率与当前分辨率不符
  267. const data = window.phoneSizeList.find(function (v) {
  268. return (
  269. v.width === Math.min(result.data.width, result.data.height) &&
  270. v.height === Math.max(result.data.width, result.data.height) &&
  271. v.dpi === result.data.dpi
  272. );
  273. });
  274. window.currentPhoneSize = data || {
  275. width: Math.min(result.data.width, result.data.height),
  276. height: Math.max(result.data.width, result.data.height),
  277. dpi: result.data.dpi,
  278. };
  279. if (result.type === 'setPhoneSize') {
  280. lastSetPhone = Date.now();
  281. }
  282. // if (result.type === 'getPhoneSize') {
  283. // 上报给后端
  284. $.ajax({
  285. url:
  286. baseUrl +
  287. '/api/resources/v5/machine/resolution/operationResolvingPower',
  288. headers: {
  289. Authorization: token,
  290. },
  291. type: 'post',
  292. dataType: 'json',
  293. contentType: 'application/json; charset=UTF-8',
  294. data: JSON.stringify({
  295. userCardId: window.userCardId,
  296. resolvingPowerId: window.currentPhoneSize.id,
  297. }),
  298. });
  299. }
  300. // }
  301. updateDB(db, storeName, {
  302. id: userCardId,
  303. socketURL: socketURL,
  304. cUrl: cUrl,
  305. cardToken: cardToken,
  306. resolvingPower: resolvingPower,
  307. width: window.currentPhoneSize.width,
  308. height: window.currentPhoneSize.height,
  309. dpi: window.currentPhoneSize.dpi,
  310. });
  311. return;
  312. }
  313. if (result.type === 'reProduceText') {
  314. // 接收到云机剪贴板复制事件
  315. // window.copyToClipboard(result.data.text);
  316. if (navigator.clipboard) {
  317. navigator.clipboard.writeText(result.data.text);
  318. }
  319. }
  320. };
  321. }
  322. $('body').on('click', function () {
  323. draw_graph('pencil', this);
  324. });
  325. //剪切板
  326. $('.upload').on('click', function () {
  327. var texts = $(this).attr('data-text');
  328. if (texts == 'uploads') {
  329. $('.mainbox').css({
  330. display: 'block',
  331. });
  332. $('.sbox').css({
  333. display: 'none',
  334. });
  335. }
  336. });
  337. //home 控制home
  338. $('.botmat1img').on('click', function () {
  339. var codes = $(this).attr('data-text');
  340. if (codes == 'home' && isControl) {
  341. wsss.send(ExexuteKeyBoard(3));
  342. } else if (codes == 'return' && isControl) {
  343. wsss.send(ExexuteKeyBoard(4));
  344. } else if (codes == 'gengduo' && isControl) {
  345. wsss.send(ExexuteKeyBoard(187));
  346. }
  347. });
  348. // 高清控制
  349. $('.PictureQuality').on('click', function () {
  350. if (!isControl) {
  351. return;
  352. }
  353. $(this).addClass('avit').siblings().removeClass('avit');
  354. var id = $(this).attr('data-id');
  355. var cmd = {
  356. type: 'switchSharpness',
  357. };
  358. decodeWoker.postMessage(cmd); //通知解码器worker切换分辨率
  359. var buffer = makeSharpness(Number(id));
  360. webSocketWorker.postMessage(buffer);
  361. });
  362. var canDraw = false;
  363. //画图形
  364. var draw_graph = function (graphType) {
  365. //把蒙版放于画板上面
  366. $('#container').css('z-index', 30);
  367. $('#dedit').css('z-index', 20);
  368. // 先画在蒙版上 再复制到画布上
  369. //鼠标按下获取 开始xy开始画图
  370. // var ongoingTouches = [];
  371. var touchstart = function (e) {
  372. // console.log('🚀 ~ file: WXdraw.js ~ line 244 ~ touchstart ~ e', e);
  373. if (!isControl) {
  374. return;
  375. }
  376. const action = 0;
  377. Array.from(e.originalEvent.changedTouches).forEach(function (item, index) {
  378. const x = item.clientX - item.target.getBoundingClientRect().x;
  379. const y = item.clientY - item.target.getBoundingClientRect().y;
  380. return wsss.send(
  381. JSON.stringify({
  382. type: 'event',
  383. data: {
  384. action,
  385. count: e.originalEvent.touches.length,
  386. pointerId: item.identifier,
  387. x: (function () {
  388. return (
  389. resolving
  390. ? x * (window.currentPhoneSize.width / vowidth)
  391. : y * (window.currentPhoneSize.height / voheight)
  392. ).toFixed(2);
  393. })(),
  394. y: (function () {
  395. return (
  396. resolving
  397. ? y * (window.currentPhoneSize.height / voheight)
  398. : (vowidth - x) * (window.currentPhoneSize.width / vowidth)
  399. ).toFixed(2);
  400. })(),
  401. },
  402. }),
  403. );
  404. });
  405. canDraw = true;
  406. };
  407. //鼠标离开 把蒙版canvas的图片生成到canvas中
  408. var touchend = function (e) {
  409. if (!isControl) {
  410. return;
  411. }
  412. const action = 1;
  413. Array.from(e.originalEvent.changedTouches).forEach(function (item, index) {
  414. const x = item.clientX - item.target.getBoundingClientRect().x;
  415. const y = item.clientY - item.target.getBoundingClientRect().y;
  416. return wsss.send(
  417. JSON.stringify({
  418. type: 'event',
  419. data: {
  420. action,
  421. count: e.originalEvent.touches.length,
  422. pointerId: item.identifier,
  423. x: (function () {
  424. return (
  425. resolving
  426. ? x * (window.currentPhoneSize.width / vowidth)
  427. : y * (window.currentPhoneSize.height / voheight)
  428. ).toFixed(2);
  429. })(),
  430. y: (function () {
  431. return (
  432. resolving
  433. ? y * (window.currentPhoneSize.height / voheight)
  434. : (vowidth - x) * (window.currentPhoneSize.width / vowidth)
  435. ).toFixed(2);
  436. })(),
  437. },
  438. }),
  439. );
  440. });
  441. // var touchfor = e.originalEvent.changedTouches; //for 的手指数组
  442. // //是否横屏
  443. // for (var i = 0; i < touchfor.length; i++) {
  444. // // var acrossWidthX = touchfor[i].pageY * (videoHeight / voheight);
  445. // // var acrossHeightY =
  446. // // videoWidth - touchfor[i].pageX * (videoWidth / vowidth);
  447. // // if (resolving) {
  448. // // var verticalWidthX =
  449. // // touchfor[i].pageX * (window.currentPhoneSize.width / vowidth);
  450. // // var verticalHeightY =
  451. // // touchfor[i].pageY * (window.currentPhoneSize.height / voheight);
  452. // // } else {
  453. // // var verticalWidthX =
  454. // // touchfor[i].pageX * (window.currentPhoneSize.width / vowidth);
  455. // // var verticalHeightY =
  456. // // touchfor[i].pageY * (window.currentPhoneSize.height / voheight);
  457. // // }
  458. // var ping =
  459. // // resolving == 0 ?
  460. // // { "data": { "action": 1, "count": ongoingTouches.length, "pointerId": touchfor[i].identifier, "x": acrossWidthX.toFixed(2), "y": acrossHeightY.toFixed(2) }, "type": "event" } :
  461. // {
  462. // data: {
  463. // action: 1,
  464. // count: ongoingTouches.length,
  465. // pointerId: touchfor[i].identifier,
  466. // x: (() =>
  467. // (resolving
  468. // ? touchfor[i].pageX * (window.currentPhoneSize.width / vowidth)
  469. // : touchfor[i].pageY * (window.currentPhoneSize.width / voheight)
  470. // ).toFixed(2))(),
  471. // y: (() =>
  472. // (resolving
  473. // ? touchfor[i].pageY *
  474. // (window.currentPhoneSize.height / voheight)
  475. // : (vowidth - touchfor[i].pageX) *
  476. // (window.currentPhoneSize.height / vowidth)
  477. // ).toFixed(2))(),
  478. // },
  479. // type: 'event',
  480. // };
  481. // wsss.send(JSON.stringify(ping));
  482. // ongoingTouches.forEach(function (item, index) {
  483. // if (item.identifier === touchfor[i].identifier) {
  484. // ongoingTouches.splice(index, 1);
  485. // }
  486. // });
  487. // }
  488. canDraw = false;
  489. };
  490. //清空层 云手机超出屏幕的开关
  491. var clearContext = function () {
  492. canDraw = false;
  493. };
  494. // 鼠标移动
  495. var touchmove = function (e) {
  496. e.preventDefault();
  497. if (!isControl) {
  498. return;
  499. }
  500. const action = 2;
  501. Array.from(e.originalEvent.changedTouches).forEach(function (item, index) {
  502. const x = item.clientX - item.target.getBoundingClientRect().x;
  503. const y = item.clientY - item.target.getBoundingClientRect().y;
  504. return wsss.send(
  505. JSON.stringify({
  506. type: 'event',
  507. data: {
  508. action,
  509. count: e.originalEvent.touches.length,
  510. pointerId: item.identifier,
  511. x: (function () {
  512. return (
  513. resolving
  514. ? x * (window.currentPhoneSize.width / vowidth)
  515. : y * (window.currentPhoneSize.height / voheight)
  516. ).toFixed(2);
  517. })(),
  518. y: (function () {
  519. return (
  520. resolving
  521. ? y * (window.currentPhoneSize.height / voheight)
  522. : (vowidth - x) * (window.currentPhoneSize.width / vowidth)
  523. ).toFixed(2);
  524. })(),
  525. },
  526. }),
  527. );
  528. });
  529. // var touchfor = e.originalEvent.targetTouches; //for 的手指数组
  530. // for (var i = 0; i < touchfor.length; i++) {
  531. // // var acrossWidthX = touchfor[i].pageY * (videoHeight / voheight);
  532. // // var acrossHeightY =
  533. // // videoWidth - touchfor[i].pageX * (videoWidth / vowidth);
  534. // // let verticalWidthX = 0;
  535. // // let verticalHeightY = 0;
  536. // // if (resolving) {
  537. // // verticalWidthX =
  538. // // touchfor[i].pageX * (window.currentPhoneSize.width / vowidth);
  539. // // verticalHeightY =
  540. // // touchfor[i].pageY * (window.currentPhoneSize.height / voheight);
  541. // // } else {
  542. // // verticalWidthX =
  543. // // touchfor[i].pageX * (window.currentPhoneSize.width / vowidth);
  544. // // verticalHeightY =
  545. // // touchfor[i].pageY * (window.currentPhoneSize.height / voheight);
  546. // // }
  547. // var ping =
  548. // // resolving == 0 ?
  549. // // { "data": { "action": 2, "count": touchfor.length, "pointerId": touchfor[i].identifier, "x": acrossWidthX.toFixed(2), "y": acrossHeightY.toFixed(2) }, "type": "event" } :
  550. // {
  551. // data: {
  552. // action: 2,
  553. // count: touchfor.length,
  554. // pointerId: touchfor[i].identifier,
  555. // x: (() =>
  556. // (resolving
  557. // ? touchfor[i].pageX * (window.currentPhoneSize.width / vowidth)
  558. // : touchfor[i].pageY * (window.currentPhoneSize.width / voheight)
  559. // ).toFixed(2))(),
  560. // y: (() =>
  561. // (resolving
  562. // ? touchfor[i].pageY *
  563. // (window.currentPhoneSize.height / voheight)
  564. // : (vowidth - touchfor[i].pageX) *
  565. // (window.currentPhoneSize.height / vowidth)
  566. // ).toFixed(2))(),
  567. // },
  568. // type: 'event',
  569. // };
  570. // wsss.send(JSON.stringify(ping));
  571. // }
  572. };
  573. //鼠标离开区域以外 除了涂鸦 都清空
  574. var mouseout = function () {
  575. if (graphType != 'handwriting') {
  576. clearContext();
  577. }
  578. };
  579. $(canvas_bak).off();
  580. $(canvas_bak).on('touchstart', touchstart);
  581. $(canvas_bak).on('touchmove', touchmove);
  582. $(canvas_bak).on('touchend', touchend);
  583. $(canvas_bak).on('mouseout', mouseout);
  584. };
  585. function GetRequest() {
  586. var url = location.search; // 获取url中"?"符后的字串
  587. var obj = new Object();
  588. if (url.indexOf('?') != -1) {
  589. var str = url.substr(1);
  590. strs = str.split('&');
  591. for (var i = 0; i < strs.length; i++) {
  592. obj[strs[i].split('=')[0]] = strs[i].split('=')[1];
  593. }
  594. }
  595. return obj;
  596. }