feat: 移动增加扫码登录并支持换号短信登录

This commit is contained in:
Nixevol
2026-10-16 02:12:08 +08:00
parent e49a72e3ca
commit 543fcce134
11 changed files with 1241 additions and 26 deletions
+77 -2
View File
@@ -161,7 +161,14 @@
}
.card { background: var(--card); border-radius: 12px; box-shadow: var(--shadow); }
#login { width: min(340px, 100%); margin: 0 auto; padding: 16px 14px 14px; }
#login h2 { margin: 0 0 12px; font-size: 16px; font-weight: 650; }
#login h2 { margin: 0; font-size: 16px; font-weight: 650; }
.login-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-bottom: 12px; }
.mode-switch { display: flex; gap: 4px; }
.mode-switch button { height: 28px; padding: 0 8px; }
.mode-switch button.on { color: var(--accent); border-color: var(--accent); }
#qrBox { display: flex; flex-direction: column; align-items: center; gap: 8px; padding: 4px 0 2px; }
#qrBox[hidden] { display: none; }
#qrImg { width: 168px; height: 168px; border: 1px solid var(--line); border-radius: 8px; background: #fff; cursor: pointer; }
.field { margin-bottom: 10px; }
label { display: block; margin-bottom: 4px; color: var(--muted); font-size: 12px; }
.login-row {
@@ -300,7 +307,13 @@
<div id="toast" class="toast" hidden></div>
<section id="login" class="card">
<h2>登录</h2>
<div class="login-head">
<h2>登录</h2>
<div class="mode-switch">
<button id="smsMode" class="on" type="button">短信</button>
<button id="qrMode" type="button">扫码</button>
</div>
</div>
<form id="form">
<div class="login-row">
<label for="phone">手机号</label>
@@ -327,6 +340,10 @@
<p id="loginMsg"></p>
<button id="submit" class="primary" type="submit">登录</button>
</form>
<div id="qrBox" hidden>
<img id="qrImg" alt="登录二维码" title="点击刷新">
<p id="qrHint">请用中国移动 App 扫码,打开后点登录</p>
</div>
</section>
<section id="pending" class="card" hidden>读取中</section>
@@ -423,6 +440,8 @@
const $ = (id) => document.getElementById(id);
let lastKey = "";
let captchaReady = false;
let qrTimer = 0;
let qrMode = false;
let smsLeft = 0;
let smsTimer = 0;
let deadline = 0;
@@ -450,6 +469,7 @@
lastKey = key;
loggedIn = !!data.loggedIn;
if (loggedIn) stopQR();
$("menu").hidden = !data.loggedIn;
$("countdown").hidden = !data.loggedIn;
$("refresh").disabled = !!data.refreshing;
@@ -661,6 +681,61 @@
$("settingsSave").addEventListener("click", () => {
saveInterval().catch(() => { $("settingsMsg").textContent = "未保存"; });
});
function stopQR() {
clearInterval(qrTimer);
qrTimer = 0;
}
function showSMS() {
qrMode = false;
stopQR();
$("form").hidden = false;
$("qrBox").hidden = true;
$("smsMode").classList.add("on");
$("qrMode").classList.remove("on");
}
async function startQR() {
qrMode = true;
stopQR();
$("form").hidden = true;
$("qrBox").hidden = false;
$("qrMode").classList.add("on");
$("smsMode").classList.remove("on");
setLoginMsg("");
$("qrHint").textContent = "请用中国移动 App 扫码,打开后点登录";
$("qrImg").src = "/api/qr?t=" + Date.now();
qrTimer = setInterval(async () => {
if (!qrMode) return;
try {
const res = await fetch("/api/qr/poll", { cache: "no-store" });
const data = await res.json();
if (data.expired) {
stopQR();
$("qrHint").textContent = data.message || "二维码已失效,点击图片刷新";
return;
}
if (!data.waiting && data.ok === false) {
stopQR();
const message = data.message || "扫码登录失败";
$("qrHint").textContent = message;
setLoginMsg(message);
return;
}
if (data.ok) {
stopQR();
lastKey = "";
await pull();
}
} catch (err) {
stopQR();
setLoginMsg("扫码登录失败");
}
}, 2000);
}
$("smsMode").addEventListener("click", () => showSMS());
$("qrMode").addEventListener("click", () => { startQR().catch(() => setLoginMsg("二维码加载失败")); });
$("qrImg").addEventListener("click", () => { if (qrMode) startQR().catch(() => setLoginMsg("二维码加载失败")); });
$("qrImg").addEventListener("error", () => { setLoginMsg("二维码加载失败"); });
$("captcha").addEventListener("click", () => { loadCaptcha(); captchaReady = true; });
$("captcha").addEventListener("error", () => { setLoginMsg("验证码加载失败"); });