ETNoticeManage.cpp 2.66 KB
#include "stdafx.h"
#include "ETNoticeManage.h"
#include "etradeclient/utility/logon_mgr.h"

#include "etradeclient/utility/win_http.h"
#include "etradeclient/utility/logging.h"
#include "etradeclient/utility/url_config.h"
#include "etradeclient/utility/application_config.h"
#include "etradeclient/utility/string_converter.h"
#include "etradeclient/utility/session.h"

#include <boost/property_tree/ptree.hpp>

#include "etradeclient/boost_patch/property_tree/json_parser.hpp" // WARNIING! Make sure to include our patched version.
#include <sstream>


CETNoticeManage::CETNoticeManage()
{
}


CETNoticeManage::~CETNoticeManage()
{
}

CString CETNoticeManage::GetNotice()
{
	CString sNoticeText;
	CString sLog;

	LOG_TRACE(_T("开始获取通知消息!"));
	const uint32_t kHTTPOK = 200;
	//m_err_msg = L"";

	auto& cURLConfig = URLConfig::Instance();
	WinHttp cWinHttp;
	cWinHttp.ConnectHost(cURLConfig.Host(), cURLConfig.Port(), cURLConfig.IsHttps());
	auto& cRequest = cWinHttp.OpenRequest(WinHttp::Method::POST, cURLConfig.NoticeMsgPath());
	cRequest.SetContentType("Content-Type:application/json;charset=UTF-8");
	//std::string post_data("{\"account\":\"" + wstr_2_str(account) + "\"," + "\"password\":\"" + wstr_2_str(pwd) + "\"," + "\"type\":\"" + type + "\"}");
	//request.SetPostData(post_data);
	if (cURLConfig.IsHttps())
	{
		auto& cAppConfig = ApplicationConfig::Instance();
		cRequest.SetClientCertificate(cAppConfig.ClientCertStore(), cAppConfig.ClientCertSubject());
	}
	cRequest.Send();
	uint32_t iStatusCode = cRequest.GetResponseStatus();
	if (kHTTPOK != iStatusCode)
	{
		sLog.Format(_T("网络请求错误! 错误码: %s"), std::to_wstring(iStatusCode));
		LOG_ERROR(sLog.GetBuffer());
	}
	std::string sResponseBody = cRequest.ReadResponseBody();
	if (sResponseBody.empty())
	{
		LOG_ERROR(L"获取服务器响应数据失败,请确保网络连接正常!");
	}
	sNoticeText = GetNoticeText(sResponseBody);
	
	return sNoticeText;
}

CString CETNoticeManage::GetNoticeText(std::string sResponse)
{
	CString sNoticeText;

	namespace PT = boost::property_tree;
	try 
	{
		PT::ptree pNoticeTree;
		std::stringstream ssNotice;
		ssNotice << sResponse;
		PT::read_json(ssNotice, pNoticeTree);

		PT::ptree pDataTree = pNoticeTree.get_child("data");
		CString sID;
		int iID = 1;
		for (auto index = pDataTree.begin(); index != pDataTree.end(); ++index)
		{
			sID.Format(_T("%d"), iID);
			std::wstring sContent = str_2_wstr(index->second.get<std::string>("content"));
			sNoticeText += sID + CString(_T(".")) + CString(sContent.c_str()) + CString(_T(" "));
			++iID;
		}
	}
	catch (...)
	{
		LOG_ERROR(L"解析服务器返回的登录结果信息时出错!请确认返回数据不为空,返回的数据格式为正确的Json格式!");
		return false;
	}
	return sNoticeText;
}