WXdraw.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579
  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. errorTime += delay;
  33. return () => {
  34. if (!flag) return;
  35. flag = false;
  36. timer = setTimeout(() => {
  37. fn();
  38. flag = true;
  39. }, delay);
  40. };
  41. }
  42. function doConnectDirectives() {
  43. videoWidth = Number(resolvingPower) ? Number(resolvingPower) : 720;
  44. videoHeight = videoWidth === 720 ? 1280 : 1920;
  45. wsss = new WebSocket(cUrl);
  46. wsss.binaryType = 'arraybuffer';
  47. wsss.onopen = function () {
  48. // 获取虚拟场景状态
  49. var pings = { type: 'getVsStatus' };
  50. wsss.send(JSON.stringify(pings));
  51. var bitRate = {
  52. data: {
  53. bitRate: 1243000,
  54. },
  55. type: 'bitRate',
  56. };
  57. wsss.send(JSON.stringify(bitRate));
  58. // 进入发起询问
  59. var pings2 = {
  60. type: 'forwardMsg',
  61. data: {
  62. code: '3000',
  63. desc: '询问是否有在控制', // 可选
  64. },
  65. };
  66. wsss.send(JSON.stringify(pings2));
  67. wsss.send(
  68. JSON.stringify({
  69. type: 'getPhoneSize',
  70. }),
  71. );
  72. };
  73. wsss.onerror = function (e) {
  74. // console.log('🚀 ~ file: WXdraw.js ~ line 82 ~ onerror ~ e', e);
  75. // wsss.close(1006);
  76. // throttle(doConnectDirectives, 100);
  77. // if (errorTime > 1000) {
  78. // quit();
  79. // }
  80. };
  81. wsss.onclose = function (e) {
  82. // console.log('🚀 ~ file: WXdraw.js ~ line 93 ~ onclose ~ e', e.code);
  83. // new WebSocket(e.)
  84. // doConnectDirectives();
  85. if (e.code === 1006) {
  86. // 异常关闭,重连
  87. // doConnectDirectives();
  88. throttle(doConnectDirectives, 100);
  89. if (errorTime > 1000) {
  90. quit();
  91. }
  92. }
  93. };
  94. wsss.onmessage = function (res) {
  95. var result = typeof res.data === 'string' ? JSON.parse(res.data) : res.data;
  96. console.log(
  97. '🚀 ~ file: WXdraw.js ~ line 78 ~ doConnectDirectives ~ result',
  98. result,
  99. );
  100. if (result.type === 'cutting') {
  101. if (result.data.status === 0) {
  102. $.toast('复制成功', 'text');
  103. } else {
  104. $.toast(result.msg, 'text');
  105. }
  106. return;
  107. }
  108. if (result.type === 'forwardMsgRep') {
  109. // 当前云机无其他终端在线,获得控制权
  110. changIsControl(true);
  111. }
  112. if (result.type === 'forwardMsg') {
  113. /**
  114. * @type {boolean} isControl 当前是否拥有控制权,初始化时为false
  115. * @type {string} isAuth 当前云机类型 - huo: 获取的云机,none: 自己的云机
  116. * @type {string} username 当前登录的双子星账号username
  117. */
  118. switch (String(result.data.code)) {
  119. case '3000': {
  120. // 被询问控制
  121. if (isControl) {
  122. // 回复有控制
  123. wsss.send(
  124. JSON.stringify({
  125. type: 'forwardMsg',
  126. data: {
  127. code: '4000',
  128. username,
  129. desc: '有控制', // 可选
  130. },
  131. }),
  132. );
  133. return;
  134. }
  135. // 回复有观看
  136. wsss.send(
  137. JSON.stringify({
  138. type: 'forwardMsg',
  139. data: {
  140. code: '4100',
  141. username,
  142. desc: '有观看', // 可选
  143. },
  144. }),
  145. );
  146. return;
  147. }
  148. case '4100': {
  149. // 收到观看回复
  150. changIsControl(true);
  151. return;
  152. }
  153. case '4000': {
  154. // 收到控制权回复
  155. // 同账号,共同控制
  156. if (result.data.username === username) {
  157. changIsControl(true);
  158. return;
  159. }
  160. // 不同账号
  161. // 当前是获取方
  162. if (isAuth === 'huo') {
  163. $.confirm(
  164. '授权方已收回控制权,您进入观看屏幕模式',
  165. function () {
  166. //点击确认后的回调函数
  167. changIsControl(false);
  168. },
  169. function () {
  170. changIsControl(false);
  171. //点击取消后的回调函数
  172. quit();
  173. },
  174. );
  175. return;
  176. }
  177. if (!isControl) {
  178. // 当前是授权方切没有控制权
  179. $.confirm(
  180. '当前云手机正在授控,是否请求获取云手机控制权?',
  181. function () {
  182. //点击确认后的回调函数
  183. wsss.send(
  184. JSON.stringify({
  185. type: 'forwardMsg',
  186. data: {
  187. code: '5000',
  188. desc: '控制权限收回', // 可选
  189. },
  190. }),
  191. );
  192. changIsControl(true);
  193. },
  194. function () {
  195. //点击取消后的回调函数
  196. changIsControl(false);
  197. },
  198. );
  199. return;
  200. }
  201. return;
  202. }
  203. case '5000': {
  204. // if (result.data.username === username) {
  205. // changIsControl(true);
  206. // return;
  207. // }
  208. if (isControl && isAuth === 'huo') {
  209. $.confirm(
  210. '授权方已收回控制权,您进入观看屏幕模式',
  211. function () {
  212. //点击确认后的回调函数
  213. changIsControl(false);
  214. },
  215. function () {
  216. changIsControl(false);
  217. //点击取消后的回调函数
  218. quit();
  219. },
  220. );
  221. return;
  222. }
  223. return;
  224. }
  225. default: {
  226. return;
  227. }
  228. }
  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. const data = window.phoneSizeList.find(function (v) {
  263. return (
  264. v.width === Math.min(result.data.width, result.data.height) &&
  265. v.height === Math.max(result.data.width, result.data.height) &&
  266. v.dpi === result.data.dpi
  267. );
  268. });
  269. window.currentPhoneSize = data || {
  270. width: Math.min(result.data.width, result.data.height),
  271. height: Math.max(result.data.width, result.data.height),
  272. dpi: result.data.dpi,
  273. };
  274. if (result.type === 'setPhoneSize') {
  275. lastSetPhone = Date.now();
  276. }
  277. return;
  278. }
  279. };
  280. }
  281. $('body').on('click', function () {
  282. draw_graph('pencil', this);
  283. });
  284. //剪切板
  285. $('.upload').on('click', function () {
  286. var texts = $(this).attr('data-text');
  287. if (texts == 'uploads') {
  288. $('.mainbox').css({
  289. display: 'block',
  290. });
  291. $('.sbox').css({
  292. display: 'none',
  293. });
  294. }
  295. });
  296. //home 控制home
  297. $('.botmat1img').on('click', function () {
  298. var codes = $(this).attr('data-text');
  299. if (codes == 'home' && isControl) {
  300. wsss.send(ExexuteKeyBoard(3));
  301. } else if (codes == 'return' && isControl) {
  302. wsss.send(ExexuteKeyBoard(4));
  303. } else if (codes == 'gengduo' && isControl) {
  304. wsss.send(ExexuteKeyBoard(187));
  305. }
  306. });
  307. // 高清控制
  308. $('.PictureQuality').on('click', function () {
  309. if (!isControl) {
  310. return;
  311. }
  312. $(this).addClass('avit').siblings().removeClass('avit');
  313. var id = $(this).attr('data-id');
  314. var cmd = {
  315. type: 'switchSharpness',
  316. };
  317. decodeWoker.postMessage(cmd); //通知解码器worker切换分辨率
  318. var buffer = makeSharpness(Number(id));
  319. webSocketWorker.postMessage(buffer);
  320. });
  321. var canDraw = false;
  322. //画图形
  323. var draw_graph = function (graphType) {
  324. //把蒙版放于画板上面
  325. $('#container').css('z-index', 30);
  326. $('#dedit').css('z-index', 20);
  327. // 先画在蒙版上 再复制到画布上
  328. //鼠标按下获取 开始xy开始画图
  329. // var ongoingTouches = [];
  330. var touchstart = function (e) {
  331. // console.log('🚀 ~ file: WXdraw.js ~ line 244 ~ touchstart ~ e', e);
  332. if (!isControl) {
  333. return;
  334. }
  335. const action = 0;
  336. Array.from(e.originalEvent.changedTouches).forEach(function (item, index) {
  337. const x = item.clientX - item.target.getBoundingClientRect().x;
  338. const y = item.clientY - item.target.getBoundingClientRect().y;
  339. return wsss.send(
  340. JSON.stringify({
  341. type: 'event',
  342. data: {
  343. action,
  344. count: e.originalEvent.touches.length,
  345. pointerId: item.identifier,
  346. x: (function () {
  347. return (
  348. resolving
  349. ? x * (window.currentPhoneSize.width / vowidth)
  350. : y * (window.currentPhoneSize.height / voheight)
  351. ).toFixed(2);
  352. })(),
  353. y: (function () {
  354. return (
  355. resolving
  356. ? y * (window.currentPhoneSize.height / voheight)
  357. : (vowidth - x) * (window.currentPhoneSize.width / vowidth)
  358. ).toFixed(2);
  359. })(),
  360. },
  361. }),
  362. );
  363. });
  364. canDraw = true;
  365. };
  366. //鼠标离开 把蒙版canvas的图片生成到canvas中
  367. var touchend = function (e) {
  368. if (!isControl) {
  369. return;
  370. }
  371. const action = 1;
  372. Array.from(e.originalEvent.changedTouches).forEach(function (item, index) {
  373. const x = item.clientX - item.target.getBoundingClientRect().x;
  374. const y = item.clientY - item.target.getBoundingClientRect().y;
  375. return wsss.send(
  376. JSON.stringify({
  377. type: 'event',
  378. data: {
  379. action,
  380. count: e.originalEvent.touches.length,
  381. pointerId: item.identifier,
  382. x: (function () {
  383. return (
  384. resolving
  385. ? x * (window.currentPhoneSize.width / vowidth)
  386. : y * (window.currentPhoneSize.height / voheight)
  387. ).toFixed(2);
  388. })(),
  389. y: (function () {
  390. return (
  391. resolving
  392. ? y * (window.currentPhoneSize.height / voheight)
  393. : (vowidth - x) * (window.currentPhoneSize.width / vowidth)
  394. ).toFixed(2);
  395. })(),
  396. },
  397. }),
  398. );
  399. });
  400. // var touchfor = e.originalEvent.changedTouches; //for 的手指数组
  401. // //是否横屏
  402. // for (var i = 0; i < touchfor.length; i++) {
  403. // // var acrossWidthX = touchfor[i].pageY * (videoHeight / voheight);
  404. // // var acrossHeightY =
  405. // // videoWidth - touchfor[i].pageX * (videoWidth / vowidth);
  406. // // if (resolving) {
  407. // // var verticalWidthX =
  408. // // touchfor[i].pageX * (window.currentPhoneSize.width / vowidth);
  409. // // var verticalHeightY =
  410. // // touchfor[i].pageY * (window.currentPhoneSize.height / voheight);
  411. // // } else {
  412. // // var verticalWidthX =
  413. // // touchfor[i].pageX * (window.currentPhoneSize.width / vowidth);
  414. // // var verticalHeightY =
  415. // // touchfor[i].pageY * (window.currentPhoneSize.height / voheight);
  416. // // }
  417. // var ping =
  418. // // resolving == 0 ?
  419. // // { "data": { "action": 1, "count": ongoingTouches.length, "pointerId": touchfor[i].identifier, "x": acrossWidthX.toFixed(2), "y": acrossHeightY.toFixed(2) }, "type": "event" } :
  420. // {
  421. // data: {
  422. // action: 1,
  423. // count: ongoingTouches.length,
  424. // pointerId: touchfor[i].identifier,
  425. // x: (() =>
  426. // (resolving
  427. // ? touchfor[i].pageX * (window.currentPhoneSize.width / vowidth)
  428. // : touchfor[i].pageY * (window.currentPhoneSize.width / voheight)
  429. // ).toFixed(2))(),
  430. // y: (() =>
  431. // (resolving
  432. // ? touchfor[i].pageY *
  433. // (window.currentPhoneSize.height / voheight)
  434. // : (vowidth - touchfor[i].pageX) *
  435. // (window.currentPhoneSize.height / vowidth)
  436. // ).toFixed(2))(),
  437. // },
  438. // type: 'event',
  439. // };
  440. // wsss.send(JSON.stringify(ping));
  441. // ongoingTouches.forEach(function (item, index) {
  442. // if (item.identifier === touchfor[i].identifier) {
  443. // ongoingTouches.splice(index, 1);
  444. // }
  445. // });
  446. // }
  447. canDraw = false;
  448. };
  449. //清空层 云手机超出屏幕的开关
  450. var clearContext = function () {
  451. canDraw = false;
  452. };
  453. // 鼠标移动
  454. var touchmove = function (e) {
  455. e.preventDefault();
  456. if (!isControl) {
  457. return;
  458. }
  459. const action = 2;
  460. Array.from(e.originalEvent.changedTouches).forEach(function (item, index) {
  461. const x = item.clientX - item.target.getBoundingClientRect().x;
  462. const y = item.clientY - item.target.getBoundingClientRect().y;
  463. return wsss.send(
  464. JSON.stringify({
  465. type: 'event',
  466. data: {
  467. action,
  468. count: e.originalEvent.touches.length,
  469. pointerId: item.identifier,
  470. x: (function () {
  471. return (
  472. resolving
  473. ? x * (window.currentPhoneSize.width / vowidth)
  474. : y * (window.currentPhoneSize.height / voheight)
  475. ).toFixed(2);
  476. })(),
  477. y: (function () {
  478. return (
  479. resolving
  480. ? y * (window.currentPhoneSize.height / voheight)
  481. : (vowidth - x) * (window.currentPhoneSize.width / vowidth)
  482. ).toFixed(2);
  483. })(),
  484. },
  485. }),
  486. );
  487. });
  488. // var touchfor = e.originalEvent.targetTouches; //for 的手指数组
  489. // for (var i = 0; i < touchfor.length; i++) {
  490. // // var acrossWidthX = touchfor[i].pageY * (videoHeight / voheight);
  491. // // var acrossHeightY =
  492. // // videoWidth - touchfor[i].pageX * (videoWidth / vowidth);
  493. // // let verticalWidthX = 0;
  494. // // let verticalHeightY = 0;
  495. // // if (resolving) {
  496. // // verticalWidthX =
  497. // // touchfor[i].pageX * (window.currentPhoneSize.width / vowidth);
  498. // // verticalHeightY =
  499. // // touchfor[i].pageY * (window.currentPhoneSize.height / voheight);
  500. // // } else {
  501. // // verticalWidthX =
  502. // // touchfor[i].pageX * (window.currentPhoneSize.width / vowidth);
  503. // // verticalHeightY =
  504. // // touchfor[i].pageY * (window.currentPhoneSize.height / voheight);
  505. // // }
  506. // var ping =
  507. // // resolving == 0 ?
  508. // // { "data": { "action": 2, "count": touchfor.length, "pointerId": touchfor[i].identifier, "x": acrossWidthX.toFixed(2), "y": acrossHeightY.toFixed(2) }, "type": "event" } :
  509. // {
  510. // data: {
  511. // action: 2,
  512. // count: touchfor.length,
  513. // pointerId: touchfor[i].identifier,
  514. // x: (() =>
  515. // (resolving
  516. // ? touchfor[i].pageX * (window.currentPhoneSize.width / vowidth)
  517. // : touchfor[i].pageY * (window.currentPhoneSize.width / voheight)
  518. // ).toFixed(2))(),
  519. // y: (() =>
  520. // (resolving
  521. // ? touchfor[i].pageY *
  522. // (window.currentPhoneSize.height / voheight)
  523. // : (vowidth - touchfor[i].pageX) *
  524. // (window.currentPhoneSize.height / vowidth)
  525. // ).toFixed(2))(),
  526. // },
  527. // type: 'event',
  528. // };
  529. // wsss.send(JSON.stringify(ping));
  530. // }
  531. };
  532. //鼠标离开区域以外 除了涂鸦 都清空
  533. var mouseout = function () {
  534. if (graphType != 'handwriting') {
  535. clearContext();
  536. }
  537. };
  538. $(canvas_bak).off();
  539. $(canvas_bak).on('touchstart', touchstart);
  540. $(canvas_bak).on('touchmove', touchmove);
  541. $(canvas_bak).on('touchend', touchend);
  542. $(canvas_bak).on('mouseout', mouseout);
  543. };
  544. function GetRequest() {
  545. var url = location.search; // 获取url中"?"符后的字串
  546. var obj = new Object();
  547. if (url.indexOf('?') != -1) {
  548. var str = url.substr(1);
  549. strs = str.split('&');
  550. for (var i = 0; i < strs.length; i++) {
  551. obj[strs[i].split('=')[0]] = strs[i].split('=')[1];
  552. }
  553. }
  554. return obj;
  555. }