WXdraw.js 11 KB

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