WXdraw.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570
  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. const data = window.phoneSizeList.find(function (v) {
  253. return (
  254. v.width === Math.min(result.data.width, result.data.height) &&
  255. v.height === Math.max(result.data.width, result.data.height) &&
  256. v.dpi === result.data.dpi
  257. );
  258. });
  259. window.currentPhoneSize = data || {
  260. width: Math.min(result.data.width, result.data.height),
  261. height: Math.max(result.data.width, result.data.height),
  262. dpi: result.data.dpi,
  263. };
  264. if (result.type === 'setPhoneSize') {
  265. lastSetPhone = Date.now();
  266. }
  267. return;
  268. }
  269. };
  270. }
  271. $('body').on('click', function () {
  272. draw_graph('pencil', this);
  273. });
  274. //剪切板
  275. $('.upload').on('click', function () {
  276. var texts = $(this).attr('data-text');
  277. if (texts == 'uploads') {
  278. $('.mainbox').css({
  279. display: 'block',
  280. });
  281. $('.sbox').css({
  282. display: 'none',
  283. });
  284. }
  285. });
  286. //home 控制home
  287. $('.botmat1img').on('click', function () {
  288. var codes = $(this).attr('data-text');
  289. if (codes == 'home' && isControl) {
  290. wsss.send(ExexuteKeyBoard(3));
  291. } else if (codes == 'return' && isControl) {
  292. wsss.send(ExexuteKeyBoard(4));
  293. } else if (codes == 'gengduo' && isControl) {
  294. wsss.send(ExexuteKeyBoard(187));
  295. }
  296. });
  297. // 高清控制
  298. $('.PictureQuality').on('click', function () {
  299. if (!isControl) {
  300. return;
  301. }
  302. $(this).addClass('avit').siblings().removeClass('avit');
  303. var id = $(this).attr('data-id');
  304. var cmd = {
  305. type: 'switchSharpness',
  306. };
  307. decodeWoker.postMessage(cmd); //通知解码器worker切换分辨率
  308. var buffer = makeSharpness(Number(id));
  309. webSocketWorker.postMessage(buffer);
  310. });
  311. var canDraw = false;
  312. //画图形
  313. var draw_graph = function (graphType) {
  314. //把蒙版放于画板上面
  315. $('#container').css('z-index', 30);
  316. $('#dedit').css('z-index', 20);
  317. // 先画在蒙版上 再复制到画布上
  318. //鼠标按下获取 开始xy开始画图
  319. // var ongoingTouches = [];
  320. var touchstart = function (e) {
  321. // console.log('🚀 ~ file: WXdraw.js ~ line 244 ~ touchstart ~ e', e);
  322. if (!isControl) {
  323. return;
  324. }
  325. const action = 0;
  326. Array.from(e.originalEvent.changedTouches).forEach(function (item, index) {
  327. const x = item.clientX - item.target.getBoundingClientRect().x;
  328. const y = item.clientY - item.target.getBoundingClientRect().y;
  329. return wsss.send(
  330. JSON.stringify({
  331. type: 'event',
  332. data: {
  333. action,
  334. count: e.originalEvent.touches.length,
  335. pointerId: item.identifier,
  336. x: (function () {
  337. return (
  338. resolving
  339. ? x * (window.currentPhoneSize.width / vowidth)
  340. : y * (window.currentPhoneSize.height / voheight)
  341. ).toFixed(2);
  342. })(),
  343. y: (function () {
  344. return (
  345. resolving
  346. ? y * (window.currentPhoneSize.height / voheight)
  347. : (vowidth - x) * (window.currentPhoneSize.width / vowidth)
  348. ).toFixed(2);
  349. })(),
  350. },
  351. }),
  352. );
  353. });
  354. canDraw = true;
  355. };
  356. //鼠标离开 把蒙版canvas的图片生成到canvas中
  357. var touchend = function (e) {
  358. if (!isControl) {
  359. return;
  360. }
  361. const action = 1;
  362. Array.from(e.originalEvent.changedTouches).forEach(function (item, index) {
  363. const x = item.clientX - item.target.getBoundingClientRect().x;
  364. const y = item.clientY - item.target.getBoundingClientRect().y;
  365. return wsss.send(
  366. JSON.stringify({
  367. type: 'event',
  368. data: {
  369. action,
  370. count: e.originalEvent.touches.length,
  371. pointerId: item.identifier,
  372. x: (function () {
  373. return (
  374. resolving
  375. ? x * (window.currentPhoneSize.width / vowidth)
  376. : y * (window.currentPhoneSize.height / voheight)
  377. ).toFixed(2);
  378. })(),
  379. y: (function () {
  380. return (
  381. resolving
  382. ? y * (window.currentPhoneSize.height / voheight)
  383. : (vowidth - x) * (window.currentPhoneSize.width / vowidth)
  384. ).toFixed(2);
  385. })(),
  386. },
  387. }),
  388. );
  389. });
  390. // var touchfor = e.originalEvent.changedTouches; //for 的手指数组
  391. // //是否横屏
  392. // for (var i = 0; i < touchfor.length; i++) {
  393. // // var acrossWidthX = touchfor[i].pageY * (videoHeight / voheight);
  394. // // var acrossHeightY =
  395. // // videoWidth - touchfor[i].pageX * (videoWidth / vowidth);
  396. // // if (resolving) {
  397. // // var verticalWidthX =
  398. // // touchfor[i].pageX * (window.currentPhoneSize.width / vowidth);
  399. // // var verticalHeightY =
  400. // // touchfor[i].pageY * (window.currentPhoneSize.height / voheight);
  401. // // } else {
  402. // // var verticalWidthX =
  403. // // touchfor[i].pageX * (window.currentPhoneSize.width / vowidth);
  404. // // var verticalHeightY =
  405. // // touchfor[i].pageY * (window.currentPhoneSize.height / voheight);
  406. // // }
  407. // var ping =
  408. // // resolving == 0 ?
  409. // // { "data": { "action": 1, "count": ongoingTouches.length, "pointerId": touchfor[i].identifier, "x": acrossWidthX.toFixed(2), "y": acrossHeightY.toFixed(2) }, "type": "event" } :
  410. // {
  411. // data: {
  412. // action: 1,
  413. // count: ongoingTouches.length,
  414. // pointerId: touchfor[i].identifier,
  415. // x: (() =>
  416. // (resolving
  417. // ? touchfor[i].pageX * (window.currentPhoneSize.width / vowidth)
  418. // : touchfor[i].pageY * (window.currentPhoneSize.width / voheight)
  419. // ).toFixed(2))(),
  420. // y: (() =>
  421. // (resolving
  422. // ? touchfor[i].pageY *
  423. // (window.currentPhoneSize.height / voheight)
  424. // : (vowidth - touchfor[i].pageX) *
  425. // (window.currentPhoneSize.height / vowidth)
  426. // ).toFixed(2))(),
  427. // },
  428. // type: 'event',
  429. // };
  430. // wsss.send(JSON.stringify(ping));
  431. // ongoingTouches.forEach(function (item, index) {
  432. // if (item.identifier === touchfor[i].identifier) {
  433. // ongoingTouches.splice(index, 1);
  434. // }
  435. // });
  436. // }
  437. canDraw = false;
  438. };
  439. //清空层 云手机超出屏幕的开关
  440. var clearContext = function () {
  441. canDraw = false;
  442. };
  443. // 鼠标移动
  444. var touchmove = function (e) {
  445. e.preventDefault();
  446. if (!isControl) {
  447. return;
  448. }
  449. const action = 2;
  450. Array.from(e.originalEvent.changedTouches).forEach(function (item, index) {
  451. const x = item.clientX - item.target.getBoundingClientRect().x;
  452. const y = item.clientY - item.target.getBoundingClientRect().y;
  453. return wsss.send(
  454. JSON.stringify({
  455. type: 'event',
  456. data: {
  457. action,
  458. count: e.originalEvent.touches.length,
  459. pointerId: item.identifier,
  460. x: (function () {
  461. return (
  462. resolving
  463. ? x * (window.currentPhoneSize.width / vowidth)
  464. : y * (window.currentPhoneSize.height / voheight)
  465. ).toFixed(2);
  466. })(),
  467. y: (function () {
  468. return (
  469. resolving
  470. ? y * (window.currentPhoneSize.height / voheight)
  471. : (vowidth - x) * (window.currentPhoneSize.width / vowidth)
  472. ).toFixed(2);
  473. })(),
  474. },
  475. }),
  476. );
  477. });
  478. // var touchfor = e.originalEvent.targetTouches; //for 的手指数组
  479. // for (var i = 0; i < touchfor.length; i++) {
  480. // // var acrossWidthX = touchfor[i].pageY * (videoHeight / voheight);
  481. // // var acrossHeightY =
  482. // // videoWidth - touchfor[i].pageX * (videoWidth / vowidth);
  483. // // let verticalWidthX = 0;
  484. // // let verticalHeightY = 0;
  485. // // if (resolving) {
  486. // // verticalWidthX =
  487. // // touchfor[i].pageX * (window.currentPhoneSize.width / vowidth);
  488. // // verticalHeightY =
  489. // // touchfor[i].pageY * (window.currentPhoneSize.height / voheight);
  490. // // } else {
  491. // // verticalWidthX =
  492. // // touchfor[i].pageX * (window.currentPhoneSize.width / vowidth);
  493. // // verticalHeightY =
  494. // // touchfor[i].pageY * (window.currentPhoneSize.height / voheight);
  495. // // }
  496. // var ping =
  497. // // resolving == 0 ?
  498. // // { "data": { "action": 2, "count": touchfor.length, "pointerId": touchfor[i].identifier, "x": acrossWidthX.toFixed(2), "y": acrossHeightY.toFixed(2) }, "type": "event" } :
  499. // {
  500. // data: {
  501. // action: 2,
  502. // count: touchfor.length,
  503. // pointerId: touchfor[i].identifier,
  504. // x: (() =>
  505. // (resolving
  506. // ? touchfor[i].pageX * (window.currentPhoneSize.width / vowidth)
  507. // : touchfor[i].pageY * (window.currentPhoneSize.width / voheight)
  508. // ).toFixed(2))(),
  509. // y: (() =>
  510. // (resolving
  511. // ? touchfor[i].pageY *
  512. // (window.currentPhoneSize.height / voheight)
  513. // : (vowidth - touchfor[i].pageX) *
  514. // (window.currentPhoneSize.height / vowidth)
  515. // ).toFixed(2))(),
  516. // },
  517. // type: 'event',
  518. // };
  519. // wsss.send(JSON.stringify(ping));
  520. // }
  521. };
  522. //鼠标离开区域以外 除了涂鸦 都清空
  523. var mouseout = function () {
  524. if (graphType != 'handwriting') {
  525. clearContext();
  526. }
  527. };
  528. $(canvas_bak).off();
  529. $(canvas_bak).on('touchstart', touchstart);
  530. $(canvas_bak).on('touchmove', touchmove);
  531. $(canvas_bak).on('touchend', touchend);
  532. $(canvas_bak).on('mouseout', mouseout);
  533. };
  534. function GetRequest() {
  535. var url = location.search; // 获取url中"?"符后的字串
  536. var obj = new Object();
  537. if (url.indexOf('?') != -1) {
  538. var str = url.substr(1);
  539. strs = str.split('&');
  540. for (var i = 0; i < strs.length; i++) {
  541. obj[strs[i].split('=')[0]] = strs[i].split('=')[1];
  542. }
  543. }
  544. return obj;
  545. }