mirror of
https://gitee.com/js-yhsec/energy_storage.git
synced 2026-05-27 18:59:26 +08:00
完成系统管理web端功能,实现系统管理服务端接口,实现登录功能
This commit is contained in:
@@ -80,27 +80,12 @@ void Menu::onMenuBtnClicked()
|
||||
|
||||
MainWindow::MainWindow()
|
||||
{
|
||||
webView = std::make_shared<QWebEngineView>(this);
|
||||
|
||||
MyWebHandler* myWebHandler = new MyWebHandler();
|
||||
QWebChannel* webChannel = new QWebChannel();
|
||||
webChannel->registerObject("cppNative", myWebHandler);
|
||||
webView->page()->setWebChannel(webChannel);
|
||||
|
||||
webView->setGeometry(0, 0, 1920, 1080);
|
||||
// 默认设置透明, 解决加载时的白屏闪烁
|
||||
webView->page()->setBackgroundColor(Qt::transparent);
|
||||
|
||||
webView->setContextMenuPolicy(Qt::NoContextMenu);
|
||||
//webView.load(QUrl("https://www.baidu.com"));
|
||||
webView->load(QUrl("file:///assets/html/main.html"));
|
||||
//connect(wWebView.get(), &QWebEngineView::loadFinished, this, &MyWidget::slotLoadFinished);
|
||||
|
||||
//std::string htmlContent = "HelloWorld";
|
||||
//webView->setHtml(htmlContent.c_str(), QUrl("file:///assets/html/"));
|
||||
webView->show();
|
||||
return;
|
||||
|
||||
if (1)
|
||||
{
|
||||
this->initWebView();
|
||||
return;
|
||||
}
|
||||
|
||||
QUI::label(labBkg_, this, 0, 0, 1920, 1080, "");
|
||||
labBkg_.setPixmap(QPixmap("assets/ui/bkg01.png"));
|
||||
|
||||
@@ -130,6 +115,29 @@ MainWindow::MainWindow()
|
||||
timer_.start(1000);
|
||||
}
|
||||
|
||||
void MainWindow::initWebView()
|
||||
{
|
||||
webView_ = std::make_shared<QWebEngineView>(this);
|
||||
|
||||
MyWebHandler* myWebHandler = new MyWebHandler();
|
||||
QWebChannel* webChannel = new QWebChannel();
|
||||
webChannel->registerObject("cppNative", myWebHandler);
|
||||
webView_->page()->setWebChannel(webChannel);
|
||||
|
||||
webView_->setGeometry(0, 0, 1920, 1080);
|
||||
// 默认设置透明, 解决加载时的白屏闪烁
|
||||
webView_->page()->setBackgroundColor(Qt::transparent);
|
||||
|
||||
webView_->setContextMenuPolicy(Qt::NoContextMenu);
|
||||
//webView.load(QUrl("https://www.baidu.com"));
|
||||
webView_->load(QUrl("file:///assets/html/main.html"));
|
||||
//connect(wWebView.get(), &QWebEngineView::loadFinished, this, &MyWidget::slotLoadFinished);
|
||||
|
||||
//std::string htmlContent = "HelloWorld";
|
||||
//webView->setHtml(htmlContent.c_str(), QUrl("file:///assets/html/"));
|
||||
webView_->show();
|
||||
}
|
||||
|
||||
void MainWindow::initMenu()
|
||||
{
|
||||
std::vector<std::string> vecMenuId =
|
||||
@@ -146,9 +154,9 @@ void MainWindow::initMenu()
|
||||
void MainWindow::resizeEvent(QResizeEvent* event)
|
||||
{
|
||||
auto& size = event->size();
|
||||
if (webView)
|
||||
if (webView_)
|
||||
{
|
||||
webView->resize(size);
|
||||
webView_->resize(size);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -30,6 +30,8 @@ public:
|
||||
MainWindow();
|
||||
void initMenu();
|
||||
|
||||
void initWebView();
|
||||
|
||||
void resizeEvent(QResizeEvent* event);
|
||||
|
||||
public slots:
|
||||
@@ -51,5 +53,5 @@ public:
|
||||
|
||||
std::shared_ptr<Menu> menu_ = nullptr;
|
||||
|
||||
std::shared_ptr<QWebEngineView> webView;
|
||||
std::shared_ptr<QWebEngineView> webView_;
|
||||
};
|
||||
@@ -7,6 +7,21 @@
|
||||
#include "common/Logger.h"
|
||||
#include "Snowflake.h"
|
||||
#include "app/Dao.h"
|
||||
#include "app/Admin.h"
|
||||
#include "app/Device.h"
|
||||
|
||||
static void VariantListRes(std::vector<DataFields>& data, QVariantList& listRes)
|
||||
{
|
||||
for (auto& fields: data)
|
||||
{
|
||||
QVariantMap row;
|
||||
for (auto& field: fields.fields())
|
||||
{
|
||||
row[field.first.c_str()] = field.second.c_str();
|
||||
}
|
||||
listRes << row;
|
||||
}
|
||||
}
|
||||
|
||||
static void JSsetResPaginaion(QVariantMap& result, std::vector<DataFields>& data, int page, int pageSize, int count, int code, string err)
|
||||
{
|
||||
@@ -47,16 +62,16 @@ void MyWebHandler::log(const QString& text)
|
||||
|
||||
QString MyWebHandler::readFile(const QString& filename)
|
||||
{
|
||||
//std::string filePath = "assets/html/系统管理/index.html"; //filename.toStdString();
|
||||
std::filesystem::path filePath = std::filesystem::u8path(filename.toStdString());
|
||||
XLOGD() << "[cppNative] readFile: " << filePath;
|
||||
std::string fileName = filename.toStdString();
|
||||
XLOGD() << "[cppNative] readFile: " << fileName;
|
||||
std::filesystem::path filePath = std::filesystem::u8path(fileName);
|
||||
std::ifstream ifs(filePath);
|
||||
if (ifs.is_open())
|
||||
{
|
||||
// 获取文件大小
|
||||
ifs.seekg(0, std::ios::end);
|
||||
std::streamsize size = ifs.tellg();
|
||||
XLOGD() << "[cppNative] readFile [" << filePath << "] success, data size=" << size;
|
||||
XLOGD() << "[cppNative] readFile [" << fileName << "] success, data size=" << size;
|
||||
|
||||
// 定位回文件开始,读取文件内容到缓冲区
|
||||
ifs.seekg(0, std::ios::beg);
|
||||
@@ -67,11 +82,23 @@ QString MyWebHandler::readFile(const QString& filename)
|
||||
}
|
||||
else
|
||||
{
|
||||
XLOGD() << "[cppNative] readFile [" << filePath << "] failed.";
|
||||
XLOGD() << "[cppNative] readFile [" << fileName << "] failed.";
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
void MyWebHandler::login(const QString& username, const QString& passwd)
|
||||
{
|
||||
XLOGI() << "login request: " << username.toStdString();
|
||||
Errcode ecode = Admin::instance().longin(username.toStdString(), passwd.toStdString());
|
||||
|
||||
//std::this_thread::sleep_for(std::chrono::milliseconds(10000));
|
||||
emit signalLongin(username, int(ecode));
|
||||
}
|
||||
|
||||
void MyWebHandler::loginOut(const QString& username)
|
||||
{
|
||||
}
|
||||
|
||||
QVariantMap MyWebHandler::queryUserList(int page, int pageSize)
|
||||
{
|
||||
@@ -102,7 +129,7 @@ int MyWebHandler::insertUser(QVariantMap params)
|
||||
|
||||
|
||||
// 设置用户角色
|
||||
std::string user_id = fields.get_str("user_id");
|
||||
std::string user_id = fields.getStr("user_id");
|
||||
if (params.contains("role_id")) {
|
||||
|
||||
int role_id = params["role_id"].toInt();
|
||||
@@ -161,16 +188,24 @@ int MyWebHandler::updateUser(const QString& userId, QVariantMap params)
|
||||
// 角色管理接口
|
||||
QVariantMap MyWebHandler::queryRoleList(int page, int pageSize)
|
||||
{
|
||||
QVariantMap result;
|
||||
|
||||
std::vector<DataFields> res;
|
||||
auto dao = DAO::get("role");
|
||||
bool ret = dao->exec("SELECT * FROM role;", res);
|
||||
|
||||
QVariantMap result;
|
||||
JSsetResPaginaion(result, res, page, pageSize, res.size(), 0, "操作成功");
|
||||
|
||||
XLOGD() << "[cppNative] queryRoleList " << (ret ? "success." : "failed.");
|
||||
|
||||
//QtConcurrent::run([this]()
|
||||
// {
|
||||
// XLOGD() << "[cppNative] lxy ========== queryRoleList 1111111111111111111111 ";
|
||||
// QThread::msleep(2000);
|
||||
// XLOGD() << "[cppNative] lxy ========== queryRoleList 2222222222222222222222 ";
|
||||
// });
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
int MyWebHandler::insertRole(QVariantMap params)
|
||||
{
|
||||
DataFields fields;
|
||||
@@ -313,6 +348,66 @@ QVariantMap MyWebHandler::queryDeviceList(int page, int pageSize)
|
||||
return result;
|
||||
}
|
||||
|
||||
static void JSgetReqParamSql(QString key, QVariantMap& params, std::string& sql)
|
||||
{
|
||||
if (params.contains(key))
|
||||
{
|
||||
auto& v = params[key];
|
||||
std::string typeName = v.typeName();
|
||||
|
||||
|
||||
XLOGD() << key.toStdString() << " : " << typeName;
|
||||
if (!sql.empty()) sql += ",";
|
||||
|
||||
if ("QVariantList" == typeName)
|
||||
{
|
||||
std::string str = "";
|
||||
for (auto& item : v.toList())
|
||||
{
|
||||
if (!str.empty()) str += ",";
|
||||
str += ("'" + item.toString().toStdString() + "'");
|
||||
}
|
||||
sql += ("`" + key.toStdString() + "` IN (" + str + ")");
|
||||
XLOGD() << "QVariantList";
|
||||
}
|
||||
else if ("QString" == typeName)
|
||||
{
|
||||
// 如果是数组, 需要处理数组格式: ["","",""]
|
||||
sql += ("`" + key.toStdString() + "`='" + v.toString().toStdString() + "'");
|
||||
XLOGD() << "QString";
|
||||
}
|
||||
else {
|
||||
XLOGD() << "???";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
QVariantList MyWebHandler::queryDevice(QVariantMap params)
|
||||
{
|
||||
XLOGD() << "MyWebHandler::queryDevice -- params.size=" << params.size();
|
||||
|
||||
QVariantList result;
|
||||
|
||||
std::string sqlc = "";
|
||||
JSgetReqParamSql("type", params, sqlc);
|
||||
|
||||
if (sqlc.empty()) {
|
||||
return result;
|
||||
}
|
||||
|
||||
std::string sql = "SELECT * FROM device WHERE " + sqlc + ";";
|
||||
auto dao = DAO::get("device");
|
||||
std::vector<DataFields> res;
|
||||
bool ret = dao->exec(sql, res);
|
||||
XLOGD() << "sql=" << sql;
|
||||
XLOGD() << "queryDevice: size=" << res.size();
|
||||
|
||||
VariantListRes(res, result);
|
||||
|
||||
XLOGD() << "queryDevice: result size=" << result.size();
|
||||
return result;
|
||||
}
|
||||
|
||||
int MyWebHandler::insertDevice(QVariantMap params)
|
||||
{
|
||||
DataFields fields;
|
||||
@@ -600,5 +695,31 @@ QVariantMap MyWebHandler::querySecRecordList(int page, int pageSize)
|
||||
return result;
|
||||
};
|
||||
|
||||
int MyWebHandler::insertSecRecord(QVariantMap params) {};
|
||||
int MyWebHandler::updateSecRecord(const QString& policyId, QVariantMap params) {};
|
||||
int MyWebHandler::insertSecRecord(QVariantMap params) { return 0; };
|
||||
int MyWebHandler::updateSecRecord(const QString& policyId, QVariantMap params) { return 0; };
|
||||
|
||||
|
||||
QVariantList MyWebHandler::getDeviceInfo(const QVariantList& types)
|
||||
{
|
||||
std::vector<std::shared_ptr<DeviceEntity>> vecDevice;
|
||||
for (auto item: types)
|
||||
{
|
||||
int deviceType = item.toInt();
|
||||
auto vecRes = Device::getDeviceByType(deviceType);
|
||||
vecDevice.insert(vecDevice.end(), vecRes.begin(), vecRes.end());
|
||||
}
|
||||
|
||||
QVariantList result;
|
||||
for (auto& device: vecDevice)
|
||||
{
|
||||
QVariantMap row;
|
||||
row["device_id"] = device->deviceId;
|
||||
row["name"] = device->name.c_str();
|
||||
row["type"] = device->type;
|
||||
row["status"] = device->status;
|
||||
row["online"] = device->online;
|
||||
row["err"] = device->err;
|
||||
result << row;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
@@ -4,6 +4,7 @@
|
||||
#include <QStringList>
|
||||
#include <QVariantMap>
|
||||
#include <QVariantList>
|
||||
#include <QtConcurrent/QtConcurrent>
|
||||
|
||||
class MyWebHandler : public QObject
|
||||
{
|
||||
@@ -22,7 +23,11 @@ signals:
|
||||
//在C++中定义的信号,可以在JS端监听此信号接收消息
|
||||
void signalNativeTextChanged(const QString& text);
|
||||
|
||||
void singalReadFileFinished(const QString& text);
|
||||
void signalReadFileFinished(const QString& text);
|
||||
|
||||
void signalLongin(const QString& username, const int& ecode);
|
||||
|
||||
void signalLonout();
|
||||
|
||||
public slots:
|
||||
//C++ 端的公共槽函数,可以在JS端调用。
|
||||
@@ -32,6 +37,13 @@ public slots:
|
||||
|
||||
QString readFile(const QString& filename);
|
||||
|
||||
|
||||
// ================================================================================================================
|
||||
// 登录,返回用户信息
|
||||
void login(const QString& username, const QString& password);
|
||||
// 登出
|
||||
void loginOut(const QString& username);
|
||||
|
||||
// ================================================================================================================
|
||||
// 用户管理接口
|
||||
QVariantMap queryUserList(int page, int pageSize);
|
||||
@@ -64,6 +76,8 @@ public slots:
|
||||
// ================================================================================================================
|
||||
// 设备管理接口
|
||||
QVariantMap queryDeviceList(int page, int pageSize);
|
||||
|
||||
QVariantList queryDevice(QVariantMap params);
|
||||
int insertDevice(QVariantMap params);
|
||||
int deleteDevice(const QString& deviceId);
|
||||
int updateDevice(const QString& deviceId, QVariantMap params);
|
||||
@@ -101,6 +115,9 @@ public slots:
|
||||
int insertSecRecord(QVariantMap params);
|
||||
int updateSecRecord(const QString& policyId, QVariantMap params);
|
||||
|
||||
// ================================================================================================================
|
||||
QVariantList getDeviceInfo(const QVariantList& types);
|
||||
|
||||
public:
|
||||
QString nativeText_;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user