Home
About
Blog
Products
Forum
Support
Contact
Sunbelt Computer Software
PL/B Language Development and Support
Home
About
Blog
Products
Forum
Support
Contact
Habbit/src/SimpleAuthSystem.cpp at main · DeepCodingInTuringAcademy/Habbit · GitHub
Skip to content
Navigation Menu
Sign in
Appearance settings
Platform
AI CODE CREATION
GitHub Copilot
Write better code with AI
GitHub Copilot app
Direct agents from issue to merge
MCP Registry
Integrate external tools
DEVELOPER WORKFLOWS
Actions
Automate any workflow
Codespaces
Instant dev environments
Issues
Plan and track work
Code Review
Manage code changes
Code Quality
Enforce quality at merge
APPLICATION SECURITY
GitHub Advanced Security
Find and fix vulnerabilities
Code security
Secure your code as you build
Secret protection
Stop leaks before they start
EXPLORE
Why GitHub
Documentation
Blog
Changelog
Marketplace
View all features
Solutions
BY COMPANY SIZE
Enterprises
Small and medium teams
Startups
Nonprofits
BY USE CASE
App Modernization
DevSecOps
DevOps
CI/CD
View all use cases
BY INDUSTRY
Healthcare
Financial services
Manufacturing
Government
View all industries
View all solutions
Resources
EXPLORE BY TOPIC
AI
Software Development
DevOps
Security
View all topics
EXPLORE BY TYPE
Customer stories
Events & webinars
Ebooks & reports
Business insights
GitHub Skills
SUPPORT & SERVICES
Documentation
Customer support
Community forum
Trust center
Partners
View all resources
Open Source
COMMUNITY
GitHub Sponsors
Fund open source developers
PROGRAMS
Security Lab
Maintainer Community
GitHub Stars
Archive Program
REPOSITORIES
Topics
Trending
Collections
Enterprise
ENTERPRISE SOLUTIONS
Enterprise platform
AI-powered developer platform
AVAILABLE ADD-ONS
GitHub Advanced Security
Enterprise-grade security features
Copilot for Business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
Uh oh!
There was an error while loading.
Please reload this page
.
DeepCodingInTuringAcademy
/
Habbit
Public
Notifications
You must be signed in to change notification settings
Fork
0
Star
5
Code
Issues
2
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Files
Expand file tree
main
Breadcrumbs
Habbit
/
src
/
SimpleAuthSystem.cpp
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
259 lines (208 loc) · 8.4 KB
main
Breadcrumbs
Habbit
/
src
/
SimpleAuthSystem.cpp
Copy path
Top
File metadata and controls
Code
Blame
259 lines (208 loc) · 8.4 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
#
include
"
SimpleAuthSystem.h
"
#
include
<
QMessageBox
>
#
include
<
QJsonParseError
>
SimpleAuthSystem::SimpleAuthSystem
(Settings &settings_ref, QWidget *parent)
: QWidget(parent), userId(
0
), settings(settings_ref)
{
//
设置服务器URL
serverUrl =
"
http://47.102.155.138:5000
"
;
//
创建UI组件
QVBoxLayout *layout =
new
QVBoxLayout
(
this
);
usernameEdit =
new
QLineEdit
(
this
);
usernameEdit->
setPlaceholderText
(
"
用户名
"
);
layout->
addWidget
(usernameEdit);
passwordEdit =
new
QLineEdit
(
this
);
passwordEdit->
setPlaceholderText
(
"
密码
"
);
passwordEdit->
setEchoMode
(QLineEdit::Password);
layout->
addWidget
(passwordEdit);
emailEdit =
new
QLineEdit
(
this
);
emailEdit->
setPlaceholderText
(
"
邮箱 (注册用)
"
);
layout->
addWidget
(emailEdit);
nicknameEdit =
new
QLineEdit
(
this
);
nicknameEdit->
setPlaceholderText
(
"
昵称
"
);
layout->
addWidget
(nicknameEdit);
skinEdit =
new
QLineEdit
(
this
);
skinEdit->
setPlaceholderText
(
"
皮肤 (default/dark/light)
"
);
layout->
addWidget
(skinEdit);
loginButton =
new
QPushButton
(
"
登录
"
,
this
);
layout->
addWidget
(loginButton);
registerButton =
new
QPushButton
(
"
注册
"
,
this
);
layout->
addWidget
(registerButton);
updateButton =
new
QPushButton
(
"
更新设置
"
,
this
);
layout->
addWidget
(updateButton);
logoutButton =
new
QPushButton
(
"
登出
"
,
this
);
layout->
addWidget
(logoutButton);
statusLabel =
new
QLabel
(
this
);
layout->
addWidget
(statusLabel);
//
连接信号和槽
connect
(loginButton, &QPushButton::clicked,
this
, &SimpleAuthSystem::onLoginClicked);
connect
(registerButton, &QPushButton::clicked,
this
, &SimpleAuthSystem::onRegisterClicked);
connect
(updateButton, &QPushButton::clicked,
this
, &SimpleAuthSystem::onUpdateSettingsClicked);
connect
(logoutButton, &QPushButton::clicked,
this
, &SimpleAuthSystem::onLogoutClicked);
setWindowTitle
(
"
简单认证系统
"
);
resize
(
300
,
400
);
}
//
辅助函数:发送HTTP请求
void
SimpleAuthSystem::sendHttpRequest
(
const
QString &endpoint,
const
QString &data,
const
QString &method,
const
QString &token,
std::function<
void
(
bool
,
const
QJsonDocument &)> callback)
{
QProcess *process =
new
QProcess
(
this
);
QStringList args;
args <<
"
-s
"
;
//
静默模式
args <<
"
-X
"
<< method;
//
请求方法
args <<
"
-H
"
<<
"
Content-Type: application/json
"
;
if
(!token.
isEmpty
())
{
args <<
"
-H
"
<<
QString
(
"
Authorization: Bearer %1
"
).
arg
(token);
}
if
(!data.
isEmpty
())
{
args <<
"
-d
"
<< data;
}
QString url = serverUrl + endpoint;
args << url;
statusLabel->
setText
(
"
正在发送请求...
"
);
connect
(process, &QProcess::finished,
this
, [=,
this
](
int
exitCode, QProcess::ExitStatus exitStatus)
{
QString response =
QString::fromUtf8
(process->
readAllStandardOutput
());
QString error =
QString::fromUtf8
(process->
readAllStandardError
());
bool
success = (exitCode ==
0
&& exitStatus == QProcess::NormalExit);
if
(!success) {
statusLabel->
setText
(
"
请求失败:
"
+ error);
process->
deleteLater
();
return
;
}
//
解析JSON响应
QJsonParseError jsonError;
QJsonDocument jsonDoc =
QJsonDocument::fromJson
(response.
toUtf8
(), &jsonError);
if
(jsonError.
error
!= QJsonParseError::NoError) {
statusLabel->
setText
(
"
JSON解析错误:
"
+ jsonError.
errorString
());
process->
deleteLater
();
return
;
}
callback
(
true
, jsonDoc);
process->
deleteLater
(); });
process->
start
(
"
curl
"
, args);
}
void
SimpleAuthSystem::onLoginClicked
()
{
QString username = usernameEdit->
text
().
trimmed
();
QString password = passwordEdit->
text
();
if
(username.
isEmpty
() || password.
isEmpty
())
{
statusLabel->
setText
(
"
用户名和密码不能为空
"
);
return
;
}
//
准备JSON数据
QJsonObject jsonObj;
jsonObj[
"
username
"
] = username;
jsonObj[
"
password
"
] = password;
QJsonDocument
doc
(jsonObj);
QString jsonData =
QString::fromUtf8
(doc.
toJson
());
//
发送请求
sendHttpRequest
(
"
/api/auth/login
"
, jsonData,
"
POST
"
,
"
"
, [
this
](
bool
success,
const
QJsonDocument &response)
{
if
(!success)
return
;
QJsonObject responseObj = response.
object
();
if
(responseObj[
"
status
"
].
toString
() ==
"
success
"
) {
accessToken = responseObj[
"
access_token
"
].
toString
();
userId = responseObj[
"
user
"
].
toObject
()[
"
id
"
].
toInt
();
QString nickname = responseObj[
"
user
"
].
toObject
()[
"
nickname
"
].
toString
();
settings.
setUserID
(userId);
settings.
setUserNickname
(nickname.
toStdString
());
statusLabel->
setText
(
"
登录成功
"
);
}
else
{
QString message = responseObj.
contains
(
"
message
"
) ?
responseObj[
"
message
"
].
toString
() :
"
未知错误
"
;
statusLabel->
setText
(
"
登录失败:
"
+ message);
} });
}
void
SimpleAuthSystem::onRegisterClicked
()
{
QString username = usernameEdit->
text
().
trimmed
();
QString password = passwordEdit->
text
();
QString email = emailEdit->
text
().
trimmed
();
QString nickname = nicknameEdit->
text
().
trimmed
();
if
(username.
isEmpty
() || password.
isEmpty
() || email.
isEmpty
())
{
statusLabel->
setText
(
"
用户名、密码和邮箱不能为空
"
);
return
;
}
//
准备JSON数据
QJsonObject jsonObj;
jsonObj[
"
username
"
] = username;
jsonObj[
"
password
"
] = password;
jsonObj[
"
email
"
] = email;
if
(!nickname.
isEmpty
())
{
jsonObj[
"
nickname
"
] = nickname;
}
QJsonDocument
doc
(jsonObj);
QString jsonData =
QString::fromUtf8
(doc.
toJson
());
//
发送请求
sendHttpRequest
(
"
/api/auth/register
"
, jsonData,
"
POST
"
,
"
"
, [
this
](
bool
success,
const
QJsonDocument &response)
{
if
(!success)
return
;
QJsonObject responseObj = response.
object
();
if
(responseObj[
"
status
"
].
toString
() ==
"
success
"
) {
accessToken = responseObj[
"
access_token
"
].
toString
();
userId = responseObj[
"
user
"
].
toObject
()[
"
id
"
].
toInt
();
QString nickname = responseObj[
"
user
"
].
toObject
()[
"
nickname
"
].
toString
();
settings.
setUserID
(userId);
settings.
setUserNickname
(nickname.
toStdString
());
statusLabel->
setText
(
"
注册成功
"
);
}
else
{
QString message = responseObj.
contains
(
"
message
"
) ?
responseObj[
"
message
"
].
toString
() :
"
未知错误
"
;
statusLabel->
setText
(
"
注册失败:
"
+ message);
} });
}
void
SimpleAuthSystem::onUpdateSettingsClicked
()
{
if
(accessToken.
isEmpty
())
{
statusLabel->
setText
(
"
请先登录
"
);
return
;
}
QString nickname = nicknameEdit->
text
().
trimmed
();
QString skin = skinEdit->
text
().
trimmed
();
if
(nickname.
isEmpty
())
{
statusLabel->
setText
(
"
昵称不能为空
"
);
return
;
}
//
准备JSON数据
QJsonObject jsonObj;
jsonObj[
"
nickname
"
] = nickname;
jsonObj[
"
current_skin
"
] = skin.
isEmpty
() ?
"
default
"
: skin;
QJsonDocument
doc
(jsonObj);
QString jsonData =
QString::fromUtf8
(doc.
toJson
());
//
发送请求
sendHttpRequest
(
"
/api/auth/settings
"
, jsonData,
"
PUT
"
, accessToken, [
this
](
bool
success,
const
QJsonDocument &response)
{
if
(!success)
return
;
QJsonObject responseObj = response.
object
();
if
(responseObj[
"
status
"
].
toString
() ==
"
success
"
) {
statusLabel->
setText
(
"
设置更新成功
"
);
}
else
{
QString message = responseObj.
contains
(
"
message
"
) ?
responseObj[
"
message
"
].
toString
() :
"
未知错误
"
;
statusLabel->
setText
(
"
更新设置失败:
"
+ message);
} });
}
void
SimpleAuthSystem::onLogoutClicked
()
{
if
(accessToken.
isEmpty
())
{
statusLabel->
setText
(
"
未登录
"
);
return
;
}
//
发送请求
sendHttpRequest
(
"
/api/auth/logout
"
,
"
"
,
"
POST
"
, accessToken, [
this
](
bool
success,
const
QJsonDocument &response)
{
//
无论服务器响应如何,都清除本地令牌
accessToken =
"
"
;
userId =
0
;
statusLabel->
setText
(
"
已登出
"
); });
}
You can’t perform that action at this time.