WXdraw.js 17 KB

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