From 03997150084df9d1bcdc1c72c5e473506f66e02c Mon Sep 17 00:00:00 2001 From: qing <1776177662@qq.com> Date: Tue, 21 Jul 2026 22:27:48 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=90=8E=E7=AB=AF=E7=99=BB?= =?UTF-8?q?=E9=99=86=E5=93=8D=E5=BA=94=E4=B8=BAjson=EF=BC=8C=E4=BF=AE?= =?UTF-8?q?=E5=A4=8D=E7=99=BB=E9=99=86=E9=A1=B5=E9=9D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- gateway/src/main.cpp | 6 +++++- static/public/js/login.js | 8 +++----- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/gateway/src/main.cpp b/gateway/src/main.cpp index 07eb437..a36983c 100644 --- a/gateway/src/main.cpp +++ b/gateway/src/main.cpp @@ -359,9 +359,13 @@ http::response handleLogin( std::string token = Base64::base64_encode(data); std::string sign = _PRIV.Sign(data); + json::value jv_rr = { + {"success", true}, + {"data", json::object{{"token", token + "_" + sign}}} // 双大括号 + }; res.set("Connection", "close"); res.result(http::status::ok); - res.body() = token + "_" + sign; + res.body() = json::serialize(jv_rr); } catch (const HttpException& e) { diff --git a/static/public/js/login.js b/static/public/js/login.js index 0993fae..96522d6 100644 --- a/static/public/js/login.js +++ b/static/public/js/login.js @@ -146,16 +146,14 @@ form.addEventListener('submit', async function(e) { throw new Error(result.message || `登录失败(HTTP ${res.status})`); } - if (result.code === 0) { - if (!result.data || !result.data.token || !result.data.username) { - throw new Error('登录成功,但服务器返回的数据不完整'); - } - alert(`${result.message},账号:${result.data.username}`); + if (result.success) { + alert(`登陆成功`); sessionStorage.setItem('token', result.data.token); location.href = 'main.html'; } else { alert(result.message || '登录失败'); } + } catch (err) { alert(err.message || '接口请求失败,请检查后端服务是否启动'); console.error('登录请求异常:', err);