WXdraw.js 9.2 KB

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