Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander-Porter committed Jul 4, 2024
2 parents 09b9dd7 + 33d152f commit 59973df
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 14 deletions.
8 changes: 7 additions & 1 deletion main.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
#include "mainwindow.h"

#include <QApplication>



int main(int argc, char *argv[])
{
QApplication a(argc, argv);

//测试用户
User testuser("newuser@example.com","123456");
testuser.login();

MainWindow w;
w.show();
return a.exec();
Expand Down
1 change: 0 additions & 1 deletion user.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
#include "user.h"

User::User() {}
107 changes: 95 additions & 12 deletions user.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,110 @@
#define USER_H
#include <QString>
#include <QCryptographicHash>
#include <QJsonDocument>
#include <QJsonObject>
#include <QNetworkAccessManager>
#include <QNetworkCookie>
#include <QNetworkCookieJar>
#include <QNetworkReply>
#include <QObject>
#include <QString>
#include <QDebug>

class User : public QObject {
Q_OBJECT

class User
{
private:
QString account;
QString token="222";
QString data;
QString hashedPassword;
QNetworkAccessManager* manager;
QNetworkReply* reply;
QString session;

public:
User();
User(QString account, QString password) {
this->account = account;
this->hashedPassword = password;
explicit User(QObject* parent = nullptr) : QObject(parent), manager(new QNetworkAccessManager(this)) {
connect(manager, &QNetworkAccessManager::finished, this, &User::loginFinished);
}
QString login(QString password) {
//https post a requests
return token;

User(const QString& account, const QString& password, QObject* parent = nullptr) : QObject(parent), account(account), hashedPassword(password), manager(new QNetworkAccessManager(this)) {
connect(manager, &QNetworkAccessManager::finished, this, &User::loginFinished);
}

void login() {
QUrl url("https://syncapi.snakekiss.com/login");
QNetworkRequest request(url);

request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
request.setHeader(QNetworkRequest::UserAgentHeader, "MyApp/1.0");

QString postData = QString("email=%1&password=%2").arg(account, hashedPassword);
reply = manager->post(request, postData.toUtf8());
}
QString getUserHash() {

QString getUserHash() const {
QByteArray hash = QCryptographicHash::hash(account.toUtf8(), QCryptographicHash::Sha1);
return hash.toHex(); // 将散列值转换为十六进制字符串并返回
return hash.toHex();
}

QString getSession(){
return session;
}

signals:
void loginCompleted(const QString& session);//登陆成功的信号,发送获取到的cookie

private slots:
void loginFinished() {
if (reply->error() == QNetworkReply::NoError) {
QByteArray responseData = reply->readAll();
QJsonDocument jsonDoc = QJsonDocument::fromJson(responseData);

if (jsonDoc.isObject()) {
QJsonObject jsonObject = jsonDoc.object();
processJsonObject(jsonObject);
} else {
qDebug() << "Invalid JSON format";
}
} else {
qDebug() << "Error:" << reply->errorString();
}

processCookies();
reply->deleteLater();
}

private:
void processJsonObject(const QJsonObject& jsonObject) {
if (jsonObject.contains("code")) {
qDebug() << "Value for code:" << jsonObject.value("code").toString();
}
if (jsonObject.contains("message")) {
qDebug() << "Value for message:" << jsonObject.value("message").toString();
}
if (jsonObject.contains("data")) {
qDebug() << "Value for data:" << jsonObject.value("data").toString();
}
}

void processCookies() {
if (manager) {
QNetworkCookieJar* cookieJar = manager->cookieJar();
QList<QNetworkCookie> cookies = cookieJar->cookiesForUrl(reply->url());
if (!cookies.isEmpty()) {
qDebug() << "Received cookies:";
for (const QNetworkCookie& cookie : cookies) {
qDebug() << "Cookie Name:" << cookie.name();
qDebug() << "Cookie Value:" << cookie.value();
session = cookie.value();
emit loginCompleted(session);
}
} else {
qDebug() << "No cookies received.";
}
} else {
qDebug() << "Failed to get QNetworkAccessManager.";
}
}
};

Expand Down

0 comments on commit 59973df

Please sign in to comment.