WXdraw.js 10 KB

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