HwangKC 2024-06-05 20:52:33 +08:00
parent 92e2c228ce
commit 8520effc3a
8 changed files with 333 additions and 45 deletions

View File

@ -13,13 +13,20 @@ TARGET := commonAction
#compile and lib parameter #compile and lib parameter
#编译参数 #编译参数
CC := g++ CC := g++
CXX := g++ CXX := g++
LIBS := -L/usr/local/lib -lhv -lsqlite3 -lpthread -ldl -static -lcgicc LIBS := -L/usr/local/lib -lhv -lmysqlcppconn -lmysqlclient_r -lcgicc -lpthread -ldl
LDFLAGS := LDFLAGS :=
DEFINES := DEFINES :=
INCLUDE := -I. -I/usr/local/include INCLUDE := -I. -I/usr/local/include
CFLAGS := -g -Wall -O3 $(DEFINES) $(INCLUDE) -std=c++11 CFLAGS := -Wall -O3 $(DEFINES) $(INCLUDE) -std=c++11
CXXFLAGS:= $(CFLAGS) -DHAVE_CONFIG_H 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 #i think you should do anything here
#下面的基本上不需要做任何改动了 #下面的基本上不需要做任何改动了
@ -38,7 +45,7 @@ rebuild: veryclean everything
clean : clean :
rm -fr *.so *.a rm -fr *.so so
rm -fr *.o rm -fr *.o

View File

@ -8,19 +8,29 @@ OBJS := $(patsubst %.c,%.o,$(patsubst %.cpp,%.o,$(SOURCE)))
#target you can change test to what you want #target you can change test to what you want
#目标文件名,输入任意你想要的执行文件名 #目标文件名,输入任意你想要的执行文件名
TARGET := common.cgi TARGET := commonAction
#compile and lib parameter #compile and lib parameter
#编译参数 #编译参数
CC := arm-g++ CC := arm-g++
CXX := 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 := LDFLAGS :=
DEFINES := 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 CFLAGS := -Wall -O3 $(DEFINES) $(INCLUDE) -std=c++11
CXXFLAGS:= $(CFLAGS) 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 #i think you should do anything here
#下面的基本上不需要做任何改动了 #下面的基本上不需要做任何改动了
.PHONY : everything objs clean veryclean rebuild .PHONY : everything objs clean veryclean rebuild

View File

@ -5,8 +5,6 @@ VisualStudioVersion = 16.0.34301.259
MinimumVisualStudioVersion = 10.0.40219.1 MinimumVisualStudioVersion = 10.0.40219.1
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "cgiCommon", "cgiCommon.vcxproj", "{EC275930-0379-4487-8C30-EAFE2C124618}" Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "cgiCommon", "cgiCommon.vcxproj", "{EC275930-0379-4487-8C30-EAFE2C124618}"
EndProject 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}" Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "jsondemo2", "..\..\examples\jsondemo2\jsondemo2.vcxproj", "{A7EA55C7-9DC1-4903-9470-66F3BF247D28}"
EndProject EndProject
Global Global
@ -45,30 +43,6 @@ Global
{EC275930-0379-4487-8C30-EAFE2C124618}.Release|x86.ActiveCfg = Release|x86 {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.Build.0 = Release|x86
{EC275930-0379-4487-8C30-EAFE2C124618}.Release|x86.Deploy.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|ARM.ActiveCfg = Debug|Win32
{A7EA55C7-9DC1-4903-9470-66F3BF247D28}.Debug|ARM64.ActiveCfg = Debug|Win32 {A7EA55C7-9DC1-4903-9470-66F3BF247D28}.Debug|ARM64.ActiveCfg = Debug|Win32
{A7EA55C7-9DC1-4903-9470-66F3BF247D28}.Debug|x64.ActiveCfg = Debug|x64 {A7EA55C7-9DC1-4903-9470-66F3BF247D28}.Debug|x64.ActiveCfg = Debug|x64

View File

@ -89,6 +89,8 @@
<ClCompile Include="handleLoginRequest.cpp" /> <ClCompile Include="handleLoginRequest.cpp" />
<ClCompile Include="handleSignupRequest.cpp" /> <ClCompile Include="handleSignupRequest.cpp" />
<ClCompile Include="main.cpp" /> <ClCompile Include="main.cpp" />
<ClCompile Include="openjson.cpp" />
<ClCompile Include="opmysql.cpp" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<None Include="Makefile" /> <None Include="Makefile" />
@ -96,6 +98,8 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="handleHeader.h" /> <ClInclude Include="handleHeader.h" />
<ClInclude Include="openjson.h" />
<ClInclude Include="opmysql.h" />
</ItemGroup> </ItemGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'"> <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<ClCompile> <ClCompile>

View File

@ -6,8 +6,9 @@
#include <cgicc/HTTPHTMLHeader.h> #include <cgicc/HTTPHTMLHeader.h>
#include <cgicc/HTMLClasses.h> #include <cgicc/HTMLClasses.h>
#include <opdatabase.h>
#include "handleHeader.h" #include "handleHeader.h"
#include "openjson.h"
#include "opmysql.h"
using namespace cgicc; using namespace cgicc;
@ -59,18 +60,34 @@ void handleLoginRequest(cgicc::Cgicc& cgi)
std::cout << "<br/>\n"; std::cout << "<br/>\n";
// 뇹잿되쩌쭉서... // 뇹잿되쩌쭉서...
//´ÓÊý¾Ý¿âÖвéѯ
bool dbok = OpDatabase::getInstance()->OpenDatabase("127.0.0.1", "root");
std::string jsonResult;
if (!dbok)
{
std::cout << "<br/>\n";
std::cout << ("failed to retrieve user info") << std::endl;
}
else
{
if (!OpDatabase::getInstance()->queryUser(username, passwd, jsonResult))
{
std::cout << "<br/>\n";
std::cout << ("invalid user") << std::endl;
}
else
{
std::cout << "<br/>\n";
std::cout << h1("Login Successful!") << std::endl;
}
}
std::cout << "<br/>\n"; std::cout << "<br/>\n";
std::cout << username.c_str() << std::endl; std::cout << jsonResult << std::endl;
std::cout << "<br/>\n";
std::cout << passwd.c_str() << std::endl;
std::cout << "<br/>\n";
std::cout << h1("Login Successful!") << std::endl;
// Close the document // Close the document
std::cout << body() << html(); std::cout << body() << html();
#endif #endif
} }

View File

@ -5,6 +5,9 @@
#include "handleHeader.h" #include "handleHeader.h"
#include "openjson.h"
#include "opmysql.h"
// 你的CGI处理函数声明 // 你的CGI处理函数声明
void handleLoginRequest(cgicc::Cgicc& cgi); void handleLoginRequest(cgicc::Cgicc& cgi);
void handleSignupRequest(cgicc::Cgicc& cgi); void handleSignupRequest(cgicc::Cgicc& cgi);
@ -28,22 +31,52 @@ void handleVersion(cgicc::Cgicc& cgi)
std::cout << body() << std::endl; std::cout << body() << std::endl;
//std::cout << h1("Version 1.0.531") << std::endl;
#define cgiVersion "1.0.531" #define cgiVersion "1.0.531"
const char* VersionInfo = "Version: " cgiVersion; const char* VersionInfo = "Version: " cgiVersion;
const char* BuildInfo = "Build Info: " __DATE__ " " __TIME__; const char* BuildInfo = "Build Info: " __DATE__ " " __TIME__;
std::cout << h1(VersionInfo) << std::endl; std::cout << h1(VersionInfo) << std::endl;
std::cout << h1(BuildInfo) << std::endl; std::cout << h2(BuildInfo) << std::endl;
// Close the document // Close the document
std::cout << body() << html(); 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() int main()
{ {
#if 0
handleTest();
return 1;
#endif
using namespace cgicc; using namespace cgicc;
using namespace std; using namespace std;

View File

@ -0,0 +1,212 @@
#include "opmysql.h"
#include <algorithm>
#include <mysql_driver.h>
#include <mysql_connection.h>
#include <cppconn/prepared_statement.h>
#include <cppconn/resultset.h>
#include <cppconn/statement.h>
#include <cppconn/exception.h>
#include <hv/hlog.h>
#include <hv/md5.h>
#include <sstream>
#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<sql::PreparedStatement> 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<sql::PreparedStatement> pstmt(pDbConnection->prepareStatement(sql));
// 绑定参数
pstmt->setString(1, user_id); // 替换为你要查询的用户名
// 执行查询
std::unique_ptr<sql::ResultSet> 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;
}
}

View File

@ -0,0 +1,31 @@
#ifndef __MY_OPERATE_MYSQL__
#define __MY_OPERATE_MYSQL__
#include <string>
#include <mysql_connection.h>
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<sql::Connection> m_pDbConnection;
sql::Connection* m_pDbConnection;
private:
static OpDatabase m_instance;
};
#endif