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 ad117d7..96522d6 100644 --- a/static/public/js/login.js +++ b/static/public/js/login.js @@ -1,40 +1,40 @@ -const msgTip = document.getElementById('msgTip'); -const pwdTip = document.getElementById('pwdTip'); -const emlTip = document.getElementById('emlTip'); -const agreeTip = document.getElementById('agreeTip'); -const form = document.getElementById('form'); - -const usernameInput = document.getElementById('loginUsername'); -const passwordInput = document.getElementById('loginPassword'); -const emailInput = document.getElementById('loginEmail'); -const agreeBox = document.getElementById('agreeBox'); +const msgTip = document.getElementById('msgTip'); +const pwdTip = document.getElementById('pwdTip'); +const emlTip = document.getElementById('emlTip'); +const agreeTip = document.getElementById('agreeTip'); +const form = document.getElementById('form'); + +const usernameInput = document.getElementById('loginUsername'); +const passwordInput = document.getElementById('loginPassword'); +const emailInput = document.getElementById('loginEmail'); +const agreeBox = document.getElementById('agreeBox'); const protocol = document.getElementById('agreement'); const resetUser = document.getElementById('resetUser'); const registerBtn = document.getElementById('registerBtn'); const loginButton = document.getElementById('loginBtn'); - -resetUser.addEventListener('click', function() { - clearTipStyle(); + +resetUser.addEventListener('click', function() { + clearTipStyle(); }); - -function clearTipStyle() { - msgTip.innerHTML = ''; - pwdTip.innerHTML = ''; - agreeTip.innerHTML = ''; - emlTip.innerHTML = ''; - - msgTip.classList.remove('tip-red', 'tip-yellow', 'tip-green'); - pwdTip.classList.remove('tip-red', 'tip-yellow', 'tip-green'); - agreeTip.classList.remove('tip-red', 'tip-yellow', 'tip-green'); - emlTip.classList.remove('tip-red', 'tip-yellow', 'tip-green'); - protocol.classList.remove('tip-red'); - protocol.classList.remove('tip-green'); -} -clearTipStyle(); - -/** - * @returns {string|false} - */ + +function clearTipStyle() { + msgTip.innerHTML = ''; + pwdTip.innerHTML = ''; + agreeTip.innerHTML = ''; + emlTip.innerHTML = ''; + + msgTip.classList.remove('tip-red', 'tip-yellow', 'tip-green'); + pwdTip.classList.remove('tip-red', 'tip-yellow', 'tip-green'); + agreeTip.classList.remove('tip-red', 'tip-yellow', 'tip-green'); + emlTip.classList.remove('tip-red', 'tip-yellow', 'tip-green'); + protocol.classList.remove('tip-red'); + protocol.classList.remove('tip-green'); +} +clearTipStyle(); + +/** + * @returns {string|false} + */ function checkUsername() { const result = FormValidators.username(usernameInput.value); msgTip.innerHTML = ''; @@ -42,11 +42,11 @@ function checkUsername() { msgTip.textContent = result.message; msgTip.classList.add(result.className); return result.valid ? result.value : false; -} - -/** - * @returns {string|false} - */ +} + +/** + * @returns {string|false} + */ function checkPwd() { const result = FormValidators.password(passwordInput.value); pwdTip.innerHTML = ''; @@ -54,11 +54,11 @@ function checkPwd() { pwdTip.textContent = result.message; pwdTip.classList.add(result.className); return result.valid ? result.value : false; -} - -/** - * @returns {string|false} - */ +} + +/** + * @returns {string|false} + */ function checkEml() { const result = FormValidators.email(emailInput.value); emlTip.innerHTML = ''; @@ -66,71 +66,71 @@ function checkEml() { emlTip.textContent = result.message; emlTip.classList.add(result.className); return result.valid ? result.value : false; -} - -/** - * @returns {boolean} - */ -function checkAgree() { - agreeTip.innerHTML = ''; - agreeTip.classList.remove('tip-red'); - protocol.classList.remove('tip-green', 'tip-red'); - if (!agreeBox.checked) { - agreeTip.innerHTML = '请同意用户协议!'; - agreeTip.classList.add('tip-red'); - protocol.classList.add('tip-red'); - return false; - } - protocol.classList.add('tip-green'); - return true; -} - -usernameInput.addEventListener('focus', checkUsername); -passwordInput.addEventListener('focus', checkPwd); - -usernameInput.addEventListener('input', checkUsername); -passwordInput.addEventListener('input', checkPwd); - -usernameInput.addEventListener('blur', checkUsername); -passwordInput.addEventListener('blur', checkPwd); - -emailInput.addEventListener('focus', checkEml); -emailInput.addEventListener('input', checkEml); -emailInput.addEventListener('blur', checkEml); - -agreeBox.addEventListener('change', checkAgree); - -registerBtn.addEventListener('click', function() { - location.href = 'register.html'; +} + +/** + * @returns {boolean} + */ +function checkAgree() { + agreeTip.innerHTML = ''; + agreeTip.classList.remove('tip-red'); + protocol.classList.remove('tip-green', 'tip-red'); + if (!agreeBox.checked) { + agreeTip.innerHTML = '请同意用户协议!'; + agreeTip.classList.add('tip-red'); + protocol.classList.add('tip-red'); + return false; + } + protocol.classList.add('tip-green'); + return true; +} + +usernameInput.addEventListener('focus', checkUsername); +passwordInput.addEventListener('focus', checkPwd); + +usernameInput.addEventListener('input', checkUsername); +passwordInput.addEventListener('input', checkPwd); + +usernameInput.addEventListener('blur', checkUsername); +passwordInput.addEventListener('blur', checkPwd); + +emailInput.addEventListener('focus', checkEml); +emailInput.addEventListener('input', checkEml); +emailInput.addEventListener('blur', checkEml); + +agreeBox.addEventListener('change', checkAgree); + +registerBtn.addEventListener('click', function() { + location.href = 'register.html'; }); - -form.addEventListener('submit', async function(e) { - e.preventDefault(); - clearTipStyle(); - - const userOk = checkUsername(); - const pwdOk = checkPwd(); - const emlOk = checkEml(); - const agreeOk = checkAgree(); - - if (!(userOk && pwdOk && emlOk && agreeOk)) { - return; - } - - const jsonBody = JSON.stringify({ - username: userOk, - password: pwdOk - }); + +form.addEventListener('submit', async function(e) { + e.preventDefault(); + clearTipStyle(); + + const userOk = checkUsername(); + const pwdOk = checkPwd(); + const emlOk = checkEml(); + const agreeOk = checkAgree(); + + if (!(userOk && pwdOk && emlOk && agreeOk)) { + return; + } + + const jsonBody = JSON.stringify({ + username: userOk, + password: pwdOk + }); try { loginButton.disabled = true; const res = await fetch('/login', { - method: 'POST', - headers: { - 'Content-Type': 'application/x-www-form-urlencoded' - }, - body: jsonBody - }); + method: 'POST', + headers: { + 'Content-Type': 'application/json' // 告诉服务器内容为json + }, + body: jsonBody + }); const responseText = await res.text(); let result; @@ -146,19 +146,17 @@ 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}`); - sessionStorage.setItem('token', result.data.token); - location.href = 'main.html'; - } else { + if (result.success) { + alert(`登陆成功`); + sessionStorage.setItem('token', result.data.token); + location.href = 'main.html'; + } else { alert(result.message || '登录失败'); - } - } catch (err) { + } + + } catch (err) { alert(err.message || '接口请求失败,请检查后端服务是否启动'); - console.error('登录请求异常:', err); + console.error('登录请求异常:', err); } finally { loginButton.disabled = false; } diff --git a/static/public/js/register.js b/static/public/js/register.js index f5f5bd0..d170438 100644 --- a/static/public/js/register.js +++ b/static/public/js/register.js @@ -183,11 +183,8 @@ form.addEventListener('submit', async function (e) { throw new Error(result.message || `注册失败(HTTP ${res.status})`); } - if (result.code === 0) { - if (!result.data || !result.data.username) { - throw new Error('注册成功,但服务器返回的数据不完整'); - } - alert(`${result.message},账号:${result.data.username}`); + if (result.success) { + alert('注册成功'); form.reset(); sendCode = ''; codeTarget = ''; @@ -196,6 +193,7 @@ form.addEventListener('submit', async function (e) { } alert(result.message || '注册失败'); + } catch (err) { console.error('注册请求异常:', err); alert(err.message || '接口请求失败,请检查后端服务是否启动'); diff --git a/static/public/login.html b/static/public/login.html index 28db766..d995f3b 100644 --- a/static/public/login.html +++ b/static/public/login.html @@ -1,47 +1,48 @@ - - + + - 登录 - - -

用户登录

-
-
- - - -
- - - -
-
- - - - - - -
-
- - - -
-
- - - -
-
+ +

用户登录

+
+
+ + + +
+ + + +
+
+ + + + + + +
+
+ + + +
+
+ + + +
+
+ - + diff --git a/static/public/main.html b/static/public/main.html index 85c08f4..6995789 100644 --- a/static/public/main.html +++ b/static/public/main.html @@ -1,102 +1,102 @@ - - - - - - - DNFS · 树状笔记 - - - - -
-

-

Python

-

-
- - -
-

发布时间:2026-07-11

-
-
- 浏览:126 - 点赞:28 - 分享:7 -
- - -
-

- Python 的 socket - 模块是网络编程的核心,其函数可分为核心套接字操作、套接字选项设置、辅助工具函数三大类。本文将逐一拆解所有函数的语法、参数、用法和实战示例,覆盖从基础到高级的所有场景。 -

- -

一、模块初始化与核心对象

-

1. socket.socket():创建套接字对象

-

语法:

-
socket.socket(family=AF_INET, type=SOCK_STREAM, proto=0, fileno=None)
-

作用:创建一个套接字对象,是所有网络操作的基础。

-

参数说明:

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
参数取值说明
familyAF_INET(默认)IPv4 协议族(最常用)
AF_INET6IPv6 协议族
AF_UNIXUnix 域套接字(本地进程通信)
typeSOCK_STREAM(默认)TCP 套接字(面向连接、可靠)
SOCK_DGRAMUDP 套接字(无连接、数据报)
SOCK_RAW原始套接字(可操作 IP 层,用于抓包)
proto0(默认)协议编号,默认自动匹配 type(TCP=6,UDP=17)
filenoNone(默认)复用已有文件描述符,无需手动指定
-
-
-

其他推荐

-
-
- -
- + + + + + + + DNFS · 树状笔记 + + + + +
+

+

Python

+

+
+ + +
+

发布时间:2026-07-11

+
+
+ 浏览:126 + 点赞:28 + 分享:7 +
+ + +
+

+ Python 的 socket + 模块是网络编程的核心,其函数可分为核心套接字操作、套接字选项设置、辅助工具函数三大类。本文将逐一拆解所有函数的语法、参数、用法和实战示例,覆盖从基础到高级的所有场景。 +

+ +

一、模块初始化与核心对象

+

1. socket.socket():创建套接字对象

+

语法:

+
socket.socket(family=AF_INET, type=SOCK_STREAM, proto=0, fileno=None)
+

作用:创建一个套接字对象,是所有网络操作的基础。

+

参数说明:

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
参数取值说明
familyAF_INET(默认)IPv4 协议族(最常用)
AF_INET6IPv6 协议族
AF_UNIXUnix 域套接字(本地进程通信)
typeSOCK_STREAM(默认)TCP 套接字(面向连接、可靠)
SOCK_DGRAMUDP 套接字(无连接、数据报)
SOCK_RAW原始套接字(可操作 IP 层,用于抓包)
proto0(默认)协议编号,默认自动匹配 type(TCP=6,UDP=17)
filenoNone(默认)复用已有文件描述符,无需手动指定
+
+
+

其他推荐

+
+
+ +
+ \ No newline at end of file diff --git a/static/public/register.html b/static/public/register.html index 9213fce..82157fa 100644 --- a/static/public/register.html +++ b/static/public/register.html @@ -1,32 +1,32 @@ - + - 注册页面 - - -

注册页面

+ +

注册页面

-
- - - -
- +
+ + + +
+
验证码:
- -
+ +