WXdraw.js 20 KB

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