WXdraw.js 19 KB

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