WXdraw.js 11 KB

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