WXdraw.js 12 KB

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