WXdraw.js 21 KB

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