index copy.html 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8" />
  5. <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
  6. <title>网络网页</title>
  7. <style>
  8. .container {
  9. display: flex;
  10. flex-direction: column;
  11. align-items: center;
  12. justify-content: center;
  13. height: 100vh;
  14. padding: 20px;
  15. }
  16. .form-group {
  17. margin-bottom: 15px;
  18. width: 100%;
  19. max-width: 300px;
  20. }
  21. .form-group label {
  22. display: block;
  23. margin-bottom: 5px;
  24. font-weight: bold;
  25. }
  26. .form-group input {
  27. width: 100%;
  28. padding: 8px 12px;
  29. border: 1px solid #ddd;
  30. border-radius: 4px;
  31. box-sizing: border-box;
  32. }
  33. .btn {
  34. width: 100%;
  35. max-width: 300px;
  36. padding: 10px;
  37. background-color: #007aff;
  38. color: white;
  39. border: none;
  40. border-radius: 4px;
  41. cursor: pointer;
  42. margin-top: 10px;
  43. }
  44. .btn:disabled {
  45. background-color: #ccc;
  46. cursor: not-allowed;
  47. }
  48. #your-dom-id {
  49. margin-top: 20px;
  50. }
  51. </style>
  52. </head>
  53. <body>
  54. <div class="container">
  55. <div class="form-group">
  56. <label for="phone">手机号</label>
  57. <input type="tel" id="phone" placeholder="请输入手机号" maxlength="11">
  58. </div>
  59. <div class="form-group">
  60. <label for="captcha">验证码</label>
  61. <input type="text" id="captcha" placeholder="请输入验证码">
  62. </div>
  63. <button class="btn" id="submitBtn">提交验证</button>
  64. <div id="your-dom-id"></div>
  65. </div>
  66. <!-- 引入必要的库 -->
  67. <script src="https://cdn.jsdelivr.net/npm/ts-md5/dist/md5.js"></script>
  68. <script type="text/javascript">
  69. var userAgent = navigator.userAgent;
  70. if (userAgent.indexOf('AlipayClient') > -1) {
  71. document.writeln('<script src="https://appx/web-view.min.js"' + '>' + '<' + '/' + 'script>');
  72. } else if (/QQ/i.test(userAgent) && /miniProgram/i.test(userAgent)) {
  73. document.write('<script type="text/javascript" src="https://qqq.gtimg.cn/miniprogram/webview_jssdk/qqjssdk-1.0.0.js"><\/script>');
  74. } else if (/miniProgram/i.test(userAgent) && /micromessenger/i.test(userAgent)) {
  75. document.write('<script type="text/javascript" src="https://res.wx.qq.com/open/js/jweixin-1.4.0.js"><\/script>');
  76. } else if (/toutiaomicroapp/i.test(userAgent)) {
  77. document.write('<script type="text/javascript" src="https://lf1-cdn-tos.bytegoofy.com/goofy/developer/jssdk/jssdk-1.2.0.js"><\/script>');
  78. } else if (/swan/i.test(userAgent)) {
  79. document.write('<script type="text/javascript" src="https://b.bdstatic.com/searchbox/icms/searchbox/js/swan-2.0.18.js"><\/script>');
  80. } else if (/quickapp/i.test(userAgent)) {
  81. document.write('<script type="text/javascript" src="https://quickapp/jssdk.webview.min.js"><\/script>');
  82. }
  83. </script>
  84. <!-- uni 的 SDK -->
  85. <script type="text/javascript" src="https://www.chengxiangjiaoyu.com/uni.webview.1.5.6.js"></script>
  86. <script>
  87. window.AliyunCaptchaConfig = {
  88. region: 'cn',
  89. prefix: 'm9a704',
  90. };
  91. </script>
  92. <script src="https://o.alicdn.com/captcha-frontend/aliyunCaptcha/AliyunCaptcha.js"></script>
  93. <script type="text/javascript">
  94. // 配置
  95. const baseUrl = 'https://你的API域名'; // 请替换为实际的API域名
  96. const timeout = 10000;
  97. // 工具函数
  98. function toast(message) {
  99. // 在HTML环境中模拟toast,你可以根据实际需求调整
  100. alert(message);
  101. }
  102. function showConfirm(message) {
  103. return new Promise((resolve) => {
  104. if (confirm(message)) {
  105. resolve({ confirm: true });
  106. } else {
  107. resolve({ confirm: false });
  108. }
  109. });
  110. }
  111. // 从localStorage获取auth信息
  112. function getAuth() {
  113. try {
  114. const authStr = localStorage.getItem('auth');
  115. return authStr ? JSON.parse(authStr) : null;
  116. } catch (e) {
  117. console.error('获取auth信息失败:', e);
  118. return null;
  119. }
  120. }
  121. // 保存auth信息到localStorage
  122. function saveAuth(auth) {
  123. try {
  124. localStorage.setItem('auth', JSON.stringify(auth));
  125. } catch (e) {
  126. console.error('保存auth信息失败:', e);
  127. }
  128. }
  129. // 请求函数 - 模拟你项目中的request函数
  130. function request(config) {
  131. return new Promise((resolve, reject) => {
  132. // 显示loading
  133. if (config.isLoading) {
  134. // 在HTML环境中可以显示自定义loading或使用uni的loading
  135. console.log('显示loading...');
  136. }
  137. // 准备headers
  138. const headers = config.headers || {};
  139. const auth = getAuth();
  140. // 添加签名和token
  141. if (auth) {
  142. headers['X-AUTH-SIGN'] = Md5.hashStr(JSON.stringify(config.data || {}) + auth.secret);
  143. headers['X-AUTH-TOKEN'] = auth.token;
  144. }
  145. // 使用uni.request发送请求
  146. uni.request({
  147. method: config.method || 'get',
  148. timeout: config.timeout || timeout,
  149. url: config.baseUrl || baseUrl + config.url,
  150. data: config.data,
  151. header: headers,
  152. dataType: 'json',
  153. success: (response) => {
  154. if (config.isLoading) {
  155. console.log('隐藏loading...');
  156. }
  157. const res = response.data;
  158. const code = res.code;
  159. const msg = res.msg;
  160. // 错误处理
  161. if (code === 401) {
  162. showConfirm('登录状态已过期,您可以继续留在该页面,或者重新登录?').then(res => {
  163. if (res.confirm) {
  164. // 跳转到登录页面
  165. uni.reLaunch({
  166. url: '/pages/Login/index'
  167. });
  168. }
  169. });
  170. reject('无效的会话,或者会话已过期,请重新登录。');
  171. } else if (code === 405) {
  172. showConfirm('登录状态已过期,您可以继续留在该页面,或者重新登录?').then(res => {
  173. if (res.confirm) {
  174. // 跳转到登录页面
  175. uni.reLaunch({
  176. url: '/pages/Login/index'
  177. });
  178. }
  179. });
  180. reject('无效的会话,或者会话已过期,请重新登录。');
  181. } else if (code === 500) {
  182. toast("数据异常-500");
  183. reject('500');
  184. } else if (code === 400) {
  185. toast("数据异常-400");
  186. reject('400');
  187. } else if (code !== 0) {
  188. toast(msg);
  189. reject(code);
  190. } else {
  191. resolve(res);
  192. }
  193. },
  194. fail: (error) => {
  195. if (config.isLoading) {
  196. console.log('隐藏loading...');
  197. }
  198. let message = error.errMsg || '请求失败';
  199. if (message === 'request:fail') {
  200. message = '后端接口连接异常';
  201. } else if (message.includes('timeout')) {
  202. message = '系统接口请求超时';
  203. }
  204. toast(message);
  205. reject(error);
  206. }
  207. });
  208. });
  209. }
  210. // 示例:发送验证请求
  211. function submitVerification(phone, captcha, captchaVerifyParam) {
  212. const requestData = {
  213. phone: phone,
  214. captcha: captcha,
  215. captchaVerifyParam: captchaVerifyParam
  216. // 其他需要的参数...
  217. };
  218. return request({
  219. url: '/your-api-endpoint', // 替换为实际的API路径
  220. method: 'post',
  221. data: requestData,
  222. isLoading: true
  223. });
  224. }
  225. // 主逻辑
  226. document.addEventListener('UniAppJSBridgeReady', function() {
  227. const submitBtn = document.getElementById('submitBtn');
  228. const phoneInput = document.getElementById('phone');
  229. const captchaInput = document.getElementById('captcha');
  230. let captchaVerifyParam = null;
  231. // 初始化验证码
  232. setTimeout(initCaptcha, 500);
  233. function initCaptcha() {
  234. try {
  235. window.initAliyunCaptcha({
  236. SceneId: '1xp14eyl',
  237. mode: "embed",
  238. element: "#your-dom-id",
  239. success: function(verifyParam) {
  240. console.log('验证码验证成功:', verifyParam);
  241. captchaVerifyParam = verifyParam;
  242. // 自动提交(可选)
  243. // autoSubmit();
  244. },
  245. fail: function(err) {
  246. console.error('验证失败:', err);
  247. toast('验证码验证失败,请重试');
  248. },
  249. slideStyle: {
  250. width: 320,
  251. height: 40
  252. },
  253. language: 'cn',
  254. timeout: 5000,
  255. });
  256. } catch (error) {
  257. console.error('初始化验证码失败:', error);
  258. }
  259. }
  260. // 提交按钮点击事件
  261. submitBtn.addEventListener('click', function() {
  262. const phone = phoneInput.value.trim();
  263. const captcha = captchaInput.value.trim();
  264. if (!phone) {
  265. toast('请输入手机号');
  266. return;
  267. }
  268. if (!captcha) {
  269. toast('请输入验证码');
  270. return;
  271. }
  272. if (!captchaVerifyParam) {
  273. toast('请先完成验证码验证');
  274. return;
  275. }
  276. // 禁用按钮防止重复提交
  277. submitBtn.disabled = true;
  278. submitBtn.textContent = '提交中...';
  279. // 发送请求
  280. submitVerification(phone, captcha, captchaVerifyParam)
  281. .then(response => {
  282. toast('验证成功');
  283. console.log('接口返回:', response);
  284. // 传递多个参数给uniapp
  285. uni.postMessage({
  286. data: {
  287. captchaResult: captchaVerifyParam,
  288. phone: phone,
  289. apiResponse: response,
  290. status: "success",
  291. timestamp: new Date().getTime()
  292. }
  293. });
  294. // 延迟返回
  295. setTimeout(() => {
  296. uni.navigateBack({
  297. delta: 1
  298. });
  299. }, 1500);
  300. })
  301. .catch(error => {
  302. console.error('验证失败:', error);
  303. // 失败时也传递信息
  304. uni.postMessage({
  305. data: {
  306. status: "fail",
  307. error: error,
  308. phone: phone,
  309. timestamp: new Date().getTime()
  310. }
  311. });
  312. })
  313. .finally(() => {
  314. // 恢复按钮状态
  315. submitBtn.disabled = false;
  316. submitBtn.textContent = '提交验证';
  317. });
  318. });
  319. // 自动提交函数(如果需要)
  320. function autoSubmit() {
  321. const phone = phoneInput.value.trim();
  322. const captcha = captchaInput.value.trim();
  323. if (phone && captcha && captchaVerifyParam) {
  324. submitBtn.click();
  325. }
  326. }
  327. });
  328. </script>
  329. </body>
  330. </html>