WXdraw.js 11 KB

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