Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander-Porter committed Jul 5, 2024
2 parents 8ea433a + 89f841d commit cc1d441
Show file tree
Hide file tree
Showing 11 changed files with 81 additions and 35 deletions.
29 changes: 20 additions & 9 deletions apirequest.cpp
Original file line number Diff line number Diff line change
@@ -1,33 +1,44 @@
#include "apirequest.h"

QByteArray ApiRequest::get(const QString &path) {
ApiResponse ApiRequest::get(const QString &path) {
QNetworkRequest request(QUrl(baseUrl + path));
QNetworkReply* reply = manager->get(request);
QEventLoop loop;
connect(reply, &QNetworkReply::finished, &loop, &QEventLoop::quit);
loop.exec();
if (reply->error() == QNetworkReply::NoError) {
if (reply->error() == QNetworkReply::NoError||reply->error()>=401) {
QByteArray responseData = reply->readAll();
response=response->processJson(responseData);
ApiResponse response(responseData);
return response;
} else {
qDebug() << "Error:" << reply->errorString();
QString jsonString = R"({"code": 666, "message": "ApiRequest error!", "data": "Nooo!"})";
QByteArray jsonData = jsonString.toUtf8();
QJsonDocument document = QJsonDocument::fromJson(jsonData);
QJsonObject jsonObject = document.object();
ApiResponse errorResponse(666,"ApiRequest error!",jsonObject);
return errorResponse;
}
return 0;
}

QByteArray ApiRequest::post(const QString &path, const QByteArray &data) {
ApiResponse ApiRequest::post(const QString &path, const QByteArray &data) {
QNetworkRequest request(QUrl(baseUrl + path));
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
QNetworkReply* reply = manager->post(request, data);
QEventLoop loop;
connect(reply, &QNetworkReply::finished, &loop, &QEventLoop::quit);
loop.exec();
if (reply->error() == QNetworkReply::NoError) {
if (reply->error() == QNetworkReply::NoError||reply->error()>=401) {
QByteArray responseData = reply->readAll();
response=response->processJson(responseData);
qDebug() << "response code:" << response->getCode();
ApiResponse response(responseData);
return response;
} else {
qDebug() << "Error:" << reply->errorString();
QString jsonString = R"({"code": 666, "message": "ApiRequest error!", "data": "Nooo!"})";
QByteArray jsonData = jsonString.toUtf8();
QJsonDocument document = QJsonDocument::fromJson(jsonData);
QJsonObject jsonObject = document.object();
ApiResponse errorResponse(666,"ApiRequest error!",jsonObject);
return errorResponse;
}
return 0;
}
6 changes: 2 additions & 4 deletions apirequest.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ class ApiRequest : public QObject
public:
explicit ApiRequest(QObject *parent = nullptr){
manager = new QNetworkAccessManager(this);
response = new ApiResponse(this);
};

// 设置 API 主域名
Expand All @@ -22,15 +21,14 @@ class ApiRequest : public QObject
}

// 发起 GET 请求
QByteArray get(const QString& path);
ApiResponse get(const QString& path);

// 发起 POST 请求
QByteArray post(const QString& path, const QByteArray& data);
ApiResponse post(const QString& path, const QByteArray& data);

private:
QString baseUrl;
QNetworkAccessManager* manager;
ApiResponse* response;
};

#endif // APIREQUEST_H
12 changes: 9 additions & 3 deletions apiresponse.cpp
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
#include "Apiresponse.h"

ApiResponse* ApiResponse::processJson(const QByteArray &responseData)
{

ApiResponse::ApiResponse(const QByteArray &responseData){
QJsonDocument jsonDoc = QJsonDocument::fromJson(responseData);
if (jsonDoc.isObject()) {
QJsonObject jsonObject = jsonDoc.object();
processJsonObject(jsonObject);
} else {
qDebug() << "Invalid JSON format";
}
return this;
}

bool ApiResponse::isSuccess()
{
if(code==200||code==201)
return true;
else return false;
}

void ApiResponse::processJsonObject(const QJsonObject &jsonObject)
Expand Down
8 changes: 5 additions & 3 deletions apiresponse.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
class ApiResponse
{
public:
ApiResponse(QObject *parent = nullptr){};
ApiResponse(int codex,QString messagex,
QJsonObject datax):code(codex),message(messagex),data(datax){};
ApiResponse(const QByteArray &responseData);

ApiResponse* processJson(const QByteArray &responseData); //处理json
void processJsonObject(const QJsonObject &jsonObject); //处理json内容
int getCode(){
return code;
}
Expand All @@ -24,8 +24,10 @@ class ApiResponse
QJsonObject getData(){
return data;
}
bool isSuccess();

private:
void processJsonObject(const QJsonObject &jsonObject); //处理json内容
int code;
QString message;
QJsonObject data;
Expand Down
18 changes: 13 additions & 5 deletions loginwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,21 @@ void LoginWindow::on_loadBtn_clicked()
{
QMessageBox::information(this, "错误","请输入密码!");
}
else if(ui->account->text()!=ui->password->text())
{
QMessageBox::information(this, "错误","账号或密码错误!");
}
//else if(ui->account->text()!=ui->password->text())
//{
// QMessageBox::information(this, "错误","账号或密码错误!");
//}
else
{
this->close();
//测试用户
//User loginuser("newuser@example.com","123456");
User loginuser(ui->account->text(),ui->password->text());
if(loginuser.login()){
QMessageBox::information(this, "成功","登录成功");
this->close();
}
else
QMessageBox::critical(this, "失败","登录失败");
//mainforms->show();
}
}
Expand Down
1 change: 1 addition & 0 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ int main(int argc, char *argv[])
{
QApplication a(argc, argv);


//测试用户
User loginuser("newuser@example.com","123456");
loginuser.login();
Expand Down
11 changes: 10 additions & 1 deletion mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,23 @@ MainWindow::MainWindow(QWidget *parent)

QString testKey_1;
QString testKey_2;
addPageNode("HOME", _homePage, ElaIconType::House);
QString testKey_3;
QString testKey_4;

addExpanderNode("同步功能",testKey_2,ElaIconType::House);
addPageNode("正在同步",new QWidget(this),testKey_2,ElaIconType::Cloud);
addPageNode("历史同步",new QWidget(this),testKey_2,ElaIconType::CheckToSlot);
addPageNode("同步文件夹管理",new QWidget(this),testKey_2,ElaIconType::FolderClosed);
addExpanderNode("版本控制",testKey_3,ElaIconType::EnvelopeOpenText);
addPageNode("查看历史",new QWidget(this),testKey_3,ElaIconType::CalendarClock);
addExpanderNode("个人功能",testKey_4,ElaIconType::User);
addPageNode("修改信息",new QWidget(this),testKey_4,ElaIconType::Text);
addPageNode("注销账号",new QWidget(this),testKey_4,ElaIconType::UserSlash);
addPageNode("退出登录",new QWidget(this),testKey_4,ElaIconType::ArrowRightFromBracket);


//下拉菜单
addPageNode("HOME", _homePage, ElaIconType::House);
addExpanderNode("ElaDxgi", _elaDxgiKey, ElaIconType::TvMusic);
//addPageNode("ElaScreen", _elaScreenPage, _elaDxgiKey, 3, ElaIconType::ObjectGroup);
// navigation(elaScreenWidget->property("ElaPageKey").toString());
Expand Down
8 changes: 7 additions & 1 deletion signin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,13 @@ void signin::on_signinBtn_clicked()
}
else
{
QMessageBox::information(this, "成功","注册成功");
//测试用户
//User enrolluser("newuser@example.com","123456");
User enrolluser(ui->ID->text(),ui->account->text(),ui->password->text());
if(enrolluser.enroll())
QMessageBox::information(this, "成功","注册成功");
else
QMessageBox::critical(this, "失败","注册失败");
emit goback();
}
}
Expand Down
1 change: 1 addition & 0 deletions signin.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define SIGNIN_H

#include <QWidget>
#include "user.h"

namespace Ui {
class signin;
Expand Down
16 changes: 10 additions & 6 deletions user.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,25 @@ User::User(const QString &account, const QString &password, QObject *parent)
apiRequest->setBaseUrl("https://syncapi.snakekiss.com");
}

void User::enroll()
bool User::enroll()
{
QString postData = QString("username=%1&email=%2&password=%3").arg(username).arg(account).arg(hashedPassword);
apiRequest->post("/register",postData.toUtf8());
QString postData
= QString("username=%1&email=%2&password=%3").arg(username).arg(account).arg(hashedPassword);
ApiResponse response=apiRequest->post("/register",postData.toUtf8());
return response.isSuccess();
}

void User::login()
bool User::login()
{
QString postData = QString("email=%1&password=%2").arg(account).arg(hashedPassword);
apiRequest->post("/login",postData.toUtf8());
ApiResponse response=apiRequest->post("/login",postData.toUtf8());
return response.isSuccess();
}

void User::forgetPassword()
bool User::forgetPassword()
{
//找回密码功能
return true;
}

QString User::getUserHash() const
Expand Down
6 changes: 3 additions & 3 deletions user.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ class User : public QObject
User(const QString &account,
const QString &password,
QObject *parent = nullptr);
void enroll(); //执行post请求,实现注册功能
void login(); //执行post请求,实现登录功能
void forgetPassword(); //执行post请求,实现找回密码功能
bool enroll(); //执行post请求,实现注册功能
bool login(); //执行post请求,实现登录功能
bool forgetPassword(); //执行post请求,实现找回密码功能

QString getUserHash() const; //返回用户账户的哈希

Expand Down

0 comments on commit cc1d441

Please sign in to comment.