diff --git a/apirequest.cpp b/apirequest.cpp index 3dde8e3..198064c 100644 --- a/apirequest.cpp +++ b/apirequest.cpp @@ -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; } diff --git a/apirequest.h b/apirequest.h index 0cb5a1e..fd7ead7 100644 --- a/apirequest.h +++ b/apirequest.h @@ -13,7 +13,6 @@ class ApiRequest : public QObject public: explicit ApiRequest(QObject *parent = nullptr){ manager = new QNetworkAccessManager(this); - response = new ApiResponse(this); }; // 设置 API 主域名 @@ -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 diff --git a/apiresponse.cpp b/apiresponse.cpp index 838a2e0..0985984 100644 --- a/apiresponse.cpp +++ b/apiresponse.cpp @@ -1,7 +1,7 @@ #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(); @@ -9,7 +9,13 @@ ApiResponse* ApiResponse::processJson(const QByteArray &responseData) } 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) diff --git a/apiresponse.h b/apiresponse.h index a7e29dd..e7a4b09 100644 --- a/apiresponse.h +++ b/apiresponse.h @@ -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; } @@ -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; diff --git a/loginwindow.cpp b/loginwindow.cpp index 11684a9..63401cd 100644 --- a/loginwindow.cpp +++ b/loginwindow.cpp @@ -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(); } } diff --git a/main.cpp b/main.cpp index e36b767..93cb7ae 100644 --- a/main.cpp +++ b/main.cpp @@ -7,6 +7,7 @@ int main(int argc, char *argv[]) { QApplication a(argc, argv); + //测试用户 User loginuser("newuser@example.com","123456"); loginuser.login(); diff --git a/mainwindow.cpp b/mainwindow.cpp index c3f0910..2cba63e 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -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()); diff --git a/signin.cpp b/signin.cpp index 54fcda7..5a16cf8 100644 --- a/signin.cpp +++ b/signin.cpp @@ -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(); } } diff --git a/signin.h b/signin.h index 88e3574..e8ee916 100644 --- a/signin.h +++ b/signin.h @@ -2,6 +2,7 @@ #define SIGNIN_H #include +#include "user.h" namespace Ui { class signin; diff --git a/user.cpp b/user.cpp index 4c41f34..090c4a7 100644 --- a/user.cpp +++ b/user.cpp @@ -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 diff --git a/user.h b/user.h index ff8cf97..1408cc8 100644 --- a/user.h +++ b/user.h @@ -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; //返回用户账户的哈希