WXdraw.js 20 KB

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