callback.html 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. var key = hash.key || hash.apiKey || hash.api_key || hash.token || hash.access_token ||
  90. query.get('key') || query.get('apiKey') || query.get('token')
  91. // 先把 key 从地址栏和历史里抹掉,再发请求
  92. try { history.replaceState(null, '', location.pathname) } catch (e) { /* 老浏览器 */ }
  93. if (!key) return fail('没有拿到登录凭据。请回到平台重新登录,或返回用账号密码登录。')
  94. fetch('/_cc/api/login', {
  95. method: 'POST',
  96. headers: { 'content-type': 'application/json' },
  97. body: JSON.stringify({ apiKey: key })
  98. }).then(function (res) {
  99. return res.json().catch(function () { return null }).then(function (json) {
  100. return { ok: res.ok && json && json.ok, error: (json && json.error) || ('HTTP ' + res.status) }
  101. })
  102. }).then(function (result) {
  103. if (result.ok) {
  104. // 被 window.open 打开时,刷新父窗口并关掉自己(和桌面端独立登录窗口的体感一致)
  105. if (window.opener && !window.opener.closed) {
  106. try { window.opener.location.replace('/') } catch (e) { /* 跨域就只关闭 */ }
  107. window.close()
  108. return
  109. }
  110. return location.replace('/')
  111. }
  112. fail(result.error)
  113. }).catch(function (err) {
  114. fail(err && err.message)
  115. })
  116. })()
  117. </script>
  118. </body>
  119. </html>