WXdraw.js 14 KB

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