WXdraw.js 9.5 KB

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