重构了连接数据库、设置设备名的代码,封装为类
This commit is contained in:
parent
1a300f4ca7
commit
6cca0535bc
@ -14,6 +14,8 @@ SOURCES += \
|
|||||||
|
|
||||||
HEADERS += \
|
HEADERS += \
|
||||||
HF15693.h \
|
HF15693.h \
|
||||||
|
databaseAPI.h \
|
||||||
|
deviceAPI.h \
|
||||||
mainwindow.h \
|
mainwindow.h \
|
||||||
newCardPage.h \
|
newCardPage.h \
|
||||||
quitAppPage.h \
|
quitAppPage.h \
|
||||||
|
83
databaseAPI.h
Normal file
83
databaseAPI.h
Normal file
@ -0,0 +1,83 @@
|
|||||||
|
#ifndef DATABASEAPI_H
|
||||||
|
#define DATABASEAPI_H
|
||||||
|
|
||||||
|
|
||||||
|
#include <QSqlDatabase>
|
||||||
|
#include <QString>
|
||||||
|
|
||||||
|
|
||||||
|
class Database
|
||||||
|
{
|
||||||
|
private:
|
||||||
|
QSqlDatabase db;
|
||||||
|
bool connected = false;
|
||||||
|
QString databaseName = QString("cardManageSystem");
|
||||||
|
QString userName = QString("cardManageSystem");
|
||||||
|
|
||||||
|
public:
|
||||||
|
Database(QSqlDatabase database)
|
||||||
|
{
|
||||||
|
db = database;
|
||||||
|
db.setDatabaseName(databaseName);
|
||||||
|
db.setUserName(userName);
|
||||||
|
}
|
||||||
|
|
||||||
|
Database(QSqlDatabase database, QString hostName, int port, QString password)
|
||||||
|
{
|
||||||
|
db = database;
|
||||||
|
db.setDatabaseName(databaseName);
|
||||||
|
db.setUserName(userName);
|
||||||
|
db.setHostName(hostName);
|
||||||
|
db.setPort(port);
|
||||||
|
db.setPassword(password);
|
||||||
|
}
|
||||||
|
|
||||||
|
QSqlDatabase getDatabase()
|
||||||
|
{
|
||||||
|
return db;
|
||||||
|
}
|
||||||
|
|
||||||
|
void setHostName(QString hostName)
|
||||||
|
{
|
||||||
|
db.setHostName(hostName);
|
||||||
|
}
|
||||||
|
|
||||||
|
QString getHostName()
|
||||||
|
{
|
||||||
|
return db.hostName();
|
||||||
|
}
|
||||||
|
|
||||||
|
void setPort(int port)
|
||||||
|
{
|
||||||
|
db.setPort(port);
|
||||||
|
}
|
||||||
|
|
||||||
|
int getPort()
|
||||||
|
{
|
||||||
|
return db.port();
|
||||||
|
}
|
||||||
|
|
||||||
|
void setPassword(QString password)
|
||||||
|
{
|
||||||
|
db.setPassword(password);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool is_connected()
|
||||||
|
{
|
||||||
|
return connected;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool open()
|
||||||
|
{
|
||||||
|
connected = db.open();
|
||||||
|
return connected;
|
||||||
|
}
|
||||||
|
|
||||||
|
~Database()
|
||||||
|
{
|
||||||
|
db.close();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
#endif // DATABASEAPI_H
|
51
deviceAPI.h
Normal file
51
deviceAPI.h
Normal file
@ -0,0 +1,51 @@
|
|||||||
|
#ifndef DEVICEAPI_H
|
||||||
|
#define DEVICEAPI_H
|
||||||
|
|
||||||
|
#include <QString>
|
||||||
|
#include <QSqlQuery>
|
||||||
|
#include <databaseAPI.h>
|
||||||
|
|
||||||
|
class Device
|
||||||
|
{
|
||||||
|
private:
|
||||||
|
bool connected = false;
|
||||||
|
bool depositAllowed = false;
|
||||||
|
QString name = QString("未指定设备名");
|
||||||
|
|
||||||
|
public:
|
||||||
|
bool is_connected()
|
||||||
|
{
|
||||||
|
return connected;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool is_depositAllowed()
|
||||||
|
{
|
||||||
|
return depositAllowed;
|
||||||
|
}
|
||||||
|
|
||||||
|
void setDevice(QString name, Database *db)
|
||||||
|
{
|
||||||
|
QSqlQuery query(db->getDatabase());
|
||||||
|
QString sql = QString("select * from device where `name` = '%1';").arg(name);
|
||||||
|
query.exec(sql);
|
||||||
|
if (query.next())
|
||||||
|
{
|
||||||
|
connected = true;
|
||||||
|
this->name = name;
|
||||||
|
depositAllowed = query.value(2).toBool();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
connected = false;
|
||||||
|
depositAllowed = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
QString getName()
|
||||||
|
{
|
||||||
|
if (connected) return name;
|
||||||
|
else return QString("未指定设备名");
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // DEVICEAPI_H
|
@ -33,7 +33,7 @@ MainWindow::MainWindow(QWidget *parent)
|
|||||||
databaseLabel = new QLabel("数据库无连接");
|
databaseLabel = new QLabel("数据库无连接");
|
||||||
ui->statusBar->addWidget(databaseLabel);
|
ui->statusBar->addWidget(databaseLabel);
|
||||||
|
|
||||||
deviceLabel = new QLabel(device);
|
deviceLabel = new QLabel(device.getName());
|
||||||
ui->statusBar->addWidget(deviceLabel);
|
ui->statusBar->addWidget(deviceLabel);
|
||||||
|
|
||||||
ui->stackedWidget->setCurrentWidget(ui->settingPage);
|
ui->stackedWidget->setCurrentWidget(ui->settingPage);
|
||||||
|
11
mainwindow.h
11
mainwindow.h
@ -6,10 +6,11 @@
|
|||||||
#include <QLabel>
|
#include <QLabel>
|
||||||
#include <QStackedWidget>
|
#include <QStackedWidget>
|
||||||
#include <QMessageBox>
|
#include <QMessageBox>
|
||||||
#include <QSqlDatabase>
|
|
||||||
#include <QSqlQuery>
|
#include <QSqlQuery>
|
||||||
|
|
||||||
#include <readerAPI.h>
|
#include <readerAPI.h>
|
||||||
|
#include <databaseAPI.h>
|
||||||
|
#include <deviceAPI.h>
|
||||||
|
|
||||||
QT_BEGIN_NAMESPACE
|
QT_BEGIN_NAMESPACE
|
||||||
namespace Ui {
|
namespace Ui {
|
||||||
@ -43,12 +44,8 @@ private slots:
|
|||||||
private:
|
private:
|
||||||
Ui::MainWindow *ui;
|
Ui::MainWindow *ui;
|
||||||
Reader reader;
|
Reader reader;
|
||||||
QSqlDatabase db;
|
Database *db = nullptr;
|
||||||
QString databaseIpAddr = "";
|
Device device;
|
||||||
int databasePort = 3306;
|
|
||||||
QString databasePassword = "";
|
|
||||||
QString device = QString("未指定设备名");
|
|
||||||
bool depositAllowed = false;
|
|
||||||
|
|
||||||
QStatusBar *statusBar;
|
QStatusBar *statusBar;
|
||||||
QStackedWidget *stackedWidget;
|
QStackedWidget *stackedWidget;
|
||||||
|
@ -63,43 +63,38 @@ void MainWindow::on_connectReaderButton_clicked()
|
|||||||
*/
|
*/
|
||||||
void MainWindow::on_connectDatabaseButton_clicked()
|
void MainWindow::on_connectDatabaseButton_clicked()
|
||||||
{
|
{
|
||||||
databaseIpAddr = ui->ipAddrEdit->text();
|
if (db == nullptr)
|
||||||
databasePort = ui->portBox->value();
|
{
|
||||||
databasePassword = ui->passwordEdit->text();
|
db = new Database(QSqlDatabase::addDatabase("QMYSQL"), ui->ipAddrEdit->text(), ui->portBox->value(), ui->passwordEdit->text());
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
db->setHostName(ui->ipAddrEdit->text());
|
||||||
|
db->setPort(ui->portBox->value());
|
||||||
|
db->setPassword(ui->passwordEdit->text());
|
||||||
|
}
|
||||||
|
|
||||||
db = QSqlDatabase::addDatabase("QMYSQL");
|
if (!db->open())
|
||||||
db.setHostName(databaseIpAddr);
|
|
||||||
db.setPort(databasePort);
|
|
||||||
db.setPassword(databasePassword);
|
|
||||||
db.setDatabaseName("cardManageSystem");
|
|
||||||
db.setUserName("cardManageSystem");
|
|
||||||
|
|
||||||
if (!db.open())
|
|
||||||
{
|
{
|
||||||
QMessageBox::warning(this, QString("数据库状态提示"), QString("数据库连接失败,请重试。"));
|
QMessageBox::warning(this, QString("数据库状态提示"), QString("数据库连接失败,请重试。"));
|
||||||
databaseConnectStatusCheckBox->setChecked(false);
|
databaseConnectStatusCheckBox->setChecked(false);
|
||||||
databaseLabel->setText(QString("数据库未连接"));
|
databaseLabel->setText(QString("数据库未连接"));
|
||||||
databaseIpAddr = "";
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
databaseConnectStatusCheckBox->setChecked(true);
|
databaseConnectStatusCheckBox->setChecked(true);
|
||||||
databaseLabel->setText(QString("数据库已连接:") + databaseIpAddr + QString(":") + QString::number(databasePort));
|
databaseLabel->setText(QString("数据库已连接:") + db->getHostName() + QString(":") + QString::number(db->getPort()));
|
||||||
|
|
||||||
device = ui->deviceEdit->text();
|
device.setDevice(ui->deviceEdit->text(), db);
|
||||||
QSqlQuery query;
|
if (!device.is_connected())
|
||||||
QString sql = QString("select * from device where id = '%1';").arg(device);
|
|
||||||
query.exec(sql);
|
|
||||||
if (query.next())
|
|
||||||
{
|
{
|
||||||
if (query.value(2).toBool()) depositAllowed = true;
|
QMessageBox::warning(this, QString("设备名提示"), QString("该设备名无效,请重试。"));
|
||||||
else depositAllowed = false;
|
deviceLabel->setText(device.getName());
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
device = QString("未指定设备名");
|
if (device.is_depositAllowed()) deviceLabel->setText(device.getName() + QString("(可充值)"));
|
||||||
QMessageBox::warning(this, QString("设备名提示"), QString("该设备名无效,请重试。"));
|
else deviceLabel->setText(device.getName() + QString("(仅可消费)"));
|
||||||
}
|
}
|
||||||
deviceLabel->setText(device);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -110,7 +105,9 @@ void MainWindow::on_connectDatabaseButton_clicked()
|
|||||||
*/
|
*/
|
||||||
bool MainWindow::ready()
|
bool MainWindow::ready()
|
||||||
{
|
{
|
||||||
if (!reader.is_connected() || databaseIpAddr.isEmpty()) return false;
|
if (!reader.is_connected()) return false;
|
||||||
|
if (db == nullptr || !db->is_connected()) return false;
|
||||||
|
if (!device.is_connected()) return false;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user