mirror of
https://gitee.com/js-yhsec/energy_storage.git
synced 2026-05-27 18:59:26 +08:00
上传项目代码
This commit is contained in:
0
src/app/AppSetting.cpp
Normal file
0
src/app/AppSetting.cpp
Normal file
12
src/app/AppSetting.h
Normal file
12
src/app/AppSetting.h
Normal file
@@ -0,0 +1,12 @@
|
||||
#pragma once
|
||||
|
||||
class AppSetting
|
||||
{
|
||||
public:
|
||||
static AppSetting& instance() {
|
||||
static AppSetting inst;
|
||||
return inst;
|
||||
}
|
||||
|
||||
|
||||
};
|
||||
21
src/app/Application.cpp
Normal file
21
src/app/Application.cpp
Normal file
@@ -0,0 +1,21 @@
|
||||
#include "Application.h"
|
||||
|
||||
#include "common/Utils.h"
|
||||
|
||||
void Application::init()
|
||||
{
|
||||
std::thread([=]()
|
||||
{
|
||||
while (!isQuit()) { runThreadMain(); }
|
||||
}).detach();
|
||||
}
|
||||
|
||||
void Application::runThreadMain()
|
||||
{
|
||||
static TimeTick tt;
|
||||
tt.elapse(1000);
|
||||
|
||||
//XLOGD() << "HelloWorld";
|
||||
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(1));
|
||||
}
|
||||
28
src/app/Application.h
Normal file
28
src/app/Application.h
Normal file
@@ -0,0 +1,28 @@
|
||||
#pragma once
|
||||
|
||||
#include <thread>
|
||||
#include "common/Logger.h"
|
||||
|
||||
#include "Operator.h"
|
||||
|
||||
class Application
|
||||
{
|
||||
public:
|
||||
static Application& instance()
|
||||
{
|
||||
static Application app;
|
||||
return app;
|
||||
}
|
||||
|
||||
void init();
|
||||
bool isQuit() { return isQuit_; }
|
||||
Operator& getOperator() { return op_; }
|
||||
|
||||
void runThreadMain();
|
||||
|
||||
private:
|
||||
bool isQuit_ = false;
|
||||
|
||||
// 登录的管理员信息
|
||||
Operator op_;
|
||||
};
|
||||
147
src/app/Dao.cpp
Normal file
147
src/app/Dao.cpp
Normal file
@@ -0,0 +1,147 @@
|
||||
#include "Dao.h"
|
||||
#include "common/Logger.h"
|
||||
#include "common/Utils.h"
|
||||
#include "common/Snowflake.h"
|
||||
|
||||
enum class EnDatabaseErr
|
||||
{
|
||||
SUCCESS = 0,
|
||||
};
|
||||
|
||||
std::shared_ptr<DaoEntity> DAO::get(std::string tableName)
|
||||
{
|
||||
return std::make_shared<DaoEntity>(tableName);
|
||||
}
|
||||
|
||||
bool DAO::login(std::shared_ptr<DaoEntity> dao, std::string account, std::string passwd, std::string& err)
|
||||
{
|
||||
std::string t = Utils::timeNowStr();
|
||||
if (!dao)
|
||||
{
|
||||
dao = std::make_shared<DaoEntity>("");
|
||||
}
|
||||
if (!dao->isConnected())
|
||||
{
|
||||
err = "数据库连接错误";
|
||||
DAO::writeSystemLog(dao, 2, "", account, "用户登录失败:" + err);
|
||||
return false;
|
||||
}
|
||||
|
||||
std::string sql = "SELECT * FROM user WHERE account='" + account + "';";
|
||||
|
||||
std::vector<DataFields> res;
|
||||
bool ret = dao->exec(sql, res);
|
||||
if (!ret)
|
||||
{
|
||||
err = "数据库操作错误";
|
||||
DAO::writeSystemLog(dao, 2, "", account, "用户登录失败:" + err);
|
||||
return false;
|
||||
}
|
||||
if (res.size() <=0)
|
||||
{
|
||||
err = "用户不存在";
|
||||
DAO::writeSystemLog(dao, 2, "", account, "用户登录失败:" + err);
|
||||
return false;
|
||||
}
|
||||
DataFields& fields = res[0];
|
||||
std::string userId = fields.get_str("user_id");
|
||||
int loginCount = fields.get_int("login_count");
|
||||
|
||||
// 判断密码
|
||||
if (passwd != fields.get_str("passwd"))
|
||||
{
|
||||
err = "密码错误";
|
||||
DAO::writeSystemLog(dao, 2, userId, account, "用户登录失败:" + err);
|
||||
return false;
|
||||
}
|
||||
|
||||
err = "登录成功";
|
||||
|
||||
// 数据库更新用户登录信息
|
||||
sql = "UPDATE user SET login_time='" + t + "', login_count='" + std::to_string(loginCount + 1) + "' WHERE user_id = '" + userId + "'; ";
|
||||
ret = dao->exec(sql);
|
||||
if (!ret)
|
||||
{
|
||||
XLOGE() << "更新用户登录信息失败:sql=" << sql;
|
||||
}
|
||||
|
||||
DAO::writeSystemLog(dao, 2, userId, account, "用户登录成功");
|
||||
return true;
|
||||
}
|
||||
|
||||
bool DAO::writeSystemLog(std::shared_ptr<DaoEntity> dao, int type, std::string userId, std::string account, std::string text)
|
||||
{
|
||||
if (!dao)
|
||||
{
|
||||
dao = std::make_shared<DaoEntity>("");
|
||||
}
|
||||
if (!dao->isConnected())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// 数据库写入登录日志
|
||||
dao->setTableName("system_log");
|
||||
DataFields fieldsLog;
|
||||
fieldsLog.set("id", Snowflake::instance().nextIdStr());
|
||||
fieldsLog.set("type", 2);
|
||||
fieldsLog.set("user_id", userId);
|
||||
fieldsLog.set("user_account", account);
|
||||
fieldsLog.set("content", text);
|
||||
fieldsLog.set("log_time", Utils::timeNowStr());
|
||||
bool ret = dao->insertFields({fieldsLog});
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
bool DAO::queryUser(std::vector<DataFields>& res)
|
||||
{
|
||||
std::shared_ptr<DaoEntity> dao = std::make_shared<DaoEntity>("");
|
||||
if (!dao->isConnected())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
std::string sql = "SELECT u.*, r.role_id, r.name role_name from USER u LEFT JOIN user_role ur ON u.user_id = ur.user_id LEFT JOIN `role` r ON r.role_id = ur.role_id;";
|
||||
bool ret = dao->exec(sql, res);
|
||||
return ret;
|
||||
}
|
||||
|
||||
int DAO::insertUser(DataFields& fields)
|
||||
{
|
||||
std::shared_ptr<DaoEntity> dao = std::make_shared<DaoEntity>("user");
|
||||
if (!dao->isConnected())
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
std::string account = fields.get_str("account");
|
||||
|
||||
// step1: 查询
|
||||
std::vector<DataFields> res;
|
||||
bool ret = dao->exec("SELECT * from user WHERE account='" + account + "';", res);
|
||||
if (!ret)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
if (res.size() > 0)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
fields.set("user_id", Snowflake::instance().nextIdStr());
|
||||
fields.set("create_time", Utils::timeNowStr());
|
||||
ret = dao->insertFields(fields);
|
||||
return (ret) ? 0 : 1;
|
||||
}
|
||||
|
||||
int DAO::updateUserById(std::string id, DataFields& fields)
|
||||
{
|
||||
std::shared_ptr<DaoEntity> dao = std::make_shared<DaoEntity>("user");
|
||||
if (!dao->isConnected())
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
string sqlC = "WHERE user_id='" + id + "'";
|
||||
bool ret = dao->updateFields(fields, sqlC);
|
||||
return (ret) ? 0 : 1;
|
||||
}
|
||||
30
src/app/Dao.h
Normal file
30
src/app/Dao.h
Normal file
@@ -0,0 +1,30 @@
|
||||
#include "database/DaoEntity.h"
|
||||
|
||||
class DAO
|
||||
{
|
||||
public:
|
||||
static std::shared_ptr<DaoEntity> get(std::string tableName="");
|
||||
|
||||
static bool login(std::shared_ptr<DaoEntity> dao, std::string account, std::string passwd, std::string& err);
|
||||
|
||||
static bool writeSystemLog(std::shared_ptr<DaoEntity> dao, int type, std::string userId, std::string account, std::string text);
|
||||
|
||||
|
||||
// =======================================================================
|
||||
// 用户管理数据操作
|
||||
|
||||
/**
|
||||
* 查询用户
|
||||
*/
|
||||
static bool queryUser(std::vector<DataFields>& res);
|
||||
|
||||
/**
|
||||
* 新增用户
|
||||
*/
|
||||
static int insertUser(DataFields& fields);
|
||||
|
||||
/**
|
||||
* 修改用户信息
|
||||
*/
|
||||
static int updateUserById(std::string id, DataFields& fields);
|
||||
};
|
||||
0
src/app/Device.cpp
Normal file
0
src/app/Device.cpp
Normal file
7
src/app/Device.h
Normal file
7
src/app/Device.h
Normal file
@@ -0,0 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
class Device
|
||||
{
|
||||
public:
|
||||
static bool init();
|
||||
};
|
||||
18
src/app/Operator.cpp
Normal file
18
src/app/Operator.cpp
Normal file
@@ -0,0 +1,18 @@
|
||||
#include "Operator.h"
|
||||
#include "DAO.h"
|
||||
#include "common/Logger.h"
|
||||
|
||||
bool Operator::login(std::string account, std::string passwd, std::string& err)
|
||||
{
|
||||
bool ret = DAO::login(nullptr, account, passwd, err);
|
||||
|
||||
if (ret)
|
||||
{
|
||||
XLOGD() << "用户[" + account + "]登录成功";
|
||||
}
|
||||
else
|
||||
{
|
||||
XLOGD() << "用户[" + account + "]登录失败: " << err;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
16
src/app/Operator.h
Normal file
16
src/app/Operator.h
Normal file
@@ -0,0 +1,16 @@
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
|
||||
class Operator
|
||||
{
|
||||
public:
|
||||
bool login(std::string account, std::string passwd, std::string& err);
|
||||
|
||||
bool logout(std::string& err);
|
||||
|
||||
|
||||
std::string account_;
|
||||
std::string passwd_;
|
||||
|
||||
};
|
||||
Reference in New Issue
Block a user