WXdraw.js 21 KB

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