WXdraw.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  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. var isAuth = parameters['authPhone']; // 是否是获取的云手机
  21. var wsss,
  22. errorTime = 0;
  23. var first = true;
  24. function throttle(fn, delay) {
  25. var flag = true;
  26. errorTime += delay;
  27. return () => {
  28. if (!flag) return;
  29. flag = false;
  30. timer = setTimeout(() => {
  31. fn();
  32. flag = true;
  33. }, delay);
  34. };
  35. }
  36. function doConnectDirectives() {
  37. videoWidth = Number(resolvingPower) ? Number(resolvingPower) : 720;
  38. videoHeight = videoWidth === 720 ? 1280 : 1920;
  39. wsss = new WebSocket(cUrl);
  40. wsss.binaryType = 'arraybuffer';
  41. wsss.onopen = function () {
  42. // 进入发起询问
  43. var pings2 = {
  44. type: 'forwardMsg',
  45. data: {
  46. code: '3000',
  47. desc: '询问是否有在控制', // 可选
  48. },
  49. };
  50. wsss.send(JSON.stringify(pings2));
  51. wsss.send(
  52. JSON.stringify({
  53. type: 'getPhoneSize',
  54. }),
  55. );
  56. };
  57. wsss.onerror = function (evt) {
  58. wsss.close();
  59. throttle(doConnectDirectives, 100);
  60. if (errorTime > 1000) {
  61. quit();
  62. }
  63. };
  64. wsss.onmessage = function (res) {
  65. console.log('res', res);
  66. var result = typeof res.data === 'string' ? JSON.parse(res.data) : res.data;
  67. console.log('result.type', result.type);
  68. if (result.type === 'cutting') {
  69. if (result.data.status === 0) {
  70. $.toast('复制成功', 'text');
  71. } else {
  72. $.toast(result.msg, 'text');
  73. }
  74. return;
  75. }
  76. if (result.type === 'forwardMsg') {
  77. if (isAuth !== 'none') {
  78. if (result.data.code === 4000 || result.data.code === 3000) {
  79. if (isAuth === 'huo') {
  80. $.confirm(
  81. '授权方已收回控制权,您进入观看屏幕模式',
  82. function () {
  83. //点击确认后的回调函数
  84. isControl = false;
  85. },
  86. function () {
  87. isControl = false;
  88. //点击取消后的回调函数
  89. quit();
  90. },
  91. );
  92. } else {
  93. $.confirm(
  94. '当前云手机正在授控,是否请求获取云手机控制权?',
  95. function () {
  96. //点击确认后的回调函数
  97. var ping = {
  98. type: 'forwardMsg',
  99. data: {
  100. code: '5000',
  101. desc: '控制权限收回', // 可选
  102. },
  103. };
  104. wsss.send(JSON.stringify(ping));
  105. isControl = true;
  106. },
  107. function () {
  108. //点击取消后的回调函数
  109. isControl = false;
  110. },
  111. );
  112. }
  113. return;
  114. }
  115. if (result.data.code === 'phoneSizeChange') {
  116. console.log(result.data);
  117. window.currentPhoneSize = {
  118. width: result.data.width,
  119. height: result.data.height,
  120. };
  121. return;
  122. }
  123. return;
  124. }
  125. return;
  126. }
  127. if (result.type === 'payInitiateEvent') {
  128. var url = window.location.href;
  129. url = url.split('/');
  130. var baseUrl = url[0] + '//' + url[2];
  131. $.ajax({
  132. url: baseUrl + '/api/pay/third/order',
  133. data: JSON.stringify({
  134. orderNum: result.data.orderNum,
  135. orderAmount: result.data.orderAmount,
  136. appKey: result.data.appKey,
  137. }),
  138. type: 'POST',
  139. dataType: 'json',
  140. contentType: 'application/json;charset=UTF-8',
  141. success: function (res) {
  142. if (result.data.payType === 1) {
  143. // 微信
  144. if (window.__wxjs_environment === 'miniprogram') {
  145. // 小程序
  146. // copyUrl(result.data.payUrl);
  147. } else {
  148. window.location.href = result.data.payUrl;
  149. }
  150. } else {
  151. window.location.href = result.data.payUrl;
  152. }
  153. },
  154. });
  155. return;
  156. }
  157. if (result.type === 'getPhoneSize') {
  158. console.log(result);
  159. window.currentPhoneSize = {
  160. width: result.data.width,
  161. height: result.data.height,
  162. };
  163. return;
  164. }
  165. };
  166. }
  167. $('body').on('click', function () {
  168. draw_graph('pencil');
  169. });
  170. //剪切板
  171. $('.upload').on('click', function () {
  172. var texts = $(this).attr('data-text');
  173. if (texts == 'uploads') {
  174. $('.mainbox').css({
  175. display: 'block',
  176. });
  177. $('.sbox').css({
  178. display: 'none',
  179. });
  180. }
  181. });
  182. //home 控制home
  183. $('.botmat1img').on('click', function () {
  184. var codes = $(this).attr('data-text');
  185. if (codes == 'home' && isControl) {
  186. wsss.send(ExexuteKeyBoard(3));
  187. } else if (codes == 'return' && isControl) {
  188. wsss.send(ExexuteKeyBoard(4));
  189. } else if (codes == 'gengduo' && isControl) {
  190. wsss.send(ExexuteKeyBoard(187));
  191. }
  192. });
  193. // 高清控制
  194. $('.PictureQuality').on('click', function () {
  195. if (!isControl) {
  196. return;
  197. }
  198. $(this).addClass('avit').siblings().removeClass('avit');
  199. var id = $(this).attr('data-id');
  200. var buffer = makeSharpness(Number(id));
  201. ws.send(buffer);
  202. });
  203. //画图形
  204. var draw_graph = function (graphType, obj) {
  205. //把蒙版放于画板上面
  206. $('#container').css('z-index', 30);
  207. $('#dedit').css('z-index', 20);
  208. var canDraw = false;
  209. //鼠标按下获取 开始xy开始画图
  210. var ongoingTouches = [];
  211. var touchstart = function (e) {
  212. if (!isControl) {
  213. return;
  214. }
  215. $('.control-right-img').attr({
  216. 'data-id': '2',
  217. });
  218. $('.leftmains').css({
  219. right: '-4rem',
  220. });
  221. var touchfor = e.originalEvent.changedTouches; //for 的手指数组
  222. //是否横屏
  223. for (var i = 0; i < touchfor.length; i++) {
  224. var acrossWidthX = touchfor[i].pageY * (videoHeight / voheight);
  225. var acrossHeightY =
  226. videoWidth - touchfor[i].pageX * (videoWidth / vowidth);
  227. var verticalWidthX = touchfor[i].pageX * (videoWidth / vowidth);
  228. var verticalHeightY = touchfor[i].pageY * (videoHeight / voheight);
  229. var idx = ongoingTouches.findIndex(function (ele) {
  230. return ele.identifier === touchfor[i].identifier;
  231. });
  232. if (idx < 0) {
  233. ongoingTouches.push(touchfor[i]);
  234. }
  235. var ping =
  236. resolving == 0
  237. ? {
  238. data: {
  239. action: 0,
  240. count: ongoingTouches.length,
  241. pointerId: touchfor[i].identifier,
  242. x: acrossWidthX.toFixed(2),
  243. y: acrossHeightY.toFixed(2),
  244. },
  245. type: 'event',
  246. }
  247. : {
  248. data: {
  249. action: 0,
  250. count: ongoingTouches.length,
  251. pointerId: touchfor[i].identifier,
  252. x: verticalWidthX.toFixed(2),
  253. y: verticalHeightY.toFixed(2),
  254. },
  255. type: 'event',
  256. };
  257. wsss.send(JSON.stringify(ping));
  258. }
  259. canDraw = true;
  260. };
  261. //鼠标离开 把蒙版canvas的图片生成到canvas中
  262. var touchend = function (e) {
  263. if (!isControl) {
  264. return;
  265. }
  266. var touchfor = e.originalEvent.changedTouches; //for 的手指数组
  267. //是否横屏
  268. for (var i = 0; i < touchfor.length; i++) {
  269. var acrossWidthX = touchfor[i].pageY * (videoHeight / voheight);
  270. var acrossHeightY =
  271. videoWidth - touchfor[i].pageX * (videoWidth / vowidth);
  272. var verticalWidthX = touchfor[i].pageX * (videoWidth / vowidth);
  273. var verticalHeightY = touchfor[i].pageY * (videoHeight / voheight);
  274. var ping =
  275. resolving == 0
  276. ? {
  277. data: {
  278. action: 1,
  279. count: ongoingTouches.length,
  280. pointerId: touchfor[i].identifier,
  281. x: acrossWidthX.toFixed(2),
  282. y: acrossHeightY.toFixed(2),
  283. },
  284. type: 'event',
  285. }
  286. : {
  287. data: {
  288. action: 1,
  289. count: ongoingTouches.length,
  290. pointerId: touchfor[i].identifier,
  291. x: verticalWidthX.toFixed(2),
  292. y: verticalHeightY.toFixed(2),
  293. },
  294. type: 'event',
  295. };
  296. wsss.send(JSON.stringify(ping));
  297. ongoingTouches.forEach(function (item, index) {
  298. if (item.identifier === touchfor[i].identifier) {
  299. ongoingTouches.splice(index, 1);
  300. }
  301. });
  302. }
  303. canDraw = false;
  304. };
  305. //清空层 云手机超出屏幕的开关
  306. var clearContext = function () {
  307. canDraw = false;
  308. };
  309. // 鼠标移动
  310. var touchmove = function (e) {
  311. if (!isControl) {
  312. return;
  313. }
  314. var touchfor = e.originalEvent.targetTouches; //for 的手指数组
  315. for (var i = 0; i < touchfor.length; i++) {
  316. var acrossWidthX = touchfor[i].pageY * (videoHeight / voheight);
  317. var acrossHeightY =
  318. videoWidth - touchfor[i].pageX * (videoWidth / vowidth);
  319. var verticalWidthX = touchfor[i].pageX * (videoWidth / vowidth);
  320. var verticalHeightY = touchfor[i].pageY * (videoHeight / voheight);
  321. var ping =
  322. resolving == 0
  323. ? {
  324. data: {
  325. action: 2,
  326. count: touchfor.length,
  327. pointerId: touchfor[i].identifier,
  328. x: acrossWidthX.toFixed(2),
  329. y: acrossHeightY.toFixed(2),
  330. },
  331. type: 'event',
  332. }
  333. : {
  334. data: {
  335. action: 2,
  336. count: touchfor.length,
  337. pointerId: touchfor[i].identifier,
  338. x: verticalWidthX.toFixed(2),
  339. y: verticalHeightY.toFixed(2),
  340. },
  341. type: 'event',
  342. };
  343. wsss.send(JSON.stringify(ping));
  344. }
  345. };
  346. //鼠标离开区域以外 除了涂鸦 都清空
  347. var mouseout = function () {
  348. if (graphType != 'handwriting') {
  349. clearContext();
  350. }
  351. };
  352. $(canvas_bak).unbind();
  353. $(canvas_bak).bind('touchstart', touchstart);
  354. $(canvas_bak).bind('touchmove', touchmove);
  355. $(canvas_bak).bind('touchend', touchend);
  356. $(canvas_bak).bind('mouseout', mouseout);
  357. };
  358. // 获取url中"?"符后的字串
  359. function GetRequest() {
  360. var url = location.search;
  361. var obj = new Object();
  362. if (url.indexOf('?') != -1) {
  363. var str = url.substr(1);
  364. strs = str.split('&');
  365. for (var i = 0; i < strs.length; i++) {
  366. obj[strs[i].split('=')[0]] = strs[i].split('=')[1];
  367. }
  368. }
  369. return obj;
  370. }