WXdraw.js 11 KB

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