WXdraw.js 20 KB

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