#include "NetworkApi.h" #include #include #include "md5.h" #include "SocketManager.h" string NetworkApi::_TRADE_SERVER_ADDR ="43.138.167.68"; int NetworkApi::_TRADE_SERVER_PORT = 11008; string NetworkApi::_COMMON_SERVER_ADDR; int NetworkApi::_COMMON_SERVER_PORT; string NetworkApi::get_rquest_id() { string requestIdStr; std::chrono::system_clock::time_point now = std::chrono::system_clock::now(); // ½«µ±Ç°Ê±¼äµãת»»Îªtime_tÀàÐÍ std::time_t currentTime = std::chrono::system_clock::to_time_t(now); requestIdStr.append("_").append(std::to_string(currentTime)).append("_"); // Éú³ÉËæ»úÊý std::random_device rd; std::mt19937 rng(rd()); std::uniform_int_distribution intDist(1, 100000); // Éú³É·¶Î§ÔÚ1-100Ö®¼äµÄÕûÊý int randomInt = intDist(rng); requestIdStr.append(std::to_string(randomInt)); return requestIdStr; } string NetworkApi::get_sign(rapidjson::Document& document) { //¼ÆËãÇ©Ãû std::list strList; for (rapidjson::Value::ConstMemberIterator itr = document.MemberBegin(); itr != document.MemberEnd(); ++itr) { if (itr->value.IsObject() || itr->value.IsArray()) { //Èç¹ûÊǶÔÏó rapidjson::StringBuffer buffer; rapidjson::Writer writer(buffer); itr->value.Accept(writer); std::string jsonString = buffer.GetString(); strList.push_back(string(itr->name.GetString()).append("=").append(jsonString)); } else { if (itr->value.IsString()) { strList.push_back(string(itr->name.GetString()).append("=").append(itr->value.GetString())); } else if (itr->value.IsInt()) { strList.push_back(string(itr->name.GetString()).append("=").append(std::to_string(itr->value.GetInt()))); } } } strList.sort(); strList.push_back("%Yeshi2014@#."); string src = ""; int index = 0; for (std::list::iterator el = strList.begin(); el != strList.end(); el++) { src.append(*el); index++; if (index != strList.size()) { src.append("&"); } } //MD5 //cout << "¼ÓÃÜǰ×Ö·û´®£º" < writer(buffer); document.Accept(writer); std::string jsonString = buffer.GetString(); //·¢ËÍÊý¾Ý std::stringstream ss; ss << std::setw(8) << std::setfill('0') << jsonString.length(); std::string length_str = string("##").append(ss.str()); std::string fstr = length_str.append(jsonString); return fstr; } //ÍøÂçÇëÇó std::string NetworkApi::base_request(std::string data) { std::string fstr = load_request_data(data); try { return SocketManager::sendMsg(_COMMON_SERVER_ADDR, _COMMON_SERVER_PORT, fstr.c_str()); } catch (...) { throw L"ÍøÂçÇëÇó³ö´í"; } } std::string NetworkApi::base_trade_request(std::string data) { rapidjson::Document document; document.Parse(data.c_str()); string requestIdStr = document["type"].IsInt()? to_string(document["type"].GetInt()) : string(document["type"].GetString()).append(get_rquest_id()); rapidjson::Document::AllocatorType& allocator = document.GetAllocator(); // Ìí¼Ó request_id ×Ö¶Î rapidjson::Value requestId(rapidjson::kStringType); requestId.SetString(requestIdStr.c_str(), allocator); document.AddMember("request_id", requestId, allocator); // Ìí¼ÓÇ©Ãû£¬sign string sign = get_sign(document); rapidjson::Value signV(rapidjson::kStringType); signV.SetString(sign.c_str(), allocator); document.AddMember("sign", signV, allocator); rapidjson::StringBuffer buffer; rapidjson::Writer writer(buffer); document.Accept(writer); std::string jsonString = buffer.GetString(); //·¢ËÍÊý¾Ý std::stringstream ss; ss << std::setw(8) << std::setfill('0') << jsonString.length(); std::string length_str = string("##").append(ss.str()); std::string fstr = length_str.append(jsonString); try { return SocketManager::sendMsg(_TRADE_SERVER_ADDR, _TRADE_SERVER_PORT, fstr.c_str()); } catch (...) { throw wstring(L"ÍøÂçÇëÇó³ö´í"); } }