实现HTTP服务架构

This commit is contained in:
lixiaoyuan
2025-08-31 14:38:53 +08:00
parent 4af4e670d2
commit e0b64a20c4
46 changed files with 1436 additions and 545 deletions

View File

@@ -5,8 +5,8 @@
#include "app/Policy.h"
#include "database/Dao.h"
#include "common/JsonN.h"
#include "common/Snowflake.h"
#include "common/Utils.h"
void ElectPeriod::parse(std::string jsonstr)
{
@@ -216,7 +216,6 @@ void AppData::initFromDB()
void AppData::init()
{
this->initFromDB();
this->initUser();
}
std::shared_ptr<Station> AppData::getStation(int stationId)
@@ -276,12 +275,35 @@ void AppData::loadStatData()
{
}
void AppData::initUser()
static std::map<std::string, User> g_mapUser;
std::string AppData::userLogin(std::string userId, std::string account)
{
auto dao = DaoEntity::create("");
std::vector<Fields> result;
for (auto iter = g_mapUser.begin(); iter!=g_mapUser.end(); ++iter)
{
if (iter->second.userId == userId) // 重复登录
{
g_mapUser.erase(iter);
break;
}
}
User user;
user.userId = userId;
user.account = account;
user.loginTime = Utils::time();
user.token = Snowflake::instance().getIdStr();
g_mapUser[user.token] = user;
return user.token;
}
User AppData::getUser(std::string token)
{
auto iter = g_mapUser.find(token);
if (iter != g_mapUser.end())
{
return iter->second;
}
return User();
}
int AppData::getWorkModeIdByName(std::string name)
{

View File

@@ -6,6 +6,7 @@
#include <map>
#include <unordered_map>
#include "common/Fields.h"
#include "app/Config.h"
class Station;
class Device;
@@ -13,6 +14,14 @@ class MyPolicy;
using VecPairSS = std::vector<std::pair<std::string, std::string>>;
struct User
{
std::string userId;
std::string account;
std::string token;
int64_t loginTime {};
};
struct DeviceType
{
int typeId {};
@@ -50,13 +59,11 @@ public:
void init();
void initFromDB();
// 读取统计数据: 今日统计数据,累计统计数据
void loadStatData();
void initUser();
std::string userLogin(std::string userId, std::string account);
User getUser(std::string token);
std::shared_ptr<Station> getStation(int stationId);

View File

@@ -8,6 +8,8 @@
#include "app/Station.h"
#include "app/Device.h"
void Application::init()
{
// 初始化系统配置,读取配置文件

View File

@@ -4,6 +4,7 @@
#include "common/JsonN.h"
#include "Logger.h"
#include "AppData.h"
AppOption Config::option;
@@ -34,5 +35,11 @@ bool Config::init(std::string filename)
XLOGI() << "[APP] load database config error: not found. host=" << option.database.host;
}
if (jsonroot.contains("token"))
{
std::string token = jsonroot["token"];
option.useToken = !token.empty();
}
return true;
}

View File

@@ -14,6 +14,7 @@ struct DatabaseOption
struct AppOption
{
DatabaseOption database;
bool useToken {true};
};
class Config

11
src/app/Constants.h Normal file
View File

@@ -0,0 +1,11 @@
#ifndef _CONSTANTS_H_
#define _CONSTANTS_H_
#include <string>
namespace CONST
{
extern const std::string VAR;
}
#endif // !_CONSTANTS_H_

View File

@@ -15,8 +15,9 @@ std::shared_ptr<DaoEntity> DAO1::get(std::string tableName)
return std::make_shared<DaoEntity>(tableName);
}
Errcode DAO1::login(std::shared_ptr<DaoEntity> dao, std::string account, std::string passwd, std::string& err)
Errcode DAO1::login(std::shared_ptr<DaoEntity> dao, std::string account, std::string passwd, Fields& fields)
{
std::string err;
std::string t = Utils::timeStr();
if (!dao)
{
@@ -29,23 +30,24 @@ Errcode DAO1::login(std::shared_ptr<DaoEntity> dao, std::string account, std::st
return Errcode::ERR_DB_CONN;
}
std::string sql = "SELECT * FROM user WHERE account='" + account + "';";
std::string sql = "SELECT u.*, ur.role_id FROM `user` u"
" LEFT JOIN user_role ur ON u.user_id = ur.user_id WHERE u.account=" + account + "';";
std::vector<Fields> res;
bool ret = dao->exec(sql, res);
std::vector<Fields> result;
bool ret = dao->exec(sql, result);
if (!ret)
{
err = "数据库操作错误";
DAO1::writeSystemLog(dao, 2, "", account, "用户登录失败:" + err);
return Errcode::ERR_DB_CONN;
}
if (res.size() <=0)
if (result.size() <=0)
{
err = "用户不存在";
DAO1::writeSystemLog(dao, 2, "", account, "用户登录失败:" + err);
return Errcode::ERR_LOGIN_USER_NOTEXIST;
}
Fields& fields = res[0];
fields = result[0];
std::string userId = fields.value("user_id");
int loginCount = fields.get<int>("login_count");
@@ -57,6 +59,15 @@ Errcode DAO1::login(std::shared_ptr<DaoEntity> dao, std::string account, std::st
return Errcode::ERR_LOGIN_PASSWD;
}
// 读取用户权限
{
result.clear();
std::string sql = "SELECT rp.role_id, rp.permission_id, p.name FROM role_permission rp "
"LEFT JOIN permission p ON p.permission_id = rp.permission_id"
"WHERE rp.role_id = 1;";
}
err = "登录成功";
// 数据库更新用户登录信息

View File

@@ -7,7 +7,7 @@ class DAO1
public:
static std::shared_ptr<DaoEntity> get(std::string tableName="");
static Errcode login(std::shared_ptr<DaoEntity> dao, std::string account, std::string passwd, std::string& err);
static Errcode login(std::shared_ptr<DaoEntity> dao, std::string account, std::string passwd, Fields& res);
static bool writeSystemLog(std::shared_ptr<DaoEntity> dao, int type, std::string userId, std::string account, std::string text);

View File

@@ -37,7 +37,7 @@ int Device::startComm()
{
if (!isOpen)
{
if (commEntity && commEntity->isAlive())
if (commEntity && commEntity->alive)
{
commEntity->close();
}

24
src/app/errcode.cpp Normal file
View File

@@ -0,0 +1,24 @@
#include "errcode.h"
static std::unordered_map<Errcode, std::string> mapErr =
{
{Errcode::OK, "操作成功"},
{Errcode::ERR, "系统错误"},
{Errcode::ERR_TOKEN, "TOKEN错误"},
{Errcode::ERR_PARAM, "参数错误"},
{Errcode::ERR_PARAM_NUL, "缺少参数"},
{Errcode::ERR_LOGIN_USER_NOTEXIST, "用户不存在"},
{Errcode::ERR_LOGIN_PASSWD, "密码错误"},
{Errcode::ERR_DB_CONN, "数据库连接错误"},
{Errcode::ERR_DB_DUPLICATE, "数据库数据重复"},
{Errcode::ERR_DB_SQL, "数据库执行错误"},
{Errcode::ERR_DB_VAL, "参数值错误"},
};
std::string ErrcodeStr(Errcode code)
{
auto iter = mapErr.find(code);
return (iter != mapErr.end() ? iter->second : "");
}

View File

@@ -1,16 +1,28 @@
#pragma once
#include <string>
#include <unordered_map>
enum class Errcode
{
OK = 0,
ERR = 1,
ERR_TOKEN, // TOKEN错误
ERR_PARAM, // 参数错误
ERR_PARAM_NUL, // 缺少参数
ERR = 100,
ERR_DB_CONN = 101, // 数据库连接错误
ERR_DB_SQL = 102, // 数据库查询SQL错误
ERR_DB_DUPLICATE, // 数据重复
ERR_USER = 100,
ERR_LOGIN_USER_NOTEXIST, // 登入错误,用户不存在
ERR_LOGIN_PASSWD, // 登入错误,密码不正确
};
ERR_DEVICE = 200,
ERR_DB_CONN = 1001, // 数据库连接错误
ERR_DB_DUPLICATE = 1062, // Duplicate entry for key
ERR_DB_SQL = 1064, // 数据库查询SQL错误
ERR_DB_VAL = 1366, // 1366,Incorrect decimal value通常为参数值错误例如空值、值类型错误
};
extern std::string ErrcodeStr(Errcode code);