WXdraw.js 17 KB

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