Browse Source

完成json解析

zhangyang 6 years ago
parent
commit
1bc687b00a

BIN
bin/Win32/Debug/zhipuzi_pos_windows/zhipuzi_pos_windows.exe


+ 50 - 0
lewaimai_dispatch/CLoginWnd.cpp

@@ -1,5 +1,55 @@
 #include "pch/pch.h"
+#include "CLoginWnd.h"
 
+void CLoginFrameWnd::HandleLogin()
+{
+	//判断账号密码是否正确
+	std::wstring account, password;
+	CEditUI* pAccountEdit = static_cast<CEditUI*>(m_pm.FindControl(_T("accountedit")));
+	if (pAccountEdit)
+	{
+		account = pAccountEdit->GetText().GetData();
+	}
+
+	CEditUI* pPasswordEdit = static_cast<CEditUI*>(m_pm.FindControl(_T("pwdedit")));
+	if (pPasswordEdit)
+	{
+		password = pPasswordEdit->GetText().GetData();
+	}
+
+	LOG_INFO("account:" << account.c_str() << ", password:" << password.c_str());
+
+	rapidjson::Document doc;
+	doc.SetObject();
+	rapidjson::Document::AllocatorType &allocator = doc.GetAllocator(); //获取分配器
+
+	string s_account = CLewaimaiString::wstring2string(account);
+	doc.AddMember(rapidjson::StringRef("useranme"), rapidjson::StringRef(s_account.c_str()), allocator);
+
+	string s_password = CLewaimaiString::wstring2string(password);
+	doc.AddMember(rapidjson::StringRef("password"), rapidjson::StringRef(s_password.c_str()), allocator);
+
+	rapidjson::StringBuffer buffer;
+	rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
+	doc.Accept(writer);
+	std::string json = std::string(buffer.GetString());
+
+	LOG_INFO("json:" << json.c_str());
+
+	CGameFrameWnd* pFrame = new CGameFrameWnd();
+	if (pFrame == NULL)
+	{
+		return;
+	}
+
+	pFrame->SetIcon(IDI_ICON_DUILIB);
+	pFrame->Create(NULL, _T("游戏中心"), UI_WNDSTYLE_FRAME, 0L, 0, 0, 1024, 738);
+	pFrame->CenterWindow();
+
+	::ShowWindow(*pFrame, SW_SHOWMAXIMIZED);
+
+	Close();
+}
 
 
 

+ 8 - 16
lewaimai_dispatch/CLoginWnd.h

@@ -3,6 +3,13 @@
 #include "pch/pch.h"
 #include "CGameFrameWnd.h"
 
+#include "rapidjson/document.h"
+#include "rapidjson/prettywriter.h"  
+#include "rapidjson/writer.h"
+#include "rapidjson/stringbuffer.h"
+
+using namespace rapidjson;
+
 class CLoginFrameWnd : public CWindowWnd, public INotifyUI, public IMessageFilterUI
 {
 public:
@@ -178,22 +185,7 @@ public:
 		return false;
 	}
 
-	void HandleLogin()
-	{
-		CGameFrameWnd* pFrame = new CGameFrameWnd();
-		if (pFrame == NULL)
-		{
-			return;
-		}
-
-		pFrame->SetIcon(IDI_ICON_DUILIB);
-		pFrame->Create(NULL, _T("游戏中心"), UI_WNDSTYLE_FRAME, 0L, 0, 0, 1024, 738);
-		pFrame->CenterWindow();
-
-		::ShowWindow(*pFrame, SW_SHOWMAXIMIZED);
-
-		Close();
-	}
+	void HandleLogin();
 
 public:
 	CPaintManagerUI m_pm;

+ 181 - 3
lewaimai_dispatch/helper/CHttpClient.cpp

@@ -1,12 +1,190 @@
-#include "CHttpClient.h"
+#include "../pch/pch.h"
+#include "CHttpClient.h"
 
+#include <curl/curl.h>
 
+CHttpClient::CHttpClient(void) :
+	m_bDebug(false)
+{
+
+}
+
+CHttpClient::~CHttpClient(void)
+{
+
+}
+
+static int OnDebug(CURL *, curl_infotype itype, char * pData, size_t size, void *)
+{
+	if (itype == CURLINFO_TEXT)
+	{
+		//printf("[TEXT]%s\n", pData);
+	}
+	else if (itype == CURLINFO_HEADER_IN)
+	{
+		printf("[HEADER_IN]%s\n", pData);
+	}
+	else if (itype == CURLINFO_HEADER_OUT)
+	{
+		printf("[HEADER_OUT]%s\n", pData);
+	}
+	else if (itype == CURLINFO_DATA_IN)
+	{
+		printf("[DATA_IN]%s\n", pData);
+	}
+	else if (itype == CURLINFO_DATA_OUT)
+	{
+		printf("[DATA_OUT]%s\n", pData);
+	}
+	return 0;
+}
+
+static size_t OnWriteData(void* buffer, size_t size, size_t nmemb, void* lpVoid)
+{
+	std::string* str = dynamic_cast<std::string*>((std::string *)lpVoid);
+	if (NULL == str || NULL == buffer)
+	{
+		return -1;
+	}
+
+	char* pData = (char*)buffer;
+	str->append(pData, size * nmemb);
+	return nmemb;
+}
+
+int CHttpClient::Post(const std::string & strUrl, const std::string & strPost, std::string & strResponse)
+{
+	CURLcode res;
+	CURL* curl = curl_easy_init();
+	if (NULL == curl)
+	{
+		return CURLE_FAILED_INIT;
+	}
+	if (m_bDebug)
+	{
+		curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
+		curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION, OnDebug);
+	}
+	curl_easy_setopt(curl, CURLOPT_URL, strUrl.c_str());
+	curl_easy_setopt(curl, CURLOPT_POST, 1);
+	curl_easy_setopt(curl, CURLOPT_POSTFIELDS, strPost.c_str());
+	curl_easy_setopt(curl, CURLOPT_READFUNCTION, NULL);
+	curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, OnWriteData);
+	curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&strResponse);
+	curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
+	curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 8);//连接超时,这个数值如果设置太短可能导致数据请求不到就断开了
+	curl_easy_setopt(curl, CURLOPT_TIMEOUT, 10);//接收数据时超时设置,如果10秒内数据未接收完,直接退出
+	res = curl_easy_perform(curl);
+	curl_easy_cleanup(curl);
+	return res;
+}
+
+int CHttpClient::Get(const std::string & strUrl, std::string & strResponse)
+{
+	CURLcode res;
+	CURL* curl = curl_easy_init();
+	if (NULL == curl)
+	{
+		return CURLE_FAILED_INIT;
+	}
+	if (m_bDebug)
+	{
+		curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
+		curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION, OnDebug);
+	}
+	curl_easy_setopt(curl, CURLOPT_URL, strUrl.c_str());
+	curl_easy_setopt(curl, CURLOPT_READFUNCTION, NULL);
+	curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, OnWriteData);
+	curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&strResponse);
+	/**
+	* 当多个线程都使用超时处理的时候,同时主线程中有sleep或是wait等操作。
+	* 如果不设置这个选项,libcurl将会发信号打断这个wait从而导致程序退出。
+	*/
+	curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
+	curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 8);//连接超时,这个数值如果设置太短可能导致数据请求不到就断开了
+	curl_easy_setopt(curl, CURLOPT_TIMEOUT, 10);//接收数据时超时设置,如果10秒内数据未接收完,直接退出
+	res = curl_easy_perform(curl);
+	curl_easy_cleanup(curl);
+	return res;
+}
+
+int CHttpClient::Posts(const std::string & strUrl, const std::string & strPost, std::string & strResponse, const char * pCaPath)
+{
+	CURLcode res;
+	CURL* curl = curl_easy_init();
+	if (NULL == curl)
+	{
+		return CURLE_FAILED_INIT;
+	}
+	if (m_bDebug)
+	{
+		curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
+		curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION, OnDebug);
+	}
+	curl_easy_setopt(curl, CURLOPT_URL, strUrl.c_str());
+	curl_easy_setopt(curl, CURLOPT_POST, 1);
+	curl_easy_setopt(curl, CURLOPT_POSTFIELDS, strPost.c_str());
+	curl_easy_setopt(curl, CURLOPT_READFUNCTION, NULL);
+	curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, OnWriteData);
+	curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&strResponse);
+	curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
+	if (NULL == pCaPath)
+	{
+		curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, false);
+		curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, false);
+	}
+	else
+	{
+		//缺省情况就是PEM,所以无需设置,另外支持DER
+		//curl_easy_setopt(curl,CURLOPT_SSLCERTTYPE,"PEM");
+		curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, true);
+		curl_easy_setopt(curl, CURLOPT_CAINFO, pCaPath);
+	}
+	curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 8);//连接超时,这个数值如果设置太短可能导致数据请求不到就断开了
+	curl_easy_setopt(curl, CURLOPT_TIMEOUT, 10);//接收数据时超时设置,如果10秒内数据未接收完,直接退出
+	res = curl_easy_perform(curl);
+	curl_easy_cleanup(curl);
+	return res;
+}
 
-CHttpClient::CHttpClient()
+int CHttpClient::Gets(const std::string & strUrl, std::string & strResponse, const char * pCaPath)
 {
+	CURLcode res;
+	CURL* curl = curl_easy_init();
+	if (NULL == curl)
+	{
+		return CURLE_FAILED_INIT;
+	}
+	if (m_bDebug)
+	{
+		curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
+		curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION, OnDebug);
+	}
+	curl_easy_setopt(curl, CURLOPT_URL, strUrl.c_str());
+	curl_easy_setopt(curl, CURLOPT_READFUNCTION, NULL);
+	curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, OnWriteData);
+	curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&strResponse);
+	curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
+	if (NULL == pCaPath)
+	{
+		curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, false);
+		curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, false);
+	}
+	else
+	{
+		curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, true);
+		curl_easy_setopt(curl, CURLOPT_CAINFO, pCaPath);
+	}
+	curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 8);//连接超时,这个数值如果设置太短可能导致数据请求不到就断开了
+	curl_easy_setopt(curl, CURLOPT_TIMEOUT, 10);//接收数据时超时设置,如果10秒内数据未接收完,直接退出
+	res = curl_easy_perform(curl);
+	curl_easy_cleanup(curl);
+	return res;
 }
 
+///////////////////////////////////////////////////////////////////////////////////////////////
 
-CHttpClient::~CHttpClient()
+void CHttpClient::SetDebug(bool bDebug)
 {
+	m_bDebug = bDebug;
 }

+ 1 - 0
lewaimai_dispatch/helper/CHttpClient.h

@@ -1,5 +1,6 @@
 #pragma once
 
+using namespace std;
 
 class CHttpClient
 {

+ 43 - 0
lewaimai_dispatch/helper/CLewaimaiString.h

@@ -2,6 +2,8 @@
 
 using namespace std;
 
+#include <Windows.h>
+
 class CLewaimaiString
 {
 public:
@@ -12,5 +14,46 @@ public:
 	static bool base64_decode(const string& input, string* output);
 
 	static void trim(string &s);
+
+	//将string转换成wstring  
+	static wstring string2wstring(string str)
+	{
+		wstring result;
+
+		//获取缓冲区大小,并申请空间,缓冲区大小按字符计算  
+		int len = MultiByteToWideChar(CP_ACP, 0, str.c_str(), str.size(), NULL, 0);
+		TCHAR* buffer = new TCHAR[len + 1];
+
+		//多字节编码转换成宽字节编码  
+		MultiByteToWideChar(CP_ACP, 0, str.c_str(), str.size(), buffer, len);
+
+		//添加字符串结尾  
+		buffer[len] = '\0';
+
+		//删除缓冲区并返回值  
+		result.append(buffer);
+
+		delete[] buffer;
+		return result;
+	}
+
+	//将wstring转换成string  
+	static string wstring2string(wstring wstr)
+	{
+		string result;
+		//获取缓冲区大小,并申请空间,缓冲区大小事按字节计算的  
+		int len = WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), wstr.size(), NULL, 0, NULL, NULL);
+		char* buffer = new char[len + 1];
+
+		//宽字节编码转换成多字节编码  
+		WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), wstr.size(), buffer, len, NULL, NULL);
+		buffer[len] = '\0';
+
+		//删除缓冲区并返回值  
+		result.append(buffer);
+
+		delete[] buffer;
+		return result;
+	}
 };
 

+ 7 - 0
lewaimai_dispatch/lewaimai_dispatch_windows.cpp

@@ -11,6 +11,13 @@ int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
 	_In_ LPWSTR    lpCmdLine,
 	_In_ int       nCmdShow)
 {
+	//初始化日志
+	CLewaimaiLog log;
+	log.Init();
+
+	//读取配置文件
+	CConfigReader::ReadConfigFile();
+
 	CPaintManagerUI::SetInstance(hInstance);
 #if 0
 	CPaintManagerUI::SetResourcePath(CPaintManagerUI::GetInstancePath() + _T("skin"));