WXdraw.js 9.7 KB

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