From 8520effc3a1da1e814dbe82e1e86e982274b58f7 Mon Sep 17 00:00:00 2001 From: HwangKC Date: Wed, 5 Jun 2024 20:52:33 +0800 Subject: [PATCH] a --- applications/WebConfigure/cgiCommon/Makefile | 17 +- .../WebConfigure/cgiCommon/Makefile.arm | 16 +- .../WebConfigure/cgiCommon/cgiCommon.sln | 26 --- .../WebConfigure/cgiCommon/cgiCommon.vcxproj | 4 + .../cgiCommon/handleLoginRequest.cpp | 35 ++- applications/WebConfigure/cgiCommon/main.cpp | 37 ++- .../WebConfigure/cgiCommon/opmysql.cpp | 212 ++++++++++++++++++ applications/WebConfigure/cgiCommon/opmysql.h | 31 +++ 8 files changed, 333 insertions(+), 45 deletions(-) create mode 100644 applications/WebConfigure/cgiCommon/opmysql.cpp create mode 100644 applications/WebConfigure/cgiCommon/opmysql.h diff --git a/applications/WebConfigure/cgiCommon/Makefile b/applications/WebConfigure/cgiCommon/Makefile index 58cc3f2..be8d32e 100644 --- a/applications/WebConfigure/cgiCommon/Makefile +++ b/applications/WebConfigure/cgiCommon/Makefile @@ -13,13 +13,20 @@ TARGET := commonAction #compile and lib parameter #编译参数 CC := g++ -CXX := g++ -LIBS := -L/usr/local/lib -lhv -lsqlite3 -lpthread -ldl -static -lcgicc +CXX := g++ +LIBS := -L/usr/local/lib -lhv -lmysqlcppconn -lmysqlclient_r -lcgicc -lpthread -ldl LDFLAGS := DEFINES := INCLUDE := -I. -I/usr/local/include -CFLAGS := -g -Wall -O3 $(DEFINES) $(INCLUDE) -std=c++11 -CXXFLAGS:= $(CFLAGS) -DHAVE_CONFIG_H +CFLAGS := -Wall -O3 $(DEFINES) $(INCLUDE) -std=c++11 +CXXFLAGS:= $(CFLAGS) + +# 根据 DEBUG 宏决定是否包含调试信息 +ifeq ($(DEBUG), 1) + CFLAGS += -g -D_DEBUG +else ifeq ($(debug),1) + CFLAGS += -g -D_DEBUG +endif #i think you should do anything here #下面的基本上不需要做任何改动了 @@ -38,7 +45,7 @@ rebuild: veryclean everything clean : - rm -fr *.so *.a + rm -fr *.so so rm -fr *.o diff --git a/applications/WebConfigure/cgiCommon/Makefile.arm b/applications/WebConfigure/cgiCommon/Makefile.arm index 961bfcf..6ccca7a 100644 --- a/applications/WebConfigure/cgiCommon/Makefile.arm +++ b/applications/WebConfigure/cgiCommon/Makefile.arm @@ -8,19 +8,29 @@ OBJS := $(patsubst %.c,%.o,$(patsubst %.cpp,%.o,$(SOURCE))) #target you can change test to what you want #目标文件名,输入任意你想要的执行文件名 -TARGET := common.cgi +TARGET := commonAction #compile and lib parameter #编译参数 CC := arm-g++ CXX := arm-g++ -LIBS := -L/usr/local/arm/lib -lhv -lsqlite3 -lcgicc -lpthread -ldl +LIBS := -L/usr/local/arm/lib -lhv -lmysqlcppconn-static -lmysqlclient_r -lpthread -ldl -static -lcgicc +#LIBS := -L/usr/local/arm/lib -lhv -lmysqlcppconn -lmysqlclient_r -lpthread -ldl -lcgicc LDFLAGS := DEFINES := -INCLUDE := -I. -I/usr/local/arm/include +INCLUDE := -I. -I/usr/local/arm/include -I/usr/local/arm/include/mysql CFLAGS := -Wall -O3 $(DEFINES) $(INCLUDE) -std=c++11 CXXFLAGS:= $(CFLAGS) + +# 根据 DEBUG 宏决定是否包含调试信息 +ifeq ($(DEBUG), 1) + CFLAGS += -g -D_DEBUG +else ifeq ($(debug),1) + CFLAGS += -g -D_DEBUG +endif + + #i think you should do anything here #下面的基本上不需要做任何改动了 .PHONY : everything objs clean veryclean rebuild diff --git a/applications/WebConfigure/cgiCommon/cgiCommon.sln b/applications/WebConfigure/cgiCommon/cgiCommon.sln index 1dd6e37..97a945e 100644 --- a/applications/WebConfigure/cgiCommon/cgiCommon.sln +++ b/applications/WebConfigure/cgiCommon/cgiCommon.sln @@ -5,8 +5,6 @@ VisualStudioVersion = 16.0.34301.259 MinimumVisualStudioVersion = 10.0.40219.1 Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "cgiCommon", "cgiCommon.vcxproj", "{EC275930-0379-4487-8C30-EAFE2C124618}" EndProject -Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "libopdb", "..\..\library\libopdb\libopdb.vcxproj", "{E8431BFC-D508-428C-AC1D-A64539C5FC18}" -EndProject Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "jsondemo2", "..\..\examples\jsondemo2\jsondemo2.vcxproj", "{A7EA55C7-9DC1-4903-9470-66F3BF247D28}" EndProject Global @@ -45,30 +43,6 @@ Global {EC275930-0379-4487-8C30-EAFE2C124618}.Release|x86.ActiveCfg = Release|x86 {EC275930-0379-4487-8C30-EAFE2C124618}.Release|x86.Build.0 = Release|x86 {EC275930-0379-4487-8C30-EAFE2C124618}.Release|x86.Deploy.0 = Release|x86 - {E8431BFC-D508-428C-AC1D-A64539C5FC18}.Debug|ARM.ActiveCfg = Debug|ARM - {E8431BFC-D508-428C-AC1D-A64539C5FC18}.Debug|ARM.Build.0 = Debug|ARM - {E8431BFC-D508-428C-AC1D-A64539C5FC18}.Debug|ARM.Deploy.0 = Debug|ARM - {E8431BFC-D508-428C-AC1D-A64539C5FC18}.Debug|ARM64.ActiveCfg = Debug|ARM64 - {E8431BFC-D508-428C-AC1D-A64539C5FC18}.Debug|ARM64.Build.0 = Debug|ARM64 - {E8431BFC-D508-428C-AC1D-A64539C5FC18}.Debug|ARM64.Deploy.0 = Debug|ARM64 - {E8431BFC-D508-428C-AC1D-A64539C5FC18}.Debug|x64.ActiveCfg = Debug|x64 - {E8431BFC-D508-428C-AC1D-A64539C5FC18}.Debug|x64.Build.0 = Debug|x64 - {E8431BFC-D508-428C-AC1D-A64539C5FC18}.Debug|x64.Deploy.0 = Debug|x64 - {E8431BFC-D508-428C-AC1D-A64539C5FC18}.Debug|x86.ActiveCfg = Debug|x86 - {E8431BFC-D508-428C-AC1D-A64539C5FC18}.Debug|x86.Build.0 = Debug|x86 - {E8431BFC-D508-428C-AC1D-A64539C5FC18}.Debug|x86.Deploy.0 = Debug|x86 - {E8431BFC-D508-428C-AC1D-A64539C5FC18}.Release|ARM.ActiveCfg = Release|ARM - {E8431BFC-D508-428C-AC1D-A64539C5FC18}.Release|ARM.Build.0 = Release|ARM - {E8431BFC-D508-428C-AC1D-A64539C5FC18}.Release|ARM.Deploy.0 = Release|ARM - {E8431BFC-D508-428C-AC1D-A64539C5FC18}.Release|ARM64.ActiveCfg = Release|ARM64 - {E8431BFC-D508-428C-AC1D-A64539C5FC18}.Release|ARM64.Build.0 = Release|ARM64 - {E8431BFC-D508-428C-AC1D-A64539C5FC18}.Release|ARM64.Deploy.0 = Release|ARM64 - {E8431BFC-D508-428C-AC1D-A64539C5FC18}.Release|x64.ActiveCfg = Release|x64 - {E8431BFC-D508-428C-AC1D-A64539C5FC18}.Release|x64.Build.0 = Release|x64 - {E8431BFC-D508-428C-AC1D-A64539C5FC18}.Release|x64.Deploy.0 = Release|x64 - {E8431BFC-D508-428C-AC1D-A64539C5FC18}.Release|x86.ActiveCfg = Release|x86 - {E8431BFC-D508-428C-AC1D-A64539C5FC18}.Release|x86.Build.0 = Release|x86 - {E8431BFC-D508-428C-AC1D-A64539C5FC18}.Release|x86.Deploy.0 = Release|x86 {A7EA55C7-9DC1-4903-9470-66F3BF247D28}.Debug|ARM.ActiveCfg = Debug|Win32 {A7EA55C7-9DC1-4903-9470-66F3BF247D28}.Debug|ARM64.ActiveCfg = Debug|Win32 {A7EA55C7-9DC1-4903-9470-66F3BF247D28}.Debug|x64.ActiveCfg = Debug|x64 diff --git a/applications/WebConfigure/cgiCommon/cgiCommon.vcxproj b/applications/WebConfigure/cgiCommon/cgiCommon.vcxproj index 57fc77f..c9dd358 100644 --- a/applications/WebConfigure/cgiCommon/cgiCommon.vcxproj +++ b/applications/WebConfigure/cgiCommon/cgiCommon.vcxproj @@ -89,6 +89,8 @@ + + @@ -96,6 +98,8 @@ + + diff --git a/applications/WebConfigure/cgiCommon/handleLoginRequest.cpp b/applications/WebConfigure/cgiCommon/handleLoginRequest.cpp index ceb9fb2..b62c8b5 100644 --- a/applications/WebConfigure/cgiCommon/handleLoginRequest.cpp +++ b/applications/WebConfigure/cgiCommon/handleLoginRequest.cpp @@ -6,8 +6,9 @@ #include #include -#include #include "handleHeader.h" +#include "openjson.h" +#include "opmysql.h" using namespace cgicc; @@ -59,18 +60,34 @@ void handleLoginRequest(cgicc::Cgicc& cgi) std::cout << "
\n"; // 处理登录逻辑... - + //从数据库中查询 + bool dbok = OpDatabase::getInstance()->OpenDatabase("127.0.0.1", "root"); + std::string jsonResult; + if (!dbok) + { + std::cout << "
\n"; + std::cout << ("failed to retrieve user info") << std::endl; + } + else + { + + if (!OpDatabase::getInstance()->queryUser(username, passwd, jsonResult)) + { + std::cout << "
\n"; + std::cout << ("invalid user") << std::endl; + } + else + { + std::cout << "
\n"; + std::cout << h1("Login Successful!") << std::endl; + } + } std::cout << "
\n"; - std::cout << username.c_str() << std::endl; - - std::cout << "
\n"; - std::cout << passwd.c_str() << std::endl; - - std::cout << "
\n"; - std::cout << h1("Login Successful!") << std::endl; + std::cout << jsonResult << std::endl; // Close the document std::cout << body() << html(); + #endif } diff --git a/applications/WebConfigure/cgiCommon/main.cpp b/applications/WebConfigure/cgiCommon/main.cpp index 49235e5..6adc0ec 100644 --- a/applications/WebConfigure/cgiCommon/main.cpp +++ b/applications/WebConfigure/cgiCommon/main.cpp @@ -5,6 +5,9 @@ #include "handleHeader.h" +#include "openjson.h" +#include "opmysql.h" + // 浣犵殑CGI澶勭悊鍑芥暟澹版槑 void handleLoginRequest(cgicc::Cgicc& cgi); void handleSignupRequest(cgicc::Cgicc& cgi); @@ -28,22 +31,52 @@ void handleVersion(cgicc::Cgicc& cgi) std::cout << body() << std::endl; - //std::cout << h1("Version 1.0.531") << std::endl; #define cgiVersion "1.0.531" const char* VersionInfo = "Version: " cgiVersion; const char* BuildInfo = "Build Info: " __DATE__ " " __TIME__; std::cout << h1(VersionInfo) << std::endl; - std::cout << h1(BuildInfo) << std::endl; + std::cout << h2(BuildInfo) << std::endl; // Close the document std::cout << body() << html(); } +void handleTest() +{ + // 澶勭悊鐧诲綍閫昏緫... + //浠庢暟鎹簱涓煡璇 + bool dbok = OpDatabase::getInstance()->OpenDatabase("127.0.0.1", "root"); + std::string jsonResult; + if (!dbok) + { + std::cout << ("failed to retrieve user info"); + } + else + { + + if (!OpDatabase::getInstance()->queryUser("hkc", "1c915f2c045ea7628e3cf170bfa59f51", jsonResult)) + { + std::cout << ("invalid user") << std::endl; + } + else + { + std::cout << ("Login Successful!") << std::endl; + } + } + + std::cout << jsonResult << std::endl; +} + int main() { +#if 0 + handleTest(); + + return 1; +#endif using namespace cgicc; using namespace std; diff --git a/applications/WebConfigure/cgiCommon/opmysql.cpp b/applications/WebConfigure/cgiCommon/opmysql.cpp new file mode 100644 index 0000000..18be558 --- /dev/null +++ b/applications/WebConfigure/cgiCommon/opmysql.cpp @@ -0,0 +1,212 @@ +#include "opmysql.h" +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "openjson.h" + +OpDatabase OpDatabase::m_instance; + +OpDatabase::OpDatabase() + :m_pDbConnection(nullptr) +{ +} + +OpDatabase::~OpDatabase() +{ + //CloseDatabase(); +} + +std::string OpDatabase::CalculateMD5(const std::string& data) +{ + char md5_str[33] = { 0 }; + hv_md5_hex((unsigned char*)data.c_str(), data.size(), md5_str, 33); + + std::string smd5(md5_str); + std::transform(smd5.begin(), smd5.end(), smd5.begin(), + [](unsigned char c) + { + return std::toupper(c); + }); + return smd5; +} + +OpDatabase* OpDatabase::getInstance() +{ + return &m_instance; +} + +bool OpDatabase::OpenDatabase(const std::string& server, const std::string& dbuser, const std::string& database) +{ + // 打开数据库 + try + { + // 数据库连接配置 + //std::string server = "tcp://127.0.0.1:3306"; + //std::string dbuser = "root"; + std::string password = "Hj57471000"; + //std::string database = "hjems"; + + // 创建连接 + sql::mysql::MySQL_Driver* driver; + driver = sql::mysql::get_mysql_driver_instance(); + //m_pDbConnection.reset(driver->connect(server, dbuser, password)); + m_pDbConnection = driver->connect(server, dbuser, password); + + if (!m_pDbConnection) + { + hloge("Failed to connect to database."); + return false; + } + + // 设置为使用指定数据库 + m_pDbConnection->setSchema(database); + + return true; + } + catch (sql::SQLException& e) + { + std::ostringstream ss; + ss << "SQLException: " << e.what(); + ss<< " (MySQL error code: " << e.getErrorCode(); + ss << ", SQLState: " << e.getSQLState() << " )"; + hloge("Failed to connect to database: %s",ss.str().c_str()); + return false; + } +} + +// 关闭数据库连接 +void OpDatabase::CloseDatabase() +{ + m_pDbConnection->close(); + //delete m_pDbConnection; +} + + +void OpDatabase::InsertMessage(const std::string& ts, const std::string& msg_type, const std::string& fsu, const std::string& content, int topic, int dev_id) +{ + const char* insertSql = "INSERT INTO tbl_data (data_timestamp, data_type, FsuCode, data_content, topic, device_id) VALUES (?,?,?,?,?,?);"; + // 创建预编译的prepared statement + std::unique_ptr insertStmt(m_pDbConnection->prepareStatement(insertSql)); + + // 绑定插入数据的参数 + insertStmt->setString(1, ts); + insertStmt->setString(2, msg_type); + insertStmt->setString(3, fsu); + insertStmt->setString(4, content); + insertStmt->setInt(5, topic); + insertStmt->setInt(6, dev_id); + + // 执行插入操作 + int rowsAffected = insertStmt->executeUpdate(); + + // 检查插入操作是否成功 + if (rowsAffected > 0) + { + hlogi( "New message inserted successfully." ); + } + else + { + hloge( "No rows affected during insert operation." ); + } +} + +bool OpDatabase::queryUser(const std::string& user_id, const std::string& passwd_md5, std::string& jsonResult) +{ + try + { + // 数据库连接配置 + std::string server = "tcp://127.0.0.1:3306"; + std::string dbuser = "root"; + std::string password = "Hj57471000"; + std::string database = "hjems"; + + // 创建连接 + sql::mysql::MySQL_Driver* driver; + driver = sql::mysql::get_mysql_driver_instance(); + //m_pDbConnection.reset(driver->connect(server, dbuser, password)); + sql::Connection* pDbConnection = driver->connect(server, dbuser, password); + + if (!pDbConnection) + { + hloge("Failed to connect to database."); + return false; + } + + // 设置为使用指定数据库 + pDbConnection->setSchema(database); + hloge("%s : %s", user_id.c_str(), passwd_md5.c_str()); + + // 准备SQL查询语句 + std::string sql = "SELECT uid,uname, upasswd,usalt,email,mobile1,mobile2,memo FROM tbl_user WHERE uid = ?"; + + // 创建预编译的prepared statement + std::unique_ptr pstmt(pDbConnection->prepareStatement(sql)); + + // 绑定参数 + pstmt->setString(1, user_id); // 替换为你要查询的用户名 + + // 执行查询 + std::unique_ptr res(pstmt->executeQuery()); + + bool ret = false; + OpenJson json; + auto& nodeRoot = json["user"]; + + int i = 0; + + // 处理结果集 + while (res->next()) + { + auto& node = nodeRoot[i++]; + node["valid"] = 0; + node["uid"] = res->getString("uid"); + + // 假设username和password都是字符串类型 + std::string pass = res->getString("upasswd"); + std::string salt = res->getString("usalt"); + + //计算passwd和salt之间的关系 + //!passwd=md5(passwd_md5+salt+salt) + std::string tmp = passwd_md5 + salt + salt; + std::string smd5 = CalculateMD5(tmp); + hloge("upass=[%s],cakc=[%s],src=[%s]", pass.c_str(),smd5.c_str(), tmp.c_str()); + + if (pass == smd5) + { + node["valid"] = 1; + + node["uname"] = res->getString("uname"); + node["email"] = res->getString("email"); + node["mobile1"] = res->getString("mobile1"); + node["mobile2"] = res->getString("mobile2"); + node["memo"] = res->getString("memo"); + + ret = true; + break; + } + } + + jsonResult = json.encode(); + + pDbConnection->close(); + //delete pDbConnection; + + return ret; + } + catch (sql::SQLException& e) + { + std::ostringstream ss; + ss << "SQLException: " << e.what(); + ss << " (MySQL error code: " << e.getErrorCode(); + ss << ", SQLState: " << e.getSQLState() << " )"; + hloge("Failed to connect to database: %s", ss.str().c_str()); + return false; + } +} \ No newline at end of file diff --git a/applications/WebConfigure/cgiCommon/opmysql.h b/applications/WebConfigure/cgiCommon/opmysql.h new file mode 100644 index 0000000..38619a8 --- /dev/null +++ b/applications/WebConfigure/cgiCommon/opmysql.h @@ -0,0 +1,31 @@ +#ifndef __MY_OPERATE_MYSQL__ +#define __MY_OPERATE_MYSQL__ + +#include +#include + + +class OpDatabase +{ +protected: + OpDatabase(); +public: + ~OpDatabase(); + static OpDatabase* getInstance(); + + //计算字符串的MD5值 + static std::string CalculateMD5(const std::string& data); + + void CloseDatabase(); + bool OpenDatabase(const std::string& server = "tcp://127.0.0.1:3306", const std::string& dbuser = "root", const std::string& database="hjems"); + void InsertMessage(const std::string& ts, const std::string& msg_type, const std::string& fsu, const std::string& content, int topic, int dev_id); + + bool queryUser(const std::string& user_id, const std::string& passwd_md5, std::string& jsonResult); +protected: + //std::unique_ptr m_pDbConnection; + sql::Connection* m_pDbConnection; +private: + static OpDatabase m_instance; +}; + +#endif