WXdraw.js 19 KB

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