WXdraw.js 19 KB

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