huawei-cb.html 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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.0, maximum-scale=1.0, user-scalable=no">
  6. <title>华为运动健康授权结果</title>
  7. <style>
  8. * { margin: 0; padding: 0; box-sizing: border-box; }
  9. body { font-family: -apple-system, BlinkMacSystemFont, "PingFang SC", "Microsoft YaHei", sans-serif; background: #F8F8F8; display: flex; align-items: center; justify-content: center; min-height: 100vh; }
  10. .card { background: #fff; border-radius: 16px; padding: 32px 24px; width: 88%; max-width: 380px; text-align: center; box-shadow: 0 2px 12px rgba(0,0,0,0.06); }
  11. .icon { font-size: 44px; margin-bottom: 12px; }
  12. h1 { font-size: 18px; font-weight: 700; color: #333; margin-bottom: 8px; }
  13. .desc { font-size: 14px; color: #999; margin-bottom: 20px; line-height: 1.6; }
  14. .btn { display: inline-block; background: #F97316; color: #fff; border: none; border-radius: 24px; padding: 10px 32px; font-size: 14px; cursor: pointer; text-decoration: none; }
  15. .btn:hover { opacity: 0.9; }
  16. </style>
  17. </head>
  18. <body>
  19. <div class="card">
  20. <div class="icon" id="icon">⏳</div>
  21. <h1 id="title">处理中…</h1>
  22. <p class="desc" id="desc">正在完成华为账号授权…</p>
  23. <a class="btn" id="btn" style="display:none" href="javascript:void(0)">返回重试</a>
  24. </div>
  25. <script>
  26. // §5.2 入口:/huawei-cb.html?code=<code>&state=<memberId>
  27. // 解析 code/state → POST /api/huawei-health/callback → 三态渲染:
  28. // 成功「已授权,请返回小程序」/ 失败错误文案+重试 / 无 code 兜底「授权未完成,请返回重试」。
  29. // 服务端 memberId 与 JWT 归属的最终校验在后端接口2完成,本页只做类型/长度基本校验。
  30. (function() {
  31. function getParam(name) {
  32. var m = location.search.match(new RegExp('[?&]' + name + '=([^&]*)'))
  33. return m ? decodeURIComponent(m[1].replace(/\+/g, '%20')) : ''
  34. }
  35. var API_BASE = '/api'
  36. var code = getParam('code')
  37. var state = getParam('state')
  38. var icon = document.getElementById('icon')
  39. var title = document.getElementById('title')
  40. var desc = document.getElementById('desc')
  41. var btn = document.getElementById('btn')
  42. function showError(text) {
  43. icon.textContent = '⚠️'
  44. title.textContent = '授权失败'
  45. desc.textContent = text
  46. btn.style.display = 'inline-block'
  47. btn.textContent = '返回重试'
  48. btn.addEventListener('click', function() { location.href = '/huawei-auth.html' })
  49. }
  50. if (!code) {
  51. // 参数缺失(无 code):兜底文案
  52. icon.textContent = 'ℹ️'
  53. title.textContent = '授权未完成'
  54. desc.textContent = '授权未完成,请返回重试'
  55. btn.style.display = 'inline-block'
  56. btn.textContent = '返回重试'
  57. btn.addEventListener('click', function() { location.href = '/huawei-auth.html' })
  58. return
  59. }
  60. var memberId = null
  61. var n = parseInt(state, 10)
  62. if (!isNaN(n) && n > 0) { memberId = n }
  63. var payload = { code: code }
  64. if (memberId) { payload.memberId = memberId }
  65. fetch(API_BASE + '/huawei-health/callback', {
  66. method: 'POST',
  67. headers: { 'Content-Type': 'application/json' },
  68. body: JSON.stringify(payload)
  69. }).then(function(res) { return res.json() })
  70. .then(function(result) {
  71. if (result && result.code === 200) {
  72. icon.textContent = '✅'
  73. title.textContent = '已授权,请返回小程序'
  74. desc.textContent = '绑定成功,数据仅用于家庭健康档案展示'
  75. // ≥3s 自动尝试关闭引导(小程序 web-view 用户手动返回亦可)
  76. setTimeout(function() {
  77. if (location.href.indexOf('weixin') >= 0) { try { window.close() } catch (e) {} }
  78. }, 3000)
  79. } else {
  80. showError((result && result.message) || '绑定失败,请重新授权')
  81. }
  82. })
  83. .catch(function() { showError('网络异常,请重新授权') })
  84. })();
  85. </script>
  86. </body>
  87. </html>