WXdraw.js 9.3 KB

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