starraid/star-raid/js/auth.js

64 lines
2.1 KiB
JavaScript
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"use strict";
/* ================= 认证流程:登陆/注册/登出 + 启动引导 ================= */
(function () {
const err = (t) => { $("auth-err").textContent = t || " "; };
function tab(login) {
$("tab-login").classList.toggle("primary", login);
$("tab-reg").classList.toggle("primary", !login);
$("f-login").classList.toggle("hide", !login);
$("f-reg").classList.toggle("hide", login);
err();
}
$("tab-login").onclick = () => tab(true);
$("tab-reg").onclick = () => tab(false);
$("f-login").addEventListener("submit", async (e) => {
e.preventDefault();
err("登陆中...");
const msg = await NET.login($("li-id").value.trim(), $("li-pw").value);
if (msg) { err(msg); SFX.hurt(); return; }
err(); SFX.level(); enterHome();
});
$("f-reg").addEventListener("submit", async (e) => {
e.preventDefault();
const name = $("rg-name").value.trim(), email = $("rg-email").value.trim();
const pw = $("rg-pw").value, pw2 = $("rg-pw2").value;
if (pw !== pw2) { err("两次输入的密码不一致"); SFX.hurt(); return; }
err("注册中...");
const msg = await NET.register(name, email, pw);
if (msg) { err(msg); SFX.hurt(); return; }
err(); SFX.level(); enterHome();
});
$("b-logout").onclick = async () => {
SFX.pick();
await NET.logout();
G = null; UI = "auth";
$("li-pw").value = "";
show("o-auth");
};
/* 进入基地(游戏主菜单) */
$("b-base").onclick = () => {
SFX.pick();
$("pilot-line").textContent = NET.user ? ("飞行员 " + NET.user.username) : " ";
bestLine(); UI = "title"; show("o-title");
};
$("b-lobby").onclick = () => { SFX.pick(); enterHome(); };
/* 启动引导:恢复会话 → 大厅;否则登陆页(网络故障给出提示) */
(async function boot() {
UI = "auth";
show("o-auth");
const r = await NET.api("/api/me", "GET");
if (r.ok && r.data.user) { NET._apply(r.data); enterHome(); }
else {
if (r.status === 0) err("无法连接服务器,请稍后刷新重试");
show("o-auth");
}
})();
})();