admin-login.html 1.53 KB
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8" />
<title>管理员登录</title>
<link rel="stylesheet" href="style.css" />
<style>
  .login-wrap { max-width: 360px; margin: 15vh auto; padding: 24px; }
  .login-wrap h1 { font-size: 18px; margin-bottom: 16px; }
  .login-wrap input { width: 100%; margin-bottom: 12px; }
  .login-wrap .err { color: #c0392b; font-size: 13px; min-height: 18px; margin-bottom: 8px; }
</style>
</head>
<body>
<div class="login-wrap">
  <h1>管理员登录</h1>
  <input type="password" id="password" placeholder="管理员口令" autofocus />
  <div class="err" id="err"></div>
  <button id="loginBtn">登录</button>
</div>
<script>
  const params = new URLSearchParams(location.search);
  const next = params.get("next") || "/admin.html";

  async function login() {
    const password = document.getElementById("password").value;
    const errBox = document.getElementById("err");
    errBox.textContent = "";
    try {
      const res = await fetch("/api/admin/login", {
        method: "POST",
        headers: { "Content-Type": "application/json" },
        body: JSON.stringify({ password }),
      });
      if (!res.ok) {
        errBox.textContent = "口令错误";
        return;
      }
      location.href = next;
    } catch (e) {
      errBox.textContent = "登录请求失败: " + e.message;
    }
  }

  document.getElementById("loginBtn").onclick = login;
  document.getElementById("password").addEventListener("keydown", (e) => {
    if (e.key === "Enter") login();
  });
</script>
</body>
</html>