parent
e125abae5c
commit
30f37d1b0b
@ -0,0 +1,26 @@
|
||||
#ifndef BASEDATAMANAGER_H
|
||||
#define BASEDATAMANAGER_H
|
||||
|
||||
#include <QObject>
|
||||
#include <QPixmap>
|
||||
#include "structs.h"
|
||||
|
||||
class BaseDataManager : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
BaseDataManager();
|
||||
virtual ~BaseDataManager();
|
||||
|
||||
virtual QPixmap GetVerificationCode();
|
||||
virtual UserInfo Login(const QString& userName,const QString& password,const QString& code);
|
||||
|
||||
signals:
|
||||
//请求结束 ret 返回值 0成功 非0失败
|
||||
void Finished(int ret);
|
||||
|
||||
protected:
|
||||
UserInfo m_userInfo;
|
||||
};
|
||||
|
||||
#endif // BASEDATAMANAGER_H
|
||||
@ -0,0 +1,160 @@
|
||||
#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();
|
||||
}
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -0,0 +1,38 @@
|
||||
#ifndef RUOYIDATAMANAGER_H
|
||||
#define RUOYIDATAMANAGER_H
|
||||
#include "basedatamanager.h"
|
||||
|
||||
class QNetworkAccessManager;
|
||||
class QNetworkReply;
|
||||
class RuoyiDataManager : public BaseDataManager
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
enum MsgType
|
||||
{
|
||||
GETVERIFICATIONCODE = 0,
|
||||
LOGIN
|
||||
};
|
||||
|
||||
public:
|
||||
RuoyiDataManager();
|
||||
~RuoyiDataManager();
|
||||
|
||||
QPixmap GetVerificationCode() override;
|
||||
UserInfo Login(const QString& userName,const QString& password,const QString& code) override;
|
||||
|
||||
public slots:
|
||||
void RequestFinished();
|
||||
|
||||
signals:
|
||||
void ParseReplyFinished();
|
||||
private:
|
||||
void ParseVerificationCode(QNetworkReply* reply);
|
||||
void ParseLogin(QNetworkReply* reply);
|
||||
|
||||
private:
|
||||
QNetworkAccessManager* m_pNetworkManager;
|
||||
QString m_sUrl;
|
||||
};
|
||||
|
||||
#endif // RUOYIDATAMANAGER_H
|
||||
@ -0,0 +1,22 @@
|
||||
#ifndef STRUCTS_H
|
||||
#define STRUCTS_H
|
||||
#include <QString>
|
||||
#include <QPixmap>
|
||||
|
||||
enum ManagerType
|
||||
{
|
||||
None = 0,
|
||||
Ruoyi,
|
||||
MySql
|
||||
};
|
||||
|
||||
//用户基本信心
|
||||
typedef struct tagUserInfo
|
||||
{
|
||||
QString userName; // 用户名
|
||||
QString uuid;// 验证码uuid
|
||||
QString token;// 登录token
|
||||
QPixmap codePix;
|
||||
}UserInfo;
|
||||
|
||||
#endif // STRUCTS_H
|
||||
@ -0,0 +1,44 @@
|
||||
#include "userdata.h"
|
||||
#include "ruoyi/ruoyidatamanager.h"
|
||||
|
||||
UserData::UserData()
|
||||
{
|
||||
m_pDataManager = nullptr;
|
||||
}
|
||||
|
||||
UserData::~UserData()
|
||||
{
|
||||
if(m_pDataManager)
|
||||
{
|
||||
delete m_pDataManager;
|
||||
m_pDataManager = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
void UserData::SetManagerType(ManagerType type)
|
||||
{
|
||||
if(type == None)
|
||||
{
|
||||
m_pDataManager = new BaseDataManager;
|
||||
}
|
||||
else if(type == Ruoyi)
|
||||
{
|
||||
m_pDataManager = new RuoyiDataManager;
|
||||
}
|
||||
}
|
||||
|
||||
QPixmap UserData::GetVerificationCode()
|
||||
{
|
||||
if(m_pDataManager == nullptr)
|
||||
return QPixmap();
|
||||
else
|
||||
return m_pDataManager->GetVerificationCode();
|
||||
}
|
||||
|
||||
UserInfo UserData::Login(const QString &userName, const QString &password, const QString &code)
|
||||
{
|
||||
if(m_pDataManager == nullptr)
|
||||
return UserInfo();
|
||||
else
|
||||
return m_pDataManager->Login(userName,password,code);
|
||||
}
|
||||
@ -0,0 +1,25 @@
|
||||
#ifndef USERDATA_H
|
||||
#define USERDATA_H
|
||||
#include <QObject>
|
||||
#include <QString>
|
||||
#include <QPixmap>
|
||||
#include "basedatamanager.h"
|
||||
#include "structs.h"
|
||||
|
||||
class UserData : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
UserData();
|
||||
~UserData();
|
||||
void SetManagerType(ManagerType type);
|
||||
//获取验证码图片;可选
|
||||
QPixmap GetVerificationCode();
|
||||
//登录 获取用户信息,同步等待
|
||||
UserInfo Login(const QString& userName,const QString& password,const QString& code);
|
||||
|
||||
private:
|
||||
BaseDataManager* m_pDataManager;
|
||||
};
|
||||
|
||||
#endif // USERDATA_H
|
||||
@ -0,0 +1,41 @@
|
||||
#include "logindialog.h"
|
||||
#include "ui_logindialog.h"
|
||||
#include <QNetworkAccessManager>
|
||||
#include <QNetworkRequest>
|
||||
#include <QUrlQuery>
|
||||
#include <QNetworkReply>
|
||||
#include <QJsonDocument>
|
||||
#include <QJsonParseError>
|
||||
#include <QJsonObject>
|
||||
|
||||
LoginDialog::LoginDialog(QWidget *parent) :
|
||||
QDialog(parent),
|
||||
ui(new Ui::LoginDialog)
|
||||
{
|
||||
ui->setupUi(this);
|
||||
m_UserData.SetManagerType(ManagerType::Ruoyi);
|
||||
GetVerificationCode();
|
||||
}
|
||||
|
||||
LoginDialog::~LoginDialog()
|
||||
{
|
||||
delete ui;
|
||||
}
|
||||
|
||||
void LoginDialog::on_pushButton_cancle_clicked()
|
||||
{
|
||||
this->reject();
|
||||
}
|
||||
|
||||
void LoginDialog::GetVerificationCode()
|
||||
{
|
||||
QPixmap pix = m_UserData.GetVerificationCode();
|
||||
ui->label_code->setPixmap(pix.scaledToHeight(34));
|
||||
}
|
||||
|
||||
void LoginDialog::on_pushButton_login_clicked()
|
||||
{
|
||||
m_UserInfo = m_UserData.Login(ui->username->text(),ui->password->text(),ui->code->text());
|
||||
qDebug() << __FUNCTION__ << " login finished: "<< m_UserInfo.userName << m_UserInfo.token << m_UserInfo.uuid;
|
||||
}
|
||||
|
||||
@ -0,0 +1,35 @@
|
||||
#ifndef LOGINDIALOG_H
|
||||
#define LOGINDIALOG_H
|
||||
|
||||
#include <QDialog>
|
||||
#include "userdata.h"
|
||||
#include "structs.h"
|
||||
|
||||
namespace Ui {
|
||||
class LoginDialog;
|
||||
}
|
||||
|
||||
class QNetworkReply;
|
||||
class LoginDialog : public QDialog
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit LoginDialog(QWidget *parent = nullptr);
|
||||
~LoginDialog();
|
||||
|
||||
private:
|
||||
//加载验证码
|
||||
void GetVerificationCode();
|
||||
|
||||
private slots:
|
||||
void on_pushButton_cancle_clicked();
|
||||
void on_pushButton_login_clicked();
|
||||
|
||||
private:
|
||||
Ui::LoginDialog *ui;
|
||||
UserData m_UserData;
|
||||
UserInfo m_UserInfo;
|
||||
};
|
||||
|
||||
#endif // LOGINDIALOG_H
|
||||
@ -0,0 +1,133 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<ui version="4.0">
|
||||
<class>LoginDialog</class>
|
||||
<widget class="QDialog" name="LoginDialog">
|
||||
<property name="geometry">
|
||||
<rect>
|
||||
<x>0</x>
|
||||
<y>0</y>
|
||||
<width>707</width>
|
||||
<height>459</height>
|
||||
</rect>
|
||||
</property>
|
||||
<property name="windowTitle">
|
||||
<string>Dialog</string>
|
||||
</property>
|
||||
<widget class="QWidget" name="layoutWidget">
|
||||
<property name="geometry">
|
||||
<rect>
|
||||
<x>150</x>
|
||||
<y>150</y>
|
||||
<width>440</width>
|
||||
<height>126</height>
|
||||
</rect>
|
||||
</property>
|
||||
<layout class="QGridLayout" name="gridLayout">
|
||||
<item row="2" column="0" colspan="2">
|
||||
<widget class="QLabel" name="label_3">
|
||||
<property name="text">
|
||||
<string>Verification code</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="2" column="2">
|
||||
<widget class="QLineEdit" name="code">
|
||||
<property name="minimumSize">
|
||||
<size>
|
||||
<width>0</width>
|
||||
<height>36</height>
|
||||
</size>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="2" column="3">
|
||||
<widget class="QLabel" name="label_code">
|
||||
<property name="minimumSize">
|
||||
<size>
|
||||
<width>96</width>
|
||||
<height>36</height>
|
||||
</size>
|
||||
</property>
|
||||
<property name="maximumSize">
|
||||
<size>
|
||||
<width>96</width>
|
||||
<height>36</height>
|
||||
</size>
|
||||
</property>
|
||||
<property name="text">
|
||||
<string/>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="1" column="0" colspan="2">
|
||||
<widget class="QLabel" name="label_2">
|
||||
<property name="text">
|
||||
<string>Password</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="0" column="0" colspan="2">
|
||||
<widget class="QLabel" name="label">
|
||||
<property name="text">
|
||||
<string>UserName</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="0" column="2">
|
||||
<widget class="QLineEdit" name="username">
|
||||
<property name="minimumSize">
|
||||
<size>
|
||||
<width>128</width>
|
||||
<height>36</height>
|
||||
</size>
|
||||
</property>
|
||||
<property name="text">
|
||||
<string>admin</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="1" column="2">
|
||||
<widget class="QLineEdit" name="password">
|
||||
<property name="minimumSize">
|
||||
<size>
|
||||
<width>0</width>
|
||||
<height>36</height>
|
||||
</size>
|
||||
</property>
|
||||
<property name="text">
|
||||
<string>1qazse42W3</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
</layout>
|
||||
</widget>
|
||||
<widget class="QWidget" name="layoutWidget">
|
||||
<property name="geometry">
|
||||
<rect>
|
||||
<x>270</x>
|
||||
<y>310</y>
|
||||
<width>169</width>
|
||||
<height>26</height>
|
||||
</rect>
|
||||
</property>
|
||||
<layout class="QHBoxLayout" name="horizontalLayout">
|
||||
<item>
|
||||
<widget class="QPushButton" name="pushButton_login">
|
||||
<property name="text">
|
||||
<string>Login</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QPushButton" name="pushButton_cancle">
|
||||
<property name="text">
|
||||
<string>Cancel</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
</layout>
|
||||
</widget>
|
||||
</widget>
|
||||
<resources/>
|
||||
<connections/>
|
||||
</ui>
|
||||
Loading…
Reference in new issue