WXdraw.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  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;
  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. 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. errorTime = 0;
  52. // 进入发起询问
  53. var pings2 = {
  54. type: 'forwardMsg',
  55. data: {
  56. code: '3000',
  57. username,
  58. desc: '询问是否有在控制', // 可选
  59. },
  60. };
  61. wsss.send(JSON.stringify(pings2));
  62. wsss.send(
  63. JSON.stringify({
  64. type: 'getPhoneSize',
  65. }),
  66. );
  67. };
  68. wsss.onerror = function (e) {
  69. // console.log('🚀 ~ file: WXdraw.js ~ line 82 ~ onerror ~ e', e);
  70. wsss.close(1006);
  71. // throttle(doConnectDirectives, 100);
  72. // if (errorTime > 1000) {
  73. // quit();
  74. // }
  75. };
  76. wsss.onclose = function (e) {
  77. // console.log('🚀 ~ file: WXdraw.js ~ line 93 ~ onclose ~ e', e);
  78. // new WebSocket(e.)
  79. // doConnectDirectives();
  80. if (e.code === 1006) {
  81. // 异常关闭,重连
  82. throttleDoConnectDirectives();
  83. // doConnectDirectives();
  84. // throttle(doConnectDirectives, 100);
  85. if (errorTime > 1000) {
  86. quit();
  87. }
  88. }
  89. };
  90. wsss.onmessage = function (res) {
  91. var result = typeof res.data === 'string' ? JSON.parse(res.data) : res.data;
  92. console.log(
  93. '🚀 ~ file: WXdraw.js ~ line 78 ~ doConnectDirectives ~ result',
  94. result,
  95. );
  96. if (result.type === 'cutting') {
  97. if (result.data.status === 0) {
  98. $.toast('复制成功', 'text', 1000);
  99. } else {
  100. $.toast(result.msg, 'text', 1000);
  101. }
  102. return;
  103. }
  104. if (result.type === 'forwardMsgRep') {
  105. // 当前云机无其他终端在线,获得控制权
  106. changIsControl(true);
  107. }
  108. if (result.type === 'forwardMsg') {
  109. /**
  110. * @type {boolean} isControl 当前是否拥有控制权,初始化时为false
  111. * @type {string} isAuth 当前云机类型 - huo: 获取的云机,none: 自己的云机
  112. * @type {string} username 当前登录的双子星账号username
  113. */
  114. if (result.data.username !== username) {
  115. switch (String(result.data.code)) {
  116. case '3000': {
  117. if (isControl) {
  118. // 回复有控制
  119. wsss.send(
  120. JSON.stringify({
  121. type: 'forwardMsg',
  122. data: {
  123. code: '4000',
  124. username,
  125. desc: '有控制', // 可选
  126. },
  127. }),
  128. );
  129. return;
  130. }
  131. // 回复有观看
  132. wsss.send(
  133. JSON.stringify({
  134. type: 'forwardMsg',
  135. data: {
  136. code: '4100',
  137. username,
  138. desc: '有观看', // 可选
  139. },
  140. }),
  141. );
  142. return;
  143. }
  144. case '4000': {
  145. // 当前是获取方
  146. if (isAuth === 'huo' && isControl) {
  147. $.confirm(
  148. '授权方已收回控制权,您进入观看屏幕模式',
  149. function () {
  150. //点击确认后的回调函数
  151. changIsControl(false);
  152. },
  153. function () {
  154. changIsControl(false);
  155. //点击取消后的回调函数
  156. quit();
  157. },
  158. );
  159. return;
  160. }
  161. if (!isControl && isAuth === 'shou') {
  162. // 当前是授权方切没有控制权
  163. $.confirm(
  164. '当前云手机正在授控,是否请求获取云手机控制权?',
  165. function () {
  166. //点击确认后的回调函数
  167. wsss.send(
  168. JSON.stringify({
  169. type: 'forwardMsg',
  170. data: {
  171. code: '5000',
  172. username,
  173. desc: '控制权限收回', // 可选
  174. },
  175. }),
  176. );
  177. changIsControl(true);
  178. },
  179. function () {
  180. //点击取消后的回调函数
  181. changIsControl(false);
  182. },
  183. );
  184. return;
  185. }
  186. }
  187. case '5000': {
  188. // if (result.data.username === username) {
  189. // changIsControl(true);
  190. // return;
  191. // }
  192. if (isAuth === 'huo' && isControl) {
  193. $.confirm(
  194. '授权方已收回控制权,您进入观看屏幕模式',
  195. function () {
  196. //点击确认后的回调函数
  197. changIsControl(false);
  198. },
  199. function () {
  200. changIsControl(false);
  201. //点击取消后的回调函数
  202. quit();
  203. },
  204. );
  205. return;
  206. }
  207. return;
  208. }
  209. default: {
  210. return;
  211. }
  212. }
  213. }
  214. return;
  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. if (
  249. window.currentPhoneSize &&
  250. (window.currentPhoneSize.width !==
  251. Math.min(result.data.width, result.data.height) ||
  252. window.currentPhoneSize.height !==
  253. Math.max(result.data.width, result.data.height) ||
  254. window.currentPhoneSize.dpi !== result.data.dpi)
  255. ) {
  256. // 获取到的分辨率与当前分辨率不符
  257. const data = window.phoneSizeList.find(function (v) {
  258. return (
  259. v.width === Math.min(result.data.width, result.data.height) &&
  260. v.height === Math.max(result.data.width, result.data.height) &&
  261. v.dpi === result.data.dpi
  262. );
  263. });
  264. window.currentPhoneSize = data || {
  265. width: Math.min(result.data.width, result.data.height),
  266. height: Math.max(result.data.width, result.data.height),
  267. dpi: result.data.dpi,
  268. };
  269. if (result.type === 'setPhoneSize') {
  270. lastSetPhone = Date.now();
  271. }
  272. // if (result.type === 'getPhoneSize') {
  273. // 上报给后端
  274. $.ajax({
  275. url:
  276. baseUrl +
  277. '/api/resources/v5/machine/resolution/operationResolvingPower',
  278. headers: {
  279. Authorization: token,
  280. },
  281. type: 'post',
  282. dataType: 'json',
  283. contentType: 'application/json; charset=UTF-8',
  284. data: JSON.stringify({
  285. userCardId: window.userCardId,
  286. resolvingPowerId: window.currentPhoneSize.id,
  287. }),
  288. });
  289. }
  290. // }
  291. updateDB(db, storeName, {
  292. id: userCardId,
  293. socketURL: socketURL,
  294. cUrl: cUrl,
  295. cardToken: cardToken,
  296. resolvingPower: resolvingPower,
  297. width: window.currentPhoneSize.width,
  298. height: window.currentPhoneSize.height,
  299. dpi: window.currentPhoneSize.dpi,
  300. });
  301. return;
  302. }
  303. if (result.type === 'reProduceText') {
  304. // 接收到云机剪贴板复制事件
  305. window.copyToClipboard(result.data.text);
  306. }
  307. };
  308. }
  309. $('body').on('click', function () {
  310. draw_graph('pencil');
  311. });
  312. //剪切板
  313. $('.upload').on('click', function () {
  314. var texts = $(this).attr('data-text');
  315. if (texts == 'uploads') {
  316. $('.mainbox').css({
  317. display: 'block',
  318. });
  319. $('.sbox').css({
  320. display: 'none',
  321. });
  322. }
  323. });
  324. //home 控制home
  325. $('.botmat1img').on('click', function () {
  326. var codes = $(this).attr('data-text');
  327. if (codes == 'home' && isControl) {
  328. wsss.send(ExexuteKeyBoard(3));
  329. } else if (codes == 'return' && isControl) {
  330. wsss.send(ExexuteKeyBoard(4));
  331. } else if (codes == 'gengduo' && isControl) {
  332. wsss.send(ExexuteKeyBoard(187));
  333. }
  334. });
  335. // 高清控制
  336. $('.PictureQuality').on('click', function () {
  337. if (!isControl) {
  338. return;
  339. }
  340. $(this).addClass('avit').siblings().removeClass('avit');
  341. var id = $(this).attr('data-id');
  342. var buffer = makeSharpness(Number(id));
  343. ws.send(buffer);
  344. });
  345. //画图形
  346. var draw_graph = function (graphType, obj) {
  347. //把蒙版放于画板上面
  348. $('#container').css('z-index', 30);
  349. $('#dedit').css('z-index', 20);
  350. var canDraw = false;
  351. //鼠标按下获取 开始xy开始画图
  352. // var ongoingTouches = [];
  353. var touchstart = function (e) {
  354. if (!isControl) {
  355. return;
  356. }
  357. const action = 0;
  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. canDraw = true;
  387. };
  388. //鼠标离开 把蒙版canvas的图片生成到canvas中
  389. var touchend = function (e) {
  390. if (!isControl) {
  391. return;
  392. }
  393. const action = 1;
  394. Array.from(e.originalEvent.changedTouches).forEach(function (item, index) {
  395. const x = item.clientX - item.target.getBoundingClientRect().x;
  396. const y = item.clientY - item.target.getBoundingClientRect().y;
  397. return wsss.send(
  398. JSON.stringify({
  399. type: 'event',
  400. data: {
  401. action,
  402. count: e.originalEvent.touches.length,
  403. pointerId: item.identifier,
  404. x: (function () {
  405. return (
  406. resolving
  407. ? x * (window.currentPhoneSize.width / vowidth)
  408. : y * (window.currentPhoneSize.height / voheight)
  409. ).toFixed(2);
  410. })(),
  411. y: (function () {
  412. return (
  413. resolving
  414. ? y * (window.currentPhoneSize.height / voheight)
  415. : (vowidth - x) * (window.currentPhoneSize.width / vowidth)
  416. ).toFixed(2);
  417. })(),
  418. },
  419. }),
  420. );
  421. });
  422. canDraw = false;
  423. };
  424. //清空层 云手机超出屏幕的开关
  425. var clearContext = function () {
  426. canDraw = false;
  427. };
  428. // 鼠标移动
  429. var touchmove = function (e) {
  430. e.preventDefault();
  431. if (!isControl) {
  432. return;
  433. }
  434. const action = 2;
  435. Array.from(e.originalEvent.changedTouches).forEach(function (item, index) {
  436. const x = item.clientX - item.target.getBoundingClientRect().x;
  437. const y = item.clientY - item.target.getBoundingClientRect().y;
  438. return wsss.send(
  439. JSON.stringify({
  440. type: 'event',
  441. data: {
  442. action,
  443. count: e.originalEvent.touches.length,
  444. pointerId: item.identifier,
  445. x: (function () {
  446. return (
  447. resolving
  448. ? x * (window.currentPhoneSize.width / vowidth)
  449. : y * (window.currentPhoneSize.height / voheight)
  450. ).toFixed(2);
  451. })(),
  452. y: (function () {
  453. return (
  454. resolving
  455. ? y * (window.currentPhoneSize.height / voheight)
  456. : (vowidth - x) * (window.currentPhoneSize.width / vowidth)
  457. ).toFixed(2);
  458. })(),
  459. },
  460. }),
  461. );
  462. });
  463. };
  464. //鼠标离开区域以外 除了涂鸦 都清空
  465. var mouseout = function () {
  466. if (graphType != 'handwriting') {
  467. clearContext();
  468. }
  469. };
  470. $(canvas_bak).off();
  471. $(canvas_bak).on('touchstart', touchstart);
  472. $(canvas_bak).on('touchmove', touchmove);
  473. $(canvas_bak).on('touchend', touchend);
  474. $(canvas_bak).on('mouseout', mouseout);
  475. };
  476. // 获取url中"?"符后的字串
  477. function GetRequest() {
  478. var url = location.search;
  479. var obj = new Object();
  480. if (url.indexOf('?') != -1) {
  481. var str = url.substr(1);
  482. strs = str.split('&');
  483. for (var i = 0; i < strs.length; i++) {
  484. obj[strs[i].split('=')[0]] = strs[i].split('=')[1];
  485. }
  486. }
  487. return obj;
  488. }