wechat-binding-mobile.html 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <!doctype html>
  2. <html lang="zh-CN">
  3. <head>
  4. <meta charset="utf-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1">
  6. <title>山峡化工服务号绑定</title>
  7. <style nonce="__NONCE__">
  8. body { margin: 0; padding: 48px 24px; background: #f5f8f7; color: #1f2a26; font-family: sans-serif; line-height: 1.7; }
  9. main { max-width: 480px; margin: auto; padding: 28px; border-radius: 16px; background: #fff; }
  10. h1 { font-size: 22px; }
  11. button { width: 100%; min-height: 44px; border: 0; border-radius: 10px; background: #0a9f6d; color: #fff; font-size: 16px; }
  12. #return-hint { color: #6b7b74; font-size: 14px; }
  13. </style>
  14. </head>
  15. <body>
  16. <main>
  17. <h1>微信通知绑定</h1>
  18. <p>__MESSAGE__</p>
  19. <button id="return-button" type="button">返回小程序</button>
  20. <p id="return-hint">如未自动返回,请点击上方按钮或左上角返回小程序。</p>
  21. </main>
  22. <script id="wechat-sdk" nonce="__NONCE__" async src="https://res.wx.qq.com/open/js/jweixin-1.6.0.js"></script>
  23. <script nonce="__NONCE__">
  24. (function () {
  25. var busy = false, autoAttempted = false;
  26. var hint = document.getElementById('return-hint');
  27. function fallback() {
  28. busy = false;
  29. hint.textContent = '如未返回,请点左上角返回小程序;原页面将自动完成绑定,也可点击“刷新绑定状态”。';
  30. }
  31. function goBack() {
  32. if (busy) return;
  33. var mp = window.wx && window.wx.miniProgram;
  34. // SDK 在桥接未就绪时会排队;不要在这里排队后再因 Ready 事件重复返回。
  35. if (!window.WeixinJSBridge || !mp || typeof mp.navigateBack !== 'function') { fallback(); return; }
  36. autoAttempted = true;
  37. busy = true;
  38. var timer = setTimeout(fallback, 2500);
  39. try {
  40. mp.navigateBack({ delta: 1, fail: function () { clearTimeout(timer); fallback(); } });
  41. } catch (_) { clearTimeout(timer); fallback(); }
  42. }
  43. function autoBack() {
  44. if (autoAttempted || !window.WeixinJSBridge || !window.wx || !window.wx.miniProgram) return;
  45. goBack();
  46. }
  47. document.getElementById('return-button').addEventListener('click', goBack);
  48. document.getElementById('wechat-sdk').addEventListener('load', autoBack);
  49. document.getElementById('wechat-sdk').addEventListener('error', fallback);
  50. document.addEventListener('WeixinJSBridgeReady', autoBack);
  51. autoBack();
  52. // SDK 加载失败或桥接无响应时仍保留用户可操作的返回入口。
  53. setTimeout(function () { if (!busy) fallback(); }, 4000);
  54. })();
  55. </script>
  56. </body>
  57. </html>