Files
agent/app/server/public/login.html
T

124 lines
5.9 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, viewport-fit=cover, interactive-widget=resizes-content">
<title>Agent 任务台 · 登录</title>
<meta name="theme-color" content="#1e3c72">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
<meta name="apple-mobile-web-app-title" content="Agent">
<link rel="manifest" href="/manifest.json">
<link rel="apple-touch-icon" href="/icons/apple-touch-icon.png">
<link rel="stylesheet" href="/css/style.css">
<style>
.login-wrap {
/* bottom 让出软键盘:表单垂直居中,键盘弹起时卡片跟着可见区域上移(--kb 见 js/viewport.js) */
position: fixed; inset: 0; bottom: var(--kb, 0px); display: flex; align-items: center; justify-content: center;
background: linear-gradient(135deg, #1e3c72, #2a5299);
}
.login-card {
background: #fff; border-radius: 14px; padding: 28px 22px 22px; width: 84%; max-width: 340px;
}
.login-card h2 { font-size: 18px; margin-bottom: 18px; text-align: center; color: #223; }
.login-card input {
display: block; width: 100%; box-sizing: border-box;
border: 1px solid #dde; border-radius: 8px; padding: 11px 12px;
font-size: 15px; margin-bottom: 12px; outline: none;
}
.login-card input:focus { border-color: #2a5299; }
.login-error { color: #d2483c; font-size: 13px; min-height: 18px; margin: 6px 0 0; text-align: center; }
</style>
</head>
<body>
<div class="login-wrap">
<form class="login-card" id="login-form" onsubmit="return doLogin(event)">
<h2>Agent 任务台</h2>
<input type="text" id="login-user" placeholder="用户名" autocomplete="username" autocapitalize="none" autocorrect="off">
<input type="password" id="login-pass" placeholder="密码" autocomplete="current-password">
<button class="primary-btn" id="login-btn" type="submit" style="width:100%;">登录</button>
<p class="login-error" id="login-error"></p>
</form>
</div>
<script src="/js/viewport.js"></script>
<script>
'use strict';
const TOKEN_KEY = 'agent_token';
// 未认证访问页面时,服务端 302 到 /login?next=<原始 URL>;登录/恢复成功后回跳该页。
// 只认本站绝对路径,拦掉 //evil.com 与反斜杠混淆,防开放重定向;无/非法则回首页。
function redirectTarget() {
const n = new URLSearchParams(location.search).get('next') || '';
if (!n.startsWith('/') || n.startsWith('//') || n.indexOf('\\') !== -1) return '/';
return n;
}
// bfcache(前进/后退缓存)恢复会绕过服务端“已登录访问 /login → 302 回主页”的判定,
// 侧滑返回可能直接复活旧登录表单。能走到这里说明 /login 是历史残留条目,立刻踢回目标页;
// 若其实已登出,服务端会再 302 回 /login,拿到的也是新鲜表单而非僵尸页。
window.addEventListener('pageshow', e => {
if (e.persisted) location.replace(redirectTarget());
});
// WebView 封装 APK 杀进程后 HttpOnly cookie 可能没落盘,但 localStorage 还在:
// 登录成功后把 token 另存一份,下次进来先尝试用旧 token 静默恢复登录态。
async function autoRestore() {
if (new URLSearchParams(location.search).has('logged_out')) {
localStorage.removeItem(TOKEN_KEY); // 显式登出:不恢复,并清掉旧 token
sessionStorage.removeItem('agent_creds');
return;
}
const t = localStorage.getItem(TOKEN_KEY);
if (!t) return;
const form = document.getElementById('login-form');
form.style.visibility = 'hidden'; // 恢复期间先藏表单,避免闪现
try {
const res = await fetch('/api/login_restore', { method: 'POST', headers: { Authorization: 'Bearer ' + t } });
if (res.ok) {
const j = await res.json().catch(() => ({}));
if (j && j.token) localStorage.setItem(TOKEN_KEY, j.token); // 续期 token
location.replace(redirectTarget());
return;
}
} catch (_) { /* 网络错误交给表单正常登录 */ }
// 恢复失败(401等):清掉无效 token,显示登录表单
localStorage.removeItem(TOKEN_KEY);
sessionStorage.removeItem('agent_creds');
form.style.visibility = '';
}
async function doLogin(e) {
e.preventDefault();
const btn = document.getElementById('login-btn');
const errEl = document.getElementById('login-error');
errEl.textContent = '';
btn.disabled = true;
try {
const res = await fetch('/api/login', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
user: document.getElementById('login-user').value.trim(),
password: document.getElementById('login-pass').value,
}),
});
if (!res.ok) {
let msg = 'HTTP ' + res.status;
try { msg = (await res.json()).error || msg; } catch (_) {}
throw new Error(msg);
}
const j = await res.json().catch(() => ({}));
if (j && j.token) {
// 仅存签名 token 供 WebView 场景 login_restore 恢复;
// 不再往 sessionStorage 写明文口令——Basic 头与 cookie 作用域不同,会引发多标签页身份分裂
localStorage.setItem(TOKEN_KEY, j.token);
}
location.replace(redirectTarget()); // 替换而非压栈:历史里不留 /login 条目,登录后侧滑不会退回登录页
} catch (e2) {
errEl.textContent = e2.message || '登录失败';
btn.disabled = false;
}
return false;
}
autoRestore();
document.getElementById('login-user').focus();
</script>
</body>
</html>