WXdraw.js 13 KB

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