callback.html 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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,viewport-fit=cover">
  6. <title>CocoBuddy · 登录中</title>
  7. <style>
  8. :root {
  9. --bg: #f6f7fb;
  10. --card: #ffffff;
  11. --ink: #171a20;
  12. --muted: #6b7280;
  13. --line: #e6e8ef;
  14. --brand: #2f6bff;
  15. --danger: #c0392b;
  16. }
  17. * { box-sizing: border-box; }
  18. html, body { height: 100%; margin: 0; }
  19. body {
  20. background: radial-gradient(1200px 600px at 50% -10%, #e8efff 0%, var(--bg) 55%);
  21. color: var(--ink);
  22. font: 15px/1.6 -apple-system, "Segoe UI", system-ui, "PingFang SC", "Microsoft YaHei", sans-serif;
  23. display: flex; align-items: center; justify-content: center; padding: 24px;
  24. }
  25. .card {
  26. width: 100%; max-width: 420px; background: var(--card);
  27. border: 1px solid var(--line); border-radius: 20px; padding: 32px 28px 26px;
  28. box-shadow: 0 24px 60px rgba(23, 26, 32, .08); text-align: center;
  29. }
  30. .mark {
  31. width: 44px; height: 44px; border-radius: 13px; margin: 0 auto 18px;
  32. background: linear-gradient(135deg, #2f6bff, #7ab0ff);
  33. color: #fff; font-weight: 700; font-size: 20px;
  34. display: flex; align-items: center; justify-content: center;
  35. }
  36. .spinner {
  37. width: 26px; height: 26px; margin: 4px auto 16px;
  38. border: 3px solid #e3e9f7; border-top-color: var(--brand);
  39. border-radius: 50%; animation: spin .8s linear infinite;
  40. }
  41. @keyframes spin { to { transform: rotate(360deg); } }
  42. h1 { font-size: 18px; margin: 0 0 8px; letter-spacing: -.2px; }
  43. p { color: var(--muted); font-size: 13.5px; margin: 0 0 18px; min-height: 22px; }
  44. a {
  45. display: inline-block; color: var(--brand); text-decoration: none;
  46. font-size: 14px; font-weight: 600; padding: 10px 18px;
  47. border: 1px solid var(--line); border-radius: 10px;
  48. }
  49. a:hover { border-color: var(--brand); }
  50. .bad { color: var(--danger); }
  51. </style>
  52. </head>
  53. <body>
  54. <div class="card">
  55. <div class="mark">C</div>
  56. <div class="spinner" id="spin"></div>
  57. <h1 id="title">正在完成登录</h1>
  58. <p id="msg">正在校验平台登录状态…</p>
  59. <a id="back" href="/_cc/login" hidden>返回登录</a>
  60. </div>
  61. <script>
  62. (function () {
  63. var msg = document.getElementById('msg')
  64. var back = document.getElementById('back')
  65. var spin = document.getElementById('spin')
  66. var title = document.getElementById('title')
  67. function fail (text) {
  68. spin.hidden = true
  69. title.textContent = '登录没有完成'
  70. title.className = 'bad'
  71. msg.textContent = text || '登录失败,请返回重试。'
  72. back.hidden = false
  73. }
  74. function readHash () {
  75. var out = {}
  76. var raw = String(location.hash || '').replace(/^#/, '')
  77. if (!raw) return out
  78. raw.split('&').forEach(function (part) {
  79. var i = part.indexOf('=')
  80. if (i <= 0) return
  81. try {
  82. out[decodeURIComponent(part.slice(0, i))] = decodeURIComponent(part.slice(i + 1))
  83. } catch (e) { /* 忽略坏字段 */ }
  84. })
  85. return out
  86. }
  87. var hash = readHash()
  88. var query = new URLSearchParams(location.search)
  89. // query 与 hash 合并:平台把参数放哪一边都能取到
  90. var all = {}
  91. query.forEach(function (v, k) { all[k] = v })
  92. Object.keys(hash).forEach(function (k) { all[k] = hash[k] })
  93. var key = all.apiKey || all.api_key || all.key || all.token || all.access_token
  94. // 先把 key 从地址栏和历史里抹掉,再发请求
  95. try { history.replaceState(null, '', location.pathname) } catch (e) { /* 老浏览器 */ }
  96. if (!key) return fail('没有拿到登录凭据。请回到平台重新登录,或返回用账号密码登录。')
  97. // 身份字段一起带上:带 userid 时服务端按"平台已认证"处理,不再反查 key
  98. var payload = {
  99. apiKey: key,
  100. userid: all.userid || all.userId || all.uid || '',
  101. username: all.username || all.name || all.nickname || '',
  102. organizeid: all.organizeid || all.organizeId || all.orgId || all.orgid || '',
  103. schoolname: all.schoolname || all.schoolName || all.organizeName || all.orgName || '',
  104. region: all.region || ''
  105. }
  106. fetch('/_cc/api/login', {
  107. method: 'POST',
  108. headers: { 'content-type': 'application/json' },
  109. body: JSON.stringify(payload)
  110. }).then(function (res) {
  111. return res.json().catch(function () { return null }).then(function (json) {
  112. return { ok: res.ok && json && json.ok, error: (json && json.error) || ('HTTP ' + res.status) }
  113. })
  114. }).then(function (result) {
  115. if (result.ok) {
  116. // 被 window.open 打开时,刷新父窗口并关掉自己(和桌面端独立登录窗口的体感一致)
  117. if (window.opener && !window.opener.closed) {
  118. try { window.opener.location.replace('/') } catch (e) { /* 跨域就只关闭 */ }
  119. window.close()
  120. return
  121. }
  122. return location.replace('/')
  123. }
  124. fail(result.error)
  125. }).catch(function (err) {
  126. fail(err && err.message)
  127. })
  128. })()
  129. </script>
  130. </body>
  131. </html>