| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- <!DOCTYPE html>
- <html lang="zh-CN">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>华为运动健康授权结果</title>
- <style>
- * { margin: 0; padding: 0; box-sizing: border-box; }
- body { font-family: -apple-system, "PingFang SC", "Microsoft YaHei", sans-serif; background: #f7f8fa; display: flex; align-items: center; justify-content: center; min-height: 100vh; }
- .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); }
- h1 { font-size: 20px; color: #1f2329; margin-bottom: 12px; }
- .icon { font-size: 40px; margin-bottom: 12px; }
- .status { font-size: 15px; color: #4b5563; line-height: 1.7; margin-bottom: 24px; }
- .btn { display: inline-block; background: #4259ff; color: #fff; border: none; border-radius: 8px; padding: 12px 28px; font-size: 15px; cursor: pointer; }
- .btn:hover { opacity: .9; }
- .btn-plain { background: #f1f3f5; color: #1f2329; margin-left: 8px; }
- .hide { display: none; }
- </style>
- </head>
- <body>
- <div class="wrap">
- <div id="ic" class="icon">⏳</div>
- <h1 id="title">授权处理中</h1>
- <p id="status" class="status">正在确认华为运动健康授权状态…</p>
- <button id="retry" class="btn hide" onclick="run()">重新授权</button>
- <button id="close" class="btn btn-plain hide" onclick="tryClose()">关闭本页</button>
- <p class="status" id="extra" style="display:none"></p>
- </div>
- <script>
- // H5 中转回调页(huawei-cb.html):解析 code/state → POST /api/huawei-health/callback → 三态渲染。
- // state 校验:华为 OAuth 原样带回 memberId,本页仅做类型/长度基本校验;
- // 服务端 memberId 与 JWT 归属的最终校验在后端接口完成(前端不得仅凭 query 决定绑定对象)。
- function parseQuery() {
- var qs = window.location.search.replace(/^\?/, '')
- var out = {}
- if (!qs) return out
- var parts = qs.split('&')
- for (var i = 0; i < parts.length; i++) {
- var kv = parts[i].split('=')
- if (kv[0]) out[decodeURIComponent(kv[0])] = decodeURIComponent(kv[1] || '')
- }
- return out
- }
- function render(icon, title, statusText, showRetry) {
- document.getElementById('ic').textContent = icon
- document.getElementById('title').textContent = title
- document.getElementById('status').textContent = statusText
- document.getElementById('retry').classList.toggle('hide', !showRetry)
- document.getElementById('close').classList.remove('hide')
- if (icon === '✅' && showRetry) {
- // 成功态:≥3s 自动尝试关闭引导
- setTimeout(tryClose, 3000)
- }
- }
- function tryClose() {
- // 小程序 web-view 场景可由宿主通过 onPostMessage 关闭;浏览器环境降级为隐藏
- try { document.title = '已授权,请返回小程序' } catch (e) { /* ignore */ }
- }
- function run() {
- var q = parseQuery()
- var code = q.code || ''
- var state = q.state || ''
- var memberId = /^\d+$/.test(state) ? parseInt(state, 10) : NaN
- if (!code) {
- render('⚠️', '授权未完成', '未获取到授权码,请返回小程序重试。', true)
- return
- }
- if (!memberId) {
- render('⚠️', '参数异常', '授权回调缺少有效的成员标识(state),请返回小程序重试。', true)
- return
- }
- var body = JSON.stringify({ code: code, memberId: memberId })
- fetch('/api/huawei-health/callback', {
- method: 'POST',
- headers: { 'Content-Type': 'application/json', 'Authorization': 'Bearer ' + (window._cfcToken || '') },
- body: body
- }).then(function(res) { return res.json() }).then(function(data) {
- if (data && data.code === 200 && data.data && data.data.bound) {
- render('✅', '已授权,请返回小程序', '华为运动健康已成功绑定,请返回小程序查看。', false)
- } else {
- render('❌', '授权失败', (data && data.message) || '绑定失败,请重新授权后重试。', true)
- }
- }).catch(function() {
- render('❌', '网络异常', '无法连接服务,请重新授权后重试。', true)
- })
- }
- run()
- </script>
- </body>
- </html>
|