WXdraw.js 13 KB

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