WXdraw.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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. console.log('res', res);
  72. var result = typeof res.data === 'string' ? JSON.parse(res.data) : res.data;
  73. console.log('result.type', result.type);
  74. if (result.type === 'cutting') {
  75. if (result.data.status === 0) {
  76. $.toast('复制成功', 'text');
  77. } else {
  78. $.toast(result.msg, 'text');
  79. }
  80. return;
  81. }
  82. if (result.type === 'forwardMsg') {
  83. switch (String(result.data.code)) {
  84. case '3000': {
  85. }
  86. case '4000': {
  87. if (isAuth === 'huo') {
  88. $.confirm(
  89. '授权方已收回控制权,您进入观看屏幕模式',
  90. function () {
  91. //点击确认后的回调函数
  92. changIsControl(false);
  93. },
  94. function () {
  95. changIsControl(false);
  96. //点击取消后的回调函数
  97. quit();
  98. },
  99. );
  100. return;
  101. }
  102. $.confirm(
  103. '当前云手机正在授控,是否请求获取云手机控制权?',
  104. function () {
  105. //点击确认后的回调函数
  106. var ping = {
  107. type: 'forwardMsg',
  108. data: {
  109. code: '5000',
  110. desc: '控制权限收回', // 可选
  111. },
  112. };
  113. wsss.send(JSON.stringify(ping));
  114. changIsControl(true);
  115. },
  116. function () {
  117. //点击取消后的回调函数
  118. changIsControl(false);
  119. },
  120. );
  121. return;
  122. }
  123. case '5000': {
  124. console.log(result);
  125. $.confirm(
  126. '授权方已收回控制权,您进入观看屏幕模式',
  127. function () {
  128. //点击确认后的回调函数
  129. changIsControl(false);
  130. },
  131. function () {
  132. changIsControl(false);
  133. //点击取消后的回调函数
  134. quit();
  135. },
  136. );
  137. return;
  138. }
  139. case 'phoneSizeChange': {
  140. window.currentPhoneSize = {
  141. id: result.data.id,
  142. width: result.data.width,
  143. height: result.data.height,
  144. pid: result.data.pid,
  145. };
  146. return;
  147. }
  148. default: {
  149. return;
  150. }
  151. }
  152. }
  153. if (result.type === 'payInitiateEvent') {
  154. var url = window.location.href;
  155. url = url.split('/');
  156. var baseUrl = url[0] + '//' + url[2];
  157. $.ajax({
  158. url: baseUrl + '/api/pay/third/order',
  159. data: JSON.stringify({
  160. orderNum: result.data.orderNum,
  161. orderAmount: result.data.orderAmount,
  162. appKey: result.data.appKey,
  163. }),
  164. type: 'POST',
  165. dataType: 'json',
  166. contentType: 'application/json;charset=UTF-8',
  167. success: function (res) {
  168. if (result.data.payType === 1) {
  169. // 微信
  170. if (window.__wxjs_environment === 'miniprogram') {
  171. // 小程序
  172. // copyUrl(result.data.payUrl);
  173. } else {
  174. window.location.href = result.data.payUrl;
  175. }
  176. } else {
  177. window.location.href = result.data.payUrl;
  178. }
  179. },
  180. });
  181. return;
  182. }
  183. if (result.type === 'getPhoneSize') {
  184. // console.log(result);
  185. const data = window.phoneSizeList.find(function (v) {
  186. return (
  187. v.width === result.data.width &&
  188. v.height === result.data.height &&
  189. v.pid === result.data.pid
  190. );
  191. });
  192. window.currentPhoneSize = data || {
  193. width: result.data.width,
  194. height: result.data.height,
  195. pid: result.data.pid,
  196. };
  197. return;
  198. }
  199. };
  200. }
  201. $('body').on('click', function () {
  202. draw_graph('pencil');
  203. });
  204. //剪切板
  205. $('.upload').on('click', function () {
  206. var texts = $(this).attr('data-text');
  207. if (texts == 'uploads') {
  208. $('.mainbox').css({
  209. display: 'block',
  210. });
  211. $('.sbox').css({
  212. display: 'none',
  213. });
  214. }
  215. });
  216. //home 控制home
  217. $('.botmat1img').on('click', function () {
  218. var codes = $(this).attr('data-text');
  219. if (codes == 'home' && isControl) {
  220. wsss.send(ExexuteKeyBoard(3));
  221. } else if (codes == 'return' && isControl) {
  222. wsss.send(ExexuteKeyBoard(4));
  223. } else if (codes == 'gengduo' && isControl) {
  224. wsss.send(ExexuteKeyBoard(187));
  225. }
  226. });
  227. // 高清控制
  228. $('.PictureQuality').on('click', function () {
  229. if (!isControl) {
  230. return;
  231. }
  232. $(this).addClass('avit').siblings().removeClass('avit');
  233. var id = $(this).attr('data-id');
  234. var buffer = makeSharpness(Number(id));
  235. ws.send(buffer);
  236. });
  237. //画图形
  238. var draw_graph = function (graphType, obj) {
  239. //把蒙版放于画板上面
  240. $('#container').css('z-index', 30);
  241. $('#dedit').css('z-index', 20);
  242. var canDraw = false;
  243. //鼠标按下获取 开始xy开始画图
  244. // var ongoingTouches = [];
  245. var touchstart = function (e) {
  246. if (!isControl) {
  247. return;
  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. }