WXdraw.js 19 KB

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