From 548314e22b85113d4e85a3264b8ed70774b5e56d Mon Sep 17 00:00:00 2001 From: JOJO <1498581755@qq.com> Date: Thu, 6 Aug 2026 10:16:49 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=8E=92=E8=A1=8C=E6=A6=9C?= =?UTF-8?q?=E5=9B=BE=E9=89=B4=E6=8E=92=E5=BA=8F500=EF=BC=9ALB=5FCOLS?= =?UTF-8?q?=E6=98=A0=E5=B0=84codex=5Fn=E4=B8=8ESQL=E5=88=AB=E5=90=8DcodexN?= =?UTF-8?q?=E4=B8=8D=E4=B8=80=E8=87=B4=EF=BC=88=E4=B8=8A=E4=B8=80=E8=BD=AE?= =?UTF-8?q?=E6=B5=8B=E8=AF=95=E6=BC=8F=E6=B5=8Bsort=3Dcodex=E5=88=86?= =?UTF-8?q?=E6=94=AF=EF=BC=8C=E6=9C=AC=E8=BD=AE=E4=B8=89=E6=8E=92=E5=BA=8F?= =?UTF-8?q?=E5=85=A8=E5=9B=9E=E5=BD=92=EF=BC=89=EF=BC=9B/api/me=E6=9C=AA?= =?UTF-8?q?=E7=99=BB=E5=BD=95=E6=94=B9=E8=BF=94200{user:null}=E6=B6=88?= =?UTF-8?q?=E9=99=A4=E5=90=AF=E5=8A=A8401=E6=8E=A7=E5=88=B6=E5=8F=B0?= =?UTF-8?q?=E5=99=AA=E9=9F=B3=EF=BC=8C=E4=BC=9A=E8=AF=9D=E8=AF=86=E5=88=AB?= =?UTF-8?q?=E5=AF=B9=E6=89=80=E6=9C=89=E8=B7=AF=E7=94=B1=E7=94=9F=E6=95=88?= =?UTF-8?q?=E3=80=81=E4=BB=85=E5=8F=97=E4=BF=9D=E6=8A=A4=E8=B7=AF=E7=94=B1?= =?UTF-8?q?=E5=BC=BA=E5=88=B6401?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- star-raid/js/auth.js | 2 +- star-raid/js/home.js | 5 +++-- star-raid/js/net.js | 4 ++-- star-raid/server/api.js | 7 ++++--- star-raid/server/server.js | 8 +++----- 5 files changed, 13 insertions(+), 13 deletions(-) diff --git a/star-raid/js/auth.js b/star-raid/js/auth.js index 2d2ce9b..53c9f3e 100644 --- a/star-raid/js/auth.js +++ b/star-raid/js/auth.js @@ -54,7 +54,7 @@ UI = "auth"; show("o-auth"); const r = await NET.api("/api/me", "GET"); - if (r.ok) { NET._apply(r.data); enterHome(); } + if (r.ok && r.data.user) { NET._apply(r.data); enterHome(); } else { if (r.status === 0) err("无法连接服务器,请稍后刷新重试"); show("o-auth"); diff --git a/star-raid/js/home.js b/star-raid/js/home.js index c87bfc5..35f8f63 100644 --- a/star-raid/js/home.js +++ b/star-raid/js/home.js @@ -18,8 +18,9 @@ async function enterHome() { show("o-home"); /* 刷新账号数据(stats/codex),再渲染面板与排行榜 */ const r = await NET.api("/api/me", "GET"); - if (r.ok) NET._apply(r.data); - else if (r.status === 401) { NET.user = null; UI = "auth"; show("o-auth"); return; } + if (r.ok && r.data.user) NET._apply(r.data); + else if (r.ok) { NET.user = null; UI = "auth"; show("o-auth"); return; } // 会话失效 + /* status===0 网络抖动:沿用现有数据,不踢出 */ buildHomeStats(); buildLeaderboard(LB_SORT); } diff --git a/star-raid/js/net.js b/star-raid/js/net.js index 2d7dc06..09008b4 100644 --- a/star-raid/js/net.js +++ b/star-raid/js/net.js @@ -23,10 +23,10 @@ const NET = { return { ok: res.ok, status: res.status, data }; }, - /* 启动:尝试恢复会话。true=已登录 */ + /* 启动:尝试恢复会话。true=已登录(未登录时服务端返回 200 {user:null}) */ async boot() { const r = await this.api("/api/me", "GET"); - if (r.ok) { this._apply(r.data); return true; } + if (r.ok && r.data.user) { this._apply(r.data); return true; } return false; }, diff --git a/star-raid/server/api.js b/star-raid/server/api.js index 9fbaab3..deed187 100644 --- a/star-raid/server/api.js +++ b/star-raid/server/api.js @@ -82,14 +82,15 @@ function logout(req, res, ctx) { return ctx.ok({ bye: true }); } -/* ---------- GET /api/me ---------- */ +/* ---------- GET /api/me(未登录返回 user:null,不产生 401 控制台噪音) ---------- */ function me(req, res, ctx) { const u = ctx.user; + if (!u) return ctx.ok({ user: null }); return ctx.ok({ user: { username: u.username, email: u.email, createdAt: u.created_at }, stats: statsOf(u.id), codex: codexOf(u.id), codexTotal: CODEX_TOTAL }); } /* ---------- GET /api/leaderboard?sort=endless|boss|codex ---------- */ -const LB_COLS = { endless: "endless_high", boss: "boss_kills", codex: "codex_n" }; +const LB_COLS = { endless: "endless_high", boss: "boss_kills", codex: "codexN" }; function leaderboard(req, res, ctx, _body, query) { const col = LB_COLS[(query && query.get("sort")) || ""] || "endless_high"; /* col 来自上方白名单映射,非用户输入直接拼接 */ @@ -134,7 +135,7 @@ const ROUTES = { "POST /api/register": { fn: register, auth: false }, "POST /api/login": { fn: login, auth: false }, "POST /api/logout": { fn: logout, auth: true }, - "GET /api/me": { fn: me, auth: true }, + "GET /api/me": { fn: me, auth: false }, "GET /api/leaderboard": { fn: leaderboard, auth: true }, "POST /api/run": { fn: run, auth: true }, }; diff --git a/star-raid/server/server.js b/star-raid/server/server.js index 4c97984..c33f1a7 100644 --- a/star-raid/server/server.js +++ b/star-raid/server/server.js @@ -52,11 +52,9 @@ async function handleApi(req, res, pathname, query) { const ctx = makeCtx(req, res); const route = ROUTES[req.method + " " + pathname]; if (!route) return ctx.fail(404, "not found"); - if (route.auth) { - const u = auth.getSessionUser(db, ctx.token); - if (!u) return ctx.fail(401, "未登录或会话已过期"); - ctx.user = u; - } + /* 有 session 就识别身份;route.auth 才强制要求登录 */ + if (ctx.token) ctx.user = auth.getSessionUser(db, ctx.token); + if (route.auth && !ctx.user) return ctx.fail(401, "未登录或会话已过期"); try { let body = null; /* logout 等无 body 的 POST 不强制 JSON;有 body 的必须 application/json */