WXdraw.js 13 KB

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