WXdraw.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  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 = true; // 是否是观看模式
  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. 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 (isControl && 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) {
  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. return;
  247. }
  248. };
  249. }
  250. $('body').on('click', function () {
  251. draw_graph('pencil');
  252. });
  253. //剪切板
  254. $('.upload').on('click', function () {
  255. var texts = $(this).attr('data-text');
  256. if (texts == 'uploads') {
  257. $('.mainbox').css({
  258. display: 'block',
  259. });
  260. $('.sbox').css({
  261. display: 'none',
  262. });
  263. }
  264. });
  265. //home 控制home
  266. $('.botmat1img').on('click', function () {
  267. var codes = $(this).attr('data-text');
  268. if (codes == 'home' && isControl) {
  269. wsss.send(ExexuteKeyBoard(3));
  270. } else if (codes == 'return' && isControl) {
  271. wsss.send(ExexuteKeyBoard(4));
  272. } else if (codes == 'gengduo' && isControl) {
  273. wsss.send(ExexuteKeyBoard(187));
  274. }
  275. });
  276. // 高清控制
  277. $('.PictureQuality').on('click', function () {
  278. if (!isControl) {
  279. return;
  280. }
  281. $(this).addClass('avit').siblings().removeClass('avit');
  282. var id = $(this).attr('data-id');
  283. var buffer = makeSharpness(Number(id));
  284. ws.send(buffer);
  285. });
  286. //画图形
  287. var draw_graph = function (graphType, obj) {
  288. //把蒙版放于画板上面
  289. $('#container').css('z-index', 30);
  290. $('#dedit').css('z-index', 20);
  291. var canDraw = false;
  292. //鼠标按下获取 开始xy开始画图
  293. // var ongoingTouches = [];
  294. var touchstart = function (e) {
  295. if (!isControl) {
  296. return;
  297. }
  298. const action = 0;
  299. Array.from(e.originalEvent.changedTouches).forEach(function (item, index) {
  300. const x = item.clientX - item.target.getBoundingClientRect().x;
  301. const y = item.clientY - item.target.getBoundingClientRect().y;
  302. return wsss.send(
  303. JSON.stringify({
  304. type: 'event',
  305. data: {
  306. action,
  307. count: e.originalEvent.touches.length,
  308. pointerId: item.identifier,
  309. x: (function () {
  310. return (
  311. resolving
  312. ? x * (window.currentPhoneSize.width / vowidth)
  313. : y * (window.currentPhoneSize.height / voheight)
  314. ).toFixed(2);
  315. })(),
  316. y: (function () {
  317. return (
  318. resolving
  319. ? y * (window.currentPhoneSize.height / voheight)
  320. : (vowidth - x) * (window.currentPhoneSize.width / vowidth)
  321. ).toFixed(2);
  322. })(),
  323. },
  324. }),
  325. );
  326. });
  327. canDraw = true;
  328. };
  329. //鼠标离开 把蒙版canvas的图片生成到canvas中
  330. var touchend = function (e) {
  331. if (!isControl) {
  332. return;
  333. }
  334. const action = 1;
  335. Array.from(e.originalEvent.changedTouches).forEach(function (item, index) {
  336. const x = item.clientX - item.target.getBoundingClientRect().x;
  337. const y = item.clientY - item.target.getBoundingClientRect().y;
  338. return wsss.send(
  339. JSON.stringify({
  340. type: 'event',
  341. data: {
  342. action,
  343. count: e.originalEvent.touches.length,
  344. pointerId: item.identifier,
  345. x: (function () {
  346. return (
  347. resolving
  348. ? x * (window.currentPhoneSize.width / vowidth)
  349. : y * (window.currentPhoneSize.height / voheight)
  350. ).toFixed(2);
  351. })(),
  352. y: (function () {
  353. return (
  354. resolving
  355. ? y * (window.currentPhoneSize.height / voheight)
  356. : (vowidth - x) * (window.currentPhoneSize.width / vowidth)
  357. ).toFixed(2);
  358. })(),
  359. },
  360. }),
  361. );
  362. });
  363. canDraw = false;
  364. };
  365. //清空层 云手机超出屏幕的开关
  366. var clearContext = function () {
  367. canDraw = false;
  368. };
  369. // 鼠标移动
  370. var touchmove = function (e) {
  371. e.preventDefault();
  372. if (!isControl) {
  373. return;
  374. }
  375. const action = 2;
  376. Array.from(e.originalEvent.changedTouches).forEach(function (item, index) {
  377. const x = item.clientX - item.target.getBoundingClientRect().x;
  378. const y = item.clientY - item.target.getBoundingClientRect().y;
  379. return wsss.send(
  380. JSON.stringify({
  381. type: 'event',
  382. data: {
  383. action,
  384. count: e.originalEvent.touches.length,
  385. pointerId: item.identifier,
  386. x: (function () {
  387. return (
  388. resolving
  389. ? x * (window.currentPhoneSize.width / vowidth)
  390. : y * (window.currentPhoneSize.height / voheight)
  391. ).toFixed(2);
  392. })(),
  393. y: (function () {
  394. return (
  395. resolving
  396. ? y * (window.currentPhoneSize.height / voheight)
  397. : (vowidth - x) * (window.currentPhoneSize.width / vowidth)
  398. ).toFixed(2);
  399. })(),
  400. },
  401. }),
  402. );
  403. });
  404. };
  405. //鼠标离开区域以外 除了涂鸦 都清空
  406. var mouseout = function () {
  407. if (graphType != 'handwriting') {
  408. clearContext();
  409. }
  410. };
  411. $(canvas_bak).off();
  412. $(canvas_bak).on('touchstart', touchstart);
  413. $(canvas_bak).on('touchmove', touchmove);
  414. $(canvas_bak).on('touchend', touchend);
  415. $(canvas_bak).on('mouseout', mouseout);
  416. };
  417. // 获取url中"?"符后的字串
  418. function GetRequest() {
  419. var url = location.search;
  420. var obj = new Object();
  421. if (url.indexOf('?') != -1) {
  422. var str = url.substr(1);
  423. strs = str.split('&');
  424. for (var i = 0; i < strs.length; i++) {
  425. obj[strs[i].split('=')[0]] = strs[i].split('=')[1];
  426. }
  427. }
  428. return obj;
  429. }