WXdraw.js 15 KB

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