huawei-cb.html 4.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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">
  6. <title>华为运动健康授权结果</title>
  7. <style>
  8. * { margin: 0; padding: 0; box-sizing: border-box; }
  9. body { font-family: -apple-system, "PingFang SC", "Microsoft YaHei", sans-serif; background: #f7f8fa; display: flex; align-items: center; justify-content: center; min-height: 100vh; }
  10. .wrap { background: #fff; border-radius: 16px; padding: 40px 32px; width: 88%; max-width: 420px; text-align: center; box-shadow: 0 2px 12px rgba(0,0,0,.06); }
  11. h1 { font-size: 20px; color: #1f2329; margin-bottom: 12px; }
  12. .icon { font-size: 40px; margin-bottom: 12px; }
  13. .status { font-size: 15px; color: #4b5563; line-height: 1.7; margin-bottom: 24px; }
  14. .btn { display: inline-block; background: #4259ff; color: #fff; border: none; border-radius: 8px; padding: 12px 28px; font-size: 15px; cursor: pointer; }
  15. .btn:hover { opacity: .9; }
  16. .btn-plain { background: #f1f3f5; color: #1f2329; margin-left: 8px; }
  17. .hide { display: none; }
  18. </style>
  19. </head>
  20. <body>
  21. <div class="wrap">
  22. <div id="ic" class="icon">⏳</div>
  23. <h1 id="title">授权处理中</h1>
  24. <p id="status" class="status">正在确认华为运动健康授权状态…</p>
  25. <button id="retry" class="btn hide" onclick="run()">重新授权</button>
  26. <button id="close" class="btn btn-plain hide" onclick="tryClose()">关闭本页</button>
  27. <p class="status" id="extra" style="display:none"></p>
  28. </div>
  29. <script>
  30. // H5 中转回调页(huawei-cb.html):解析 code/state → POST /api/huawei-health/callback → 三态渲染。
  31. // state 校验:华为 OAuth 原样带回 memberId,本页仅做类型/长度基本校验;
  32. // 服务端 memberId 与 JWT 归属的最终校验在后端接口完成(前端不得仅凭 query 决定绑定对象)。
  33. function parseQuery() {
  34. var qs = window.location.search.replace(/^\?/, '')
  35. var out = {}
  36. if (!qs) return out
  37. var parts = qs.split('&')
  38. for (var i = 0; i < parts.length; i++) {
  39. var kv = parts[i].split('=')
  40. if (kv[0]) out[decodeURIComponent(kv[0])] = decodeURIComponent(kv[1] || '')
  41. }
  42. return out
  43. }
  44. function render(icon, title, statusText, showRetry) {
  45. document.getElementById('ic').textContent = icon
  46. document.getElementById('title').textContent = title
  47. document.getElementById('status').textContent = statusText
  48. document.getElementById('retry').classList.toggle('hide', !showRetry)
  49. document.getElementById('close').classList.remove('hide')
  50. if (icon === '✅' && showRetry) {
  51. // 成功态:≥3s 自动尝试关闭引导
  52. setTimeout(tryClose, 3000)
  53. }
  54. }
  55. function tryClose() {
  56. // 小程序 web-view 场景可由宿主通过 onPostMessage 关闭;浏览器环境降级为隐藏
  57. try { document.title = '已授权,请返回小程序' } catch (e) { /* ignore */ }
  58. }
  59. function run() {
  60. var q = parseQuery()
  61. var code = q.code || ''
  62. var state = q.state || ''
  63. var memberId = /^\d+$/.test(state) ? parseInt(state, 10) : NaN
  64. if (!code) {
  65. render('⚠️', '授权未完成', '未获取到授权码,请返回小程序重试。', true)
  66. return
  67. }
  68. if (!memberId) {
  69. render('⚠️', '参数异常', '授权回调缺少有效的成员标识(state),请返回小程序重试。', true)
  70. return
  71. }
  72. var body = JSON.stringify({ code: code, memberId: memberId })
  73. fetch('/api/huawei-health/callback', {
  74. method: 'POST',
  75. headers: { 'Content-Type': 'application/json', 'Authorization': 'Bearer ' + (window._cfcToken || '') },
  76. body: body
  77. }).then(function(res) { return res.json() }).then(function(data) {
  78. if (data && data.code === 200 && data.data && data.data.bound) {
  79. render('✅', '已授权,请返回小程序', '华为运动健康已成功绑定,请返回小程序查看。', false)
  80. } else {
  81. render('❌', '授权失败', (data && data.message) || '绑定失败,请重新授权后重试。', true)
  82. }
  83. }).catch(function() {
  84. render('❌', '网络异常', '无法连接服务,请重新授权后重试。', true)
  85. })
  86. }
  87. run()
  88. </script>
  89. </body>
  90. </html>