143 lines
5.1 KiB
HTML
143 lines
5.1 KiB
HTML
<!DOCTYPE html>
|
||
<html lang="zh-CN">
|
||
<head>
|
||
<meta charset="utf-8" />
|
||
<meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover" />
|
||
<title>登录 · 复盘系统中控</title>
|
||
<link rel="stylesheet" href="/assets/app.css?v=20260530-hub-embed-login" />
|
||
</head>
|
||
<body class="login-page">
|
||
<div class="login-bg" aria-hidden="true"></div>
|
||
<div class="login-panel">
|
||
<div class="login-brand">
|
||
<span class="brand-mark"></span>
|
||
<div>
|
||
<div class="login-title">复盘系统中控</div>
|
||
<div class="login-sub">CRYPTO MONITOR · COMMAND</div>
|
||
</div>
|
||
</div>
|
||
<form id="login-form" class="login-form" autocomplete="on">
|
||
<label class="field">
|
||
<span>用户名</span>
|
||
<input type="text" name="username" id="login-username" required autocomplete="username" />
|
||
</label>
|
||
<label class="field">
|
||
<span>密码</span>
|
||
<input type="password" name="password" id="login-password" required autocomplete="current-password" />
|
||
</label>
|
||
<button type="submit" class="primary login-submit" id="login-submit">进入系统</button>
|
||
<p id="login-err" class="login-err" hidden></p>
|
||
<p id="login-hint" class="login-foot" hidden></p>
|
||
</form>
|
||
<p class="login-foot">账号在云端 hub 的 <code>.env</code>:<code>HUB_USERNAME</code> / <code>HUB_PASSWORD</code></p>
|
||
</div>
|
||
<script>
|
||
(function () {
|
||
const form = document.getElementById("login-form");
|
||
const err = document.getElementById("login-err");
|
||
const hint = document.getElementById("login-hint");
|
||
const submitBtn = document.getElementById("login-submit");
|
||
const userInput = document.getElementById("login-username");
|
||
const params = new URLSearchParams(location.search);
|
||
const next = params.get("next") || "/monitor";
|
||
const inFrame = window.self !== window.top;
|
||
const isHttps = location.protocol === "https:";
|
||
|
||
if (inFrame) {
|
||
hint.hidden = false;
|
||
hint.textContent = isHttps
|
||
? "嵌入模式:登录成功后将自动写入会话。"
|
||
: "嵌入模式需 HTTPS 中控;HTTP 时请用本地导航工具栏「中控登录」。";
|
||
}
|
||
|
||
function showErr(msg) {
|
||
err.textContent = msg;
|
||
err.hidden = false;
|
||
}
|
||
|
||
function gotoAfterLogin(token, dest) {
|
||
const target = dest.startsWith("/") ? dest : "/monitor";
|
||
if (inFrame) {
|
||
if (!token) {
|
||
showErr("登录响应缺少 session_token,请升级云端 hub 或使用本地导航「中控登录」。");
|
||
return;
|
||
}
|
||
if (!isHttps) {
|
||
showErr("跨站 iframe 登录需要 HTTPS 中控;请改用本地导航「中控登录」按钮。");
|
||
return;
|
||
}
|
||
const q = new URLSearchParams({ token, next: target });
|
||
const embedUrl = "/embed-auth?" + q.toString();
|
||
try {
|
||
window.parent.postMessage(
|
||
{
|
||
type: "hub:login-ok",
|
||
session_token: token,
|
||
next: target,
|
||
embed_auth_url: location.origin + embedUrl,
|
||
},
|
||
"*"
|
||
);
|
||
} catch (_) {}
|
||
submitBtn.textContent = "跳转中…";
|
||
submitBtn.disabled = true;
|
||
location.replace(embedUrl);
|
||
return;
|
||
}
|
||
location.href = target;
|
||
}
|
||
|
||
fetch("/api/auth/status")
|
||
.then((r) => r.json())
|
||
.then((s) => {
|
||
if (!s.required || s.logged_in) gotoAfterLogin(null, next);
|
||
if (s.username_hint && !userInput.value) userInput.value = s.username_hint;
|
||
})
|
||
.catch(() => {});
|
||
|
||
form.addEventListener("submit", async (e) => {
|
||
e.preventDefault();
|
||
err.hidden = true;
|
||
submitBtn.disabled = true;
|
||
const oldLabel = submitBtn.textContent;
|
||
submitBtn.textContent = "登录中…";
|
||
const username = userInput.value.trim();
|
||
const password = document.getElementById("login-password").value;
|
||
const headers = { "Content-Type": "application/json", Accept: "application/json" };
|
||
if (inFrame) headers["X-Hub-Embed"] = "1";
|
||
try {
|
||
const r = await fetch("/api/auth/login", {
|
||
method: "POST",
|
||
headers,
|
||
body: JSON.stringify({ username, password }),
|
||
});
|
||
let j = {};
|
||
try {
|
||
j = await r.json();
|
||
} catch (_) {
|
||
j = {};
|
||
}
|
||
if (r.ok && j.ok) {
|
||
gotoAfterLogin(j.session_token || null, next);
|
||
if (!inFrame) {
|
||
submitBtn.disabled = false;
|
||
submitBtn.textContent = oldLabel;
|
||
}
|
||
return;
|
||
}
|
||
if (r.status === 403) {
|
||
showErr("访问被拒绝(403):云端 hub 需设置 HUB_ALLOW_PUBLIC=true");
|
||
} else {
|
||
showErr(j.detail || j.msg || "用户名或密码错误 (" + r.status + ")");
|
||
}
|
||
} catch (ex) {
|
||
showErr("网络错误:" + ex);
|
||
}
|
||
submitBtn.disabled = false;
|
||
submitBtn.textContent = oldLabel;
|
||
});
|
||
})();
|
||
</script>
|
||
</body>
|
||
</html>
|