demo.html 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  6. <title>Document</title>
  7. <style>
  8. .click {
  9. width: 200px;
  10. height: 60px;
  11. border: 1px solid #ccc;
  12. margin: 0 auto;
  13. margin-bottom: 10px;
  14. line-height: 60px;
  15. text-align: center;
  16. border-radius: 10px;
  17. }
  18. </style>
  19. <script src="https://cdn.bootcss.com/crypto-js/4.0.0/crypto-js.min.js" type="text/javascript"></script>
  20. <script src="https://cdn.staticfile.org/jquery/3.5.1/jquery.min.js" type="text/javascript"></script>
  21. </head>
  22. <body>
  23. <button id="login">登录</button>
  24. <button id="config">获取配置</button>
  25. <button id="start">开始游戏</button>
  26. <button id="end">游戏结束</button>
  27. <button id="topList">排行榜</button>
  28. <button id="reward">抽奖</button>
  29. <button id="saveAddress">上报地址</button>
  30. <button id="sms">发送短信验证码</button>
  31. <input type="text" name="mobile" id="mobile" value="" />
  32. <input type="text" name="code" id="code" value="" />
  33. <button id="copy">测试复制</button>
  34. <div id="data"></div>
  35. <div id="loginResult"></div>
  36. <div id="configResult"></div>
  37. <div id="startResult"></div>
  38. <div id="endResult"></div>
  39. <div id="topListResult"></div>
  40. <div id="rewardResult"></div>
  41. <div id="saveAddressResult"></div>
  42. <div id="smsResult"></div>
  43. <input style="display: absolute; z-index: -1; height: 0; width: 0" id="copyTest" />
  44. <script type="text/javascript">
  45. var ua = navigator.userAgent.toLocaleUpperCase().indexOf("WEIBO") >= 0;
  46. function setCookie(cname, cvalue, exdays) {
  47. var d = new Date();
  48. d.setTime(d.getTime() + exdays * 24 * 60 * 60 * 1000);
  49. var expires = "expires=" + d.toGMTString();
  50. document.cookie = cname + "=" + cvalue + "; " + expires;
  51. }
  52. //获取cookie
  53. function getCookie(cname) {
  54. var name = cname + "=";
  55. var ca = document.cookie.split(";");
  56. for (var i = 0; i < ca.length; i++) {
  57. var c = ca[i].trim();
  58. if (c.indexOf(name) == 0) return c.substring(name.length, c.length);
  59. }
  60. return "";
  61. }
  62. function getQueryString(name) {
  63. let reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
  64. let r = window.location.search.substr(1).match(reg);
  65. if (r != null) return unescape(r[2]);
  66. return null;
  67. }
  68. if (!ua) {
  69. let url = "http://www.weibo.cn/demo.html"; // 要打开的 H5 页面的地址
  70. let scheme = `sinaweibo://browser?url=` + encodeURIComponent(url);
  71. window.location.href = `https://m.weibo.cn/feature/openapp?scheme=` + encodeURIComponent(scheme);
  72. } else {
  73. if (getCookie("SUB") == "") {
  74. setCookie("SUB", getQueryString("sub"), 7);
  75. // alert("未登录");
  76. // let url = "http://www.weibo.cn/demo.html"; // 要打开的 H5 页面的地址
  77. // //未登录状态下跳转 scheme:
  78. // window.location.href = `sinaweibo://login?redirect_scheme` + encodeURIComponent(url);
  79. }
  80. $("#login").click(function () {
  81. getCode("1");
  82. // callNative('sendCode', '');
  83. });
  84. $("#copy").click(function () {
  85. document.getElementById("copyTest").val(new Date());
  86. document.getElementById("copyTest").select();
  87. if (document.execCommand("copy")) {
  88. document.execCommand("copy");
  89. alert("复制成功");
  90. }
  91. });
  92. // 判断平台
  93. var u = navigator.userAgent;
  94. var isAndroid = u.indexOf("Android") > -1 || u.indexOf("Adr") > -1; //android终端
  95. var isiOS = !!u.match(/(iPhone|iPad|iPod)/gi); //ios终端
  96. var isWeChat = u.toLowerCase().match(/MicroMessenger/i) == "micromessenger"; //微信终端
  97. function getInfo(data) {
  98. // 暴露公用方法 ios 安卓调用H5
  99. dataDom.innerHTML = data;
  100. }
  101. var loginToken = "a0f18fa6b1f04232be549b6bb0186529";
  102. var gameID = "";
  103. var gameResult = {
  104. score: 301,
  105. list: [
  106. {
  107. clicktime: 1602222222,
  108. score: 301,
  109. },
  110. ],
  111. };
  112. var orderID = "";
  113. var mobile = "";
  114. var code = "";
  115. function getCode(data) {
  116. // 获取客户端传回来的code
  117. // 此处请求后端接口,将端给的code传给后端进行登录
  118. let params = { token: loginToken };
  119. $.ajax({
  120. type: "POST",
  121. url: "http://www.weibo.cn/index/index/checkLogin",
  122. data: JSON.stringify({ cookie: document.cookie }),
  123. dataType: "json",
  124. xhrFields: {
  125. withCredentials: true, //允许跨域带Cookie
  126. },
  127. beforeSend: function (xhr) {
  128. xhr.withCredentials = true;
  129. },
  130. success: function (res) {
  131. console.log(res);
  132. loginToken = res.data.token;
  133. dataDom.innerHTML = res.data.token;
  134. },
  135. });
  136. }
  137. $("#config").click(function () {
  138. let params = { token: loginToken };
  139. // let encryptedData = Encrypt(JSON.stringify(params));
  140. $.post("http://www.weibo.cn/index/login", params, function (res) {
  141. $("#configResult").html(res);
  142. let decrypted = Decrypt(res.data);
  143. $("#configResult").html(decrypted);
  144. });
  145. });
  146. $("#start").click(function () {
  147. let params = { token: loginToken };
  148. // let encryptedData = Encrypt(JSON.stringify(params));
  149. $.post("http://www.weibo.cn/index/startGame", params, function (res) {
  150. $("#startResult").html(JSON.stringify(res));
  151. let decrypted = res.data;
  152. gameID = decrypted.gameID;
  153. });
  154. });
  155. $("#topList").click(function () {
  156. let params = { token: loginToken };
  157. // let encryptedData = Encrypt(JSON.stringify(params));
  158. $.post("http://www.weibo.cn/index/topList", params, function (res) {
  159. $("#topListResult").html(JSON.stringify(res));
  160. // let decrypted = (Decrypt(res.data));
  161. // $('#topListResult').html(decrypted);
  162. // decrypted = JSON.parse(decrypted);
  163. // gameID = decrypted.gameID;
  164. });
  165. });
  166. $("#end").click(function () {
  167. let params = { token: loginToken, gameID: gameID, score: 300, gameData: gameResult };
  168. // let encryptedData = Encrypt(JSON.stringify(params));
  169. $.post("http://www.weibo.cn/index/endGame", params, function (res) {
  170. $("#endResult").html(JSON.stringify(res));
  171. // let decrypted = (Decrypt(res.data));
  172. // $('#endResult').html(decrypted);
  173. // decrypted = JSON.parse(decrypted);
  174. // gameID = decrypted.gameID;
  175. });
  176. });
  177. $("#reward").click(function () {
  178. let params = { token: loginToken, gameID: gameID };
  179. // let encryptedData = Encrypt(JSON.stringify(params));
  180. $.post("http://www.weibo.cn/index/reward", params, function (res) {
  181. $("#rewardResult").html(JSON.stringify(res));
  182. orderID = res.data.orderID;
  183. // let decrypted = (Decrypt(res.data));
  184. // $('#endResult').html(decrypted);
  185. // decrypted = JSON.parse(decrypted);
  186. // gameID = decrypted.gameID;
  187. });
  188. });
  189. $("#sms").click(function () {
  190. mobile = $("#mobile").val();
  191. let params = { token: loginToken, mobile: mobile };
  192. // let encryptedData = Encrypt(JSON.stringify(params));
  193. $.post("http://www.weibo.cn/index/sendSmsCode", params, function (res) {
  194. $("#smsResult").html(JSON.stringify(res));
  195. // orderID = res.data.orderID;
  196. // let decrypted = (Decrypt(res.data));
  197. // $('#endResult').html(decrypted);
  198. // decrypted = JSON.parse(decrypted);
  199. // gameID = decrypted.gameID;
  200. });
  201. });
  202. $("#saveAddress").click(function () {
  203. code = $("#code").val();
  204. let params = {
  205. token: loginToken,
  206. orderID: orderID,
  207. isTopTen: false,
  208. name: "人人粉我",
  209. address: "上海东方明珠",
  210. mobile: mobile,
  211. code: code,
  212. };
  213. // let encryptedData = Encrypt(JSON.stringify(params));
  214. $.post("http://www.weibo.cn/index/saveAddress", params, function (res) {
  215. $("#saveAddressResult").html(JSON.stringify(res));
  216. // orderID = res.data.orderID;
  217. // let decrypted = (Decrypt(res.data));
  218. // $('#endResult').html(decrypted);
  219. // decrypted = JSON.parse(decrypted);
  220. // gameID = decrypted.gameID;
  221. });
  222. });
  223. }
  224. // 触发方法
  225. function callNative(name, value) {
  226. dataDom.innerHTML = navigator.userAgent;
  227. try {
  228. if (isWeChat && weChatUrl != null) {
  229. // 微信
  230. location.href = weChatUrl;
  231. } else if (isAndroid) {
  232. // H5调用安卓
  233. // 我传值
  234. dataDom.innerHTML = "设置值2";
  235. client.call(name, value);
  236. } else if (isiOS) {
  237. // H5调用ios
  238. dataDom.innerHTML = "设置值2";
  239. window.webkit.messageHandlers.calliOS.postMessage({ name, value });
  240. }
  241. } catch (e) {
  242. dataDom.innerHTML = "设置值3" + e;
  243. }
  244. }
  245. let dataDom = document.getElementById("data");
  246. function setData() {
  247. // 触发调用
  248. dataDom.innerHTML = "设置值1";
  249. // 下載圖片
  250. // downloadImg: 调用原生的方法名
  251. // 图片url: 写入需要传入的url
  252. // callNative('downloadImg', '图片url')
  253. // 调用code
  254. callNative("sendCode", "");
  255. }
  256. /**
  257. * 解密
  258. * @param word
  259. * @returns {*}
  260. */
  261. function Decrypt(word) {
  262. var key = CryptoJS.enc.Utf8.parse("yushenjianglinle");
  263. var decrypt = CryptoJS.AES.decrypt(word, key, { mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.Pkcs7 });
  264. return CryptoJS.enc.Utf8.stringify(decrypt).toString();
  265. }
  266. //加密
  267. function Encrypt(word) {
  268. var key = CryptoJS.enc.Utf8.parse("yushenjianglinle");
  269. let srcs = CryptoJS.enc.Utf8.parse(word);
  270. let encrypted = CryptoJS.AES.encrypt(srcs, key, { mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.Pkcs7 });
  271. return encrypted.toString();
  272. }
  273. </script>
  274. </body>
  275. </html>