You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
163 lines
4.7 KiB
163 lines
4.7 KiB
#include "ruoyidatamanager.h"
|
|
#include <QNetworkAccessManager>
|
|
#include <QNetworkRequest>
|
|
#include <QUrlQuery>
|
|
#include <QNetworkReply>
|
|
#include <QJsonDocument>
|
|
#include <QJsonParseError>
|
|
#include <QJsonObject>
|
|
#include <QEventLoop>
|
|
#include <QTimer>
|
|
|
|
RuoyiDataManager::RuoyiDataManager()
|
|
{
|
|
m_pNetworkManager = new QNetworkAccessManager;
|
|
//服务器地址
|
|
m_sUrl = "http://top.365rise.top";
|
|
}
|
|
|
|
RuoyiDataManager::~RuoyiDataManager()
|
|
{
|
|
if(m_pNetworkManager)
|
|
{
|
|
m_pNetworkManager->deleteLater();
|
|
m_pNetworkManager = nullptr;
|
|
}
|
|
}
|
|
|
|
QPixmap RuoyiDataManager::GetVerificationCode()
|
|
{
|
|
QNetworkRequest request;
|
|
QString urlStr(m_sUrl + "/prod-api/captchaImage");
|
|
QUrl url = QUrl(urlStr);
|
|
request.setUrl(url);
|
|
QNetworkReply *reply = m_pNetworkManager->get(request);
|
|
reply->setProperty("msgType",QVariant(int(GETVERIFICATIONCODE)));
|
|
connect(reply,&QNetworkReply::finished,this,&RuoyiDataManager::RequestFinished);
|
|
|
|
QEventLoop loop;
|
|
connect(this, SIGNAL(ParseReplyFinished()), &loop, SLOT(quit()));
|
|
loop.exec();
|
|
|
|
return m_userInfo.codePix;
|
|
}
|
|
|
|
UserInfo RuoyiDataManager::Login(const QString &userName, const QString &password, const QString &code)
|
|
{
|
|
m_userInfo.userName = userName;
|
|
|
|
QNetworkRequest request;
|
|
QString urlStr(m_sUrl + "/prod-api/login");
|
|
|
|
QJsonDocument doc;
|
|
QJsonObject rootObject;
|
|
rootObject.insert("username",userName);
|
|
rootObject.insert("password",password);
|
|
rootObject.insert("code",code);
|
|
rootObject.insert("uuid",m_userInfo.uuid);
|
|
doc.setObject(rootObject);
|
|
qDebug() << __FUNCTION__ << " login json: " << doc.toJson();
|
|
|
|
QUrl url = QUrl(urlStr);
|
|
request.setUrl(url);
|
|
request.setHeader(QNetworkRequest::ContentTypeHeader,"application/json;charset=UTF-8");
|
|
QNetworkReply *reply = m_pNetworkManager->post(request,doc.toJson());
|
|
reply->setProperty("msgType",QVariant(int(LOGIN)));
|
|
connect(reply,&QNetworkReply::finished,this,&RuoyiDataManager::RequestFinished);
|
|
|
|
QEventLoop loop;
|
|
connect(this, SIGNAL(ParseReplyFinished()), &loop, SLOT(quit()));
|
|
loop.exec();
|
|
|
|
return m_userInfo;
|
|
}
|
|
|
|
void RuoyiDataManager::RequestFinished()
|
|
{
|
|
QNetworkReply *reply = dynamic_cast< QNetworkReply* >(sender());
|
|
MsgType type =(MsgType) reply->property("msgType").toInt();
|
|
if(type == GETVERIFICATIONCODE)
|
|
{
|
|
ParseVerificationCode(reply);
|
|
}
|
|
else if(type == LOGIN)
|
|
{
|
|
ParseLogin(reply);
|
|
}
|
|
emit ParseReplyFinished();
|
|
}
|
|
|
|
void RuoyiDataManager::ParseVerificationCode(QNetworkReply *reply)
|
|
{
|
|
QByteArray readAllArray;
|
|
QVariant statusCode = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute);
|
|
if(statusCode.isValid())
|
|
{
|
|
qDebug() << "status code=" << statusCode.toInt();
|
|
}
|
|
|
|
QNetworkReply::NetworkError err = reply->error();
|
|
if(err != QNetworkReply::NoError)
|
|
{
|
|
qDebug() << "Failed: " << reply->errorString();
|
|
}
|
|
else
|
|
{
|
|
readAllArray = reply->readAll();
|
|
QJsonParseError jsonError;
|
|
QJsonDocument document = QJsonDocument::fromJson(readAllArray,&jsonError);
|
|
if(jsonError.error == QJsonParseError::NoError && !document.isNull())
|
|
{
|
|
QJsonObject root = document.object();
|
|
if(root.contains("img"))
|
|
{
|
|
QPixmap pix;
|
|
bool ret = pix.loadFromData(QByteArray::fromBase64(root["img"].toString().toUtf8()));
|
|
m_userInfo.codePix = pix;
|
|
}
|
|
m_userInfo.uuid = root["uuid"].toString();
|
|
}
|
|
else
|
|
{
|
|
qDebug() << __FUNCTION__ << __LINE__ << jsonError.errorString();
|
|
}
|
|
}
|
|
}
|
|
|
|
void RuoyiDataManager::ParseLogin(QNetworkReply *reply)
|
|
{
|
|
QByteArray readAllArray;
|
|
QVariant statusCode = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute);
|
|
if(statusCode.isValid())
|
|
{
|
|
qDebug() << "status code=" << statusCode.toInt();
|
|
}
|
|
|
|
QNetworkReply::NetworkError err = reply->error();
|
|
if(err != QNetworkReply::NoError)
|
|
{
|
|
qDebug() << "Failed: " << reply->errorString();
|
|
m_userInfo.token = "";
|
|
}
|
|
else
|
|
{
|
|
readAllArray = reply->readAll();
|
|
QJsonParseError jsonError;
|
|
QJsonDocument document = QJsonDocument::fromJson(readAllArray,&jsonError);
|
|
if(jsonError.error == QJsonParseError::NoError && !document.isNull())
|
|
{
|
|
QJsonObject root = document.object();
|
|
if(root.contains("token") && root["code"].toInt() == 200)
|
|
{
|
|
m_userInfo.token = root["token"].toString();
|
|
}
|
|
}
|
|
else
|
|
{
|
|
qDebug() << __FUNCTION__ << __LINE__ << jsonError.errorString();
|
|
m_userInfo.token = "";
|
|
}
|
|
}
|
|
}
|
|
|