#include "common/pch.h" #include #include #include #include"StringUtil.h" #include"LowSuctionNetworkApi.h" #include "JsonUtil.h" #include #include #include #include "md5.h" #include #include #include #include string LowSuctionNetworkApi::_TRADE_SERVER_ADDR = string("43.138.167.68"); int LowSuctionNetworkApi::_TRADE_SERVER_PORT = 14008; string LowSuctionNetworkApi::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 LowSuctionNetworkApi::get_sign(rapidjson::Document& document) { //¼ÆËãÇ©Ãû std::list strList; for (rapidjson::Value::ConstMemberIterator itr = document.MemberBegin(); itr != document.MemberEnd(); ++itr) { std::cout << "¼ü: " << itr->name.GetString() << ", Öµ: "; 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 LowSuctionNetworkApi::base_trade_request(std::string data) { rapidjson::Document document; document.Parse(data.c_str()); string requestIdStr = 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"ÍøÂçÇëÇó³ö´í"); } } string LowSuctionNetworkApi::test_cancel_order(string code) { rapidjson::StringBuffer buf; rapidjson::PrettyWriter writer(buf); writer.StartObject(); writer.Key("type"); writer.String("common"); writer.Key("data"); writer.StartObject(); writer.Key("ctype"); writer.String("test_cancel_order"); writer.Key("code"); writer.String(code.c_str()); writer.EndObject(); writer.EndObject(); const char* json_content = buf.GetString(); string result = base_trade_request(std::string(json_content)); return result; } string LowSuctionNetworkApi::get_env(bool need_history_data) { rapidjson::StringBuffer buf; rapidjson::PrettyWriter writer(buf); writer.StartObject(); writer.Key("type"); writer.String("common"); writer.Key("data"); writer.StartObject(); writer.Key("ctype"); writer.String("get_env"); if (need_history_data) { writer.Key("history"); writer.Int(1); } writer.EndObject(); writer.EndObject(); const char* json_content = buf.GetString(); string result = base_trade_request(std::string(json_content)); return result; } string LowSuctionNetworkApi::update_leading_limit_up_datas() { rapidjson::StringBuffer buf; rapidjson::PrettyWriter writer(buf); writer.StartObject(); writer.Key("type"); writer.String("common"); writer.Key("data"); writer.StartObject(); writer.Key("ctype"); writer.String("update_leading_limit_up_datas"); writer.EndObject(); writer.EndObject(); const char* json_content = buf.GetString(); string result = base_trade_request(std::string(json_content)); return result; } string LowSuctionNetworkApi::update_code_jx_plates() { rapidjson::StringBuffer buf; rapidjson::PrettyWriter writer(buf); writer.StartObject(); writer.Key("type"); writer.String("common"); writer.Key("data"); writer.StartObject(); writer.Key("ctype"); writer.String("update_kpl_code_jx_blocks_datas"); writer.EndObject(); writer.EndObject(); const char* json_content = buf.GetString(); string result = base_trade_request(std::string(json_content)); return result; } string LowSuctionNetworkApi::init_datas() { rapidjson::StringBuffer buf; rapidjson::PrettyWriter writer(buf); writer.StartObject(); writer.Key("type"); writer.String("common"); writer.Key("data"); writer.StartObject(); writer.Key("ctype"); writer.String("init_datas"); writer.EndObject(); writer.EndObject(); const char* json_content = buf.GetString(); string result = base_trade_request(std::string(json_content)); return result; } string LowSuctionNetworkApi::get_place_order_records() { rapidjson::StringBuffer buf; rapidjson::PrettyWriter writer(buf); writer.StartObject(); writer.Key("type"); writer.String("common"); writer.Key("data"); writer.StartObject(); writer.Key("ctype"); writer.String("get_place_order_records"); writer.EndObject(); writer.EndObject(); const char* json_content = buf.GetString(); string result = base_trade_request(std::string(json_content)); return result; } string LowSuctionNetworkApi::get_settings() { rapidjson::StringBuffer buf; rapidjson::PrettyWriter writer(buf); writer.StartObject(); writer.Key("type"); writer.String("common"); writer.Key("data"); writer.StartObject(); writer.Key("ctype"); writer.String("get_settings"); writer.EndObject(); writer.EndObject(); const char* json_content = buf.GetString(); string result = base_trade_request(std::string(json_content)); return result; } string LowSuctionNetworkApi::set_settings(int trade_state, int buy_money) { rapidjson::StringBuffer buf; rapidjson::PrettyWriter writer(buf); writer.StartObject(); writer.Key("type"); writer.String("common"); writer.Key("data"); writer.StartObject(); writer.Key("ctype"); writer.String("set_settings"); writer.Key("trade_state"); writer.Int(trade_state); writer.Key("buy_money"); writer.Int(buy_money); writer.EndObject(); writer.EndObject(); const char* json_content = buf.GetString(); string result = base_trade_request(std::string(json_content)); return result; } string LowSuctionNetworkApi::read_place_order_queue() { rapidjson::StringBuffer buf; rapidjson::PrettyWriter writer(buf); writer.StartObject(); writer.Key("type"); writer.String("common"); writer.Key("data"); writer.StartObject(); writer.Key("ctype"); writer.String("get_not_process_place_order_record"); writer.EndObject(); writer.EndObject(); const char* json_content = buf.GetString(); string result = base_trade_request(std::string(json_content)); return result; } string LowSuctionNetworkApi::add_black_list(string code) { rapidjson::StringBuffer buf; rapidjson::PrettyWriter writer(buf); writer.StartObject(); writer.Key("type"); writer.String("common"); writer.Key("data"); writer.StartObject(); writer.Key("ctype"); writer.String("add_black_list"); writer.Key("code"); writer.String(code.c_str()); writer.EndObject(); writer.EndObject(); const char* json_content = buf.GetString(); string result = base_trade_request(std::string(json_content)); return result; } string LowSuctionNetworkApi::set_buy(string id) { rapidjson::StringBuffer buf; rapidjson::PrettyWriter writer(buf); writer.StartObject(); writer.Key("type"); writer.String("common"); writer.Key("data"); writer.StartObject(); writer.Key("ctype"); writer.String("set_place_order_buy"); writer.Key("id"); writer.String(id.c_str()); writer.EndObject(); writer.EndObject(); const char* json_content = buf.GetString(); string result = base_trade_request(std::string(json_content)); return result; } string LowSuctionNetworkApi::set_not_buy(string id) { rapidjson::StringBuffer buf; rapidjson::PrettyWriter writer(buf); writer.StartObject(); writer.Key("type"); writer.String("common"); writer.Key("data"); writer.StartObject(); writer.Key("ctype"); writer.String("set_place_order_not_buy"); writer.Key("id"); writer.String(id.c_str()); writer.EndObject(); writer.EndObject(); const char* json_content = buf.GetString(); string result = base_trade_request(std::string(json_content)); return result; }