WXdraw.js 15 KB

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