WXdraw.js 15 KB

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