WXdraw.js 18 KB

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