fix:优化当前的项目
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
const $ = (id) => document.getElementById(id);
|
||||
let challengeId = "";
|
||||
|
||||
function setStatus(msg, danger = false) {
|
||||
const el = $("status");
|
||||
@@ -24,6 +25,21 @@ async function postJSON(url, body) {
|
||||
return data;
|
||||
}
|
||||
|
||||
async function refreshChallenge() {
|
||||
try {
|
||||
const res = await fetch("/api/auth/challenge");
|
||||
const data = await res.json();
|
||||
if (!res.ok || !data.ok) throw new Error(data.detail || "校验题加载失败");
|
||||
challengeId = (data.challenge_id || "").trim();
|
||||
const label = $("challengeLabel");
|
||||
if (label) label.textContent = `人机校验:${data.question || ""}`;
|
||||
const ans = $("challengeAnswer");
|
||||
if (ans) ans.value = "";
|
||||
} catch {
|
||||
setStatus("校验题加载失败,请刷新页面重试", true);
|
||||
}
|
||||
}
|
||||
|
||||
function nextPath() {
|
||||
const nxt = (window.__NEXT_PATH__ || "/").trim();
|
||||
if (!nxt.startsWith("/")) return "/";
|
||||
@@ -35,6 +51,9 @@ function fields() {
|
||||
username: ($("username") && $("username").value.trim()) || "",
|
||||
password: ($("password") && $("password").value) || "",
|
||||
remember_me: Boolean($("rememberMe") && $("rememberMe").checked),
|
||||
challenge_id: challengeId,
|
||||
challenge_answer: ($("challengeAnswer") && $("challengeAnswer").value.trim()) || "",
|
||||
honeypot: ($("botTrap") && $("botTrap").value) || "",
|
||||
};
|
||||
}
|
||||
|
||||
@@ -57,6 +76,7 @@ async function authAction(url, button, idleText, loadingText, okMessage) {
|
||||
|
||||
const loginBtn = $("loginBtn");
|
||||
const registerBtn = $("registerBtn");
|
||||
const refreshChallengeBtn = $("refreshChallengeBtn");
|
||||
|
||||
if (loginBtn) {
|
||||
loginBtn.addEventListener("click", async () => {
|
||||
@@ -78,7 +98,7 @@ if (registerBtn) {
|
||||
const msg =
|
||||
`注册成功!请务必保存你的重置码(找回密码唯一凭证):\n\n${code}\n\n` +
|
||||
"请立即复制并妥善保管,点击“确定”后继续进入系统。";
|
||||
window.alert(msg);
|
||||
await window.uiAlert(msg, "注册成功");
|
||||
try {
|
||||
if (navigator.clipboard && navigator.clipboard.writeText) {
|
||||
await navigator.clipboard.writeText(code);
|
||||
@@ -91,8 +111,19 @@ if (registerBtn) {
|
||||
window.location.href = nextPath();
|
||||
} catch (e) {
|
||||
setStatus(e.message || "请求异常", true);
|
||||
await refreshChallenge();
|
||||
} finally {
|
||||
setLoading(registerBtn, false, "注册", "注册中...");
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
if (refreshChallengeBtn) {
|
||||
refreshChallengeBtn.addEventListener("click", async () => {
|
||||
setLoading(refreshChallengeBtn, true, "刷新题目", "刷新中...");
|
||||
await refreshChallenge();
|
||||
setLoading(refreshChallengeBtn, false, "刷新题目", "刷新中...");
|
||||
});
|
||||
}
|
||||
|
||||
refreshChallenge();
|
||||
|
||||
Reference in New Issue
Block a user