WXdraw.js 16 KB

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