#include "pch.h" #include "HttpSocket.h" #include #include #include #include #include #include #include #include #include #include using namespace bsoncxx::builder::basic; using namespace chrono; mongocxx::instance g_instance{}; std::int64_t getstrfromstamp(std::string strtime) { __int64 igettime = 0; boost::replace_all(strtime, " ", "T"); boost::replace_all(strtime, "-", ""); boost::replace_all(strtime, ":", ""); strtime += "000"; boost::posix_time::ptime ssp = boost::posix_time::from_iso_string(strtime); boost::posix_time::time_duration time_from_epoch = boost::posix_time::microsec_clock::universal_time() - boost::posix_time::microsec_clock::local_time(); boost::posix_time::ptime epoch(boost::gregorian::date(1970, boost::gregorian::Jan, 1)); boost::posix_time::time_duration time_epoch = ssp - epoch; igettime = time_epoch.total_milliseconds() + time_from_epoch.total_milliseconds() / 360000 * 360000; return igettime; } HttpSocket::HttpSocket() { __int64 itime = funclib::gettimestamp(); m_random = default_random_engine(itime); m_pmsgservice = boost::make_shared(); m_pmsgwork = boost::make_shared(*m_pmsgservice); data_time = boost::make_shared(*m_pmsgservice); //数据库配置 string strip = funclib::getdatainfo(); //strip = "mongodb://datzahndle:pajdt873hyu3h37@110.40.64.236:37317"; //strip = "mongodb://110.40.59.112:37317"; //strip = "mongodb://192.168.0.220:27017"; m_puri = boost::make_shared(strip.c_str()); m_pclient = boost::make_shared (*m_puri); m_pdb = boost::make_shared(); m_platform = boost::make_shared(); m_gamelog = boost::make_shared(); m_pvipuserdb = boost::make_shared(); m_ptask = boost::make_shared(); m_viplog = boost::make_shared(); m_webdata = boost::make_shared(); m_pallindex = boost::make_shared(); m_pcoll = boost::make_shared(); m_plosewincoll = boost::make_shared(); m_pvipuser = boost::make_shared(); m_pplayuseronline = boost::make_shared(); m_userloginonline = boost::make_shared(); m_pusertradeinfo = boost::make_shared(); m_pviptradeinfo = boost::make_shared(); m_poperatinfo = boost::make_shared(); m_pgameviploginfo = boost::make_shared(); m_pgameserverloginfo = boost::make_shared(); m_pvipalllogcache = boost::make_shared(); m_pserverlogcache = boost::make_shared(); m_puserlogcache = boost::make_shared(); m_pgameuserloginfo = boost::make_shared(); m_pviplogcache = boost::make_shared(); m_pgameprizelog = boost::make_shared(); m_puserdowninfo = boost::make_shared(); m_pvipscore = boost::make_shared(); m_pvipscoredate = boost::make_shared(); m_pvipuseraccounts = boost::make_shared(); m_puserdatainfo = boost::make_shared(); m_pvipconfigure = boost::make_shared(); m_pusersub = boost::make_shared(); m_pexechangerate = boost::make_shared(); m_pthreeadmin = boost::make_shared(); m_pconfig = boost::make_shared(); m_pkinditem = boost::make_shared(); m_pipwhite = boost::make_shared(); m_papplyviplog = boost::make_shared(); m_pvipmsglog = boost::make_shared(); m_pgamelist = boost::make_shared(); m_pgamegameitem = boost::make_shared(); m_ppopularizesit = boost::make_shared(); m_activityfreegame = boost::make_shared(); m_pmarquee = boost::make_shared(); m_pcustomerlink = boost::make_shared(); m_pcredittopwinner = boost::make_shared(); m_resendorder = boost::make_shared(); m_pareagames = boost::make_shared(); m_pplayrecordini = boost::make_shared(); m_pgamebet = boost::make_shared(); m_pactivityini = boost::make_shared(); m_pplatformini = boost::make_shared(); m_pthirddeveloper = boost::make_shared(); m_pthirdgames = boost::make_shared(); m_pgameprizelog = boost::make_shared(); *m_pdb = (*m_pclient)["accounts"]; *m_platform = (*m_pclient)["platform"]; *m_gamelog = (*m_pclient)["gamelog"]; *m_ptask = (*m_pclient)["task"]; *m_viplog = (*m_pclient)["viplog"]; *m_webdata = (*m_pclient)["webdata"]; *m_pvipuserdb = (*m_pclient)["vipuser"]; *m_pallindex = (*m_pdb)["allindex"]; *m_pcoll = (*m_pdb)["userinfo"]; *m_plosewincoll = (*m_pdb)["losewin"]; *m_pvipuser = (*m_pvipuserdb)["user"]; *m_pplayuseronline = (*m_gamelog)["playuseronline"]; *m_userloginonline = (*m_gamelog)["userloginonline"]; *m_poperatinfo = (*m_gamelog)["operatinfo"]; *m_puserdowninfo = (*m_gamelog)["userdowninfo"]; *m_pusertradeinfo = (*m_pvipuserdb)["usertradeinfo"]; *m_pgameviploginfo = (*m_gamelog)["gameviploginfo"]; *m_pgameserverloginfo = (*m_gamelog)["gameserverloginfo"]; *m_pvipalllogcache = (*m_gamelog)["vipalllogcache"]; *m_pserverlogcache = (*m_gamelog)["serverlogcache"]; *m_puserlogcache = (*m_gamelog)["userlogcache"]; *m_pgameuserloginfo = (*m_gamelog)["gameuserloginfo"]; *m_pviplogcache = (*m_gamelog)["viplogcache"]; *m_pgameprizelog = (*m_gamelog)["gameprizelog"]; *m_resendorder = (*m_gamelog)["resendorder"]; *m_pviptradeinfo = (*m_pvipuserdb)["viptradeinfo"]; *m_pvipscore = (*m_pvipuserdb)["vipscore"]; *m_pvipscoredate = (*m_pvipuserdb)["vipscoredate"]; *m_pvipuseraccounts = (*m_pvipuserdb)["vipuseraccounts"]; *m_puserdatainfo = (*m_gamelog)["userdatainfo"]; *m_pvipconfigure = (*m_pvipuserdb)["vipconfigure"]; *m_pthreeadmin = (*m_pvipuserdb)["threeadmin"]; *m_pusersub = (*m_pvipuserdb)["usersub"]; *m_pexechangerate = (*m_platform)["exechangerate"]; *m_pkinditem = (*m_platform)["kinditem"]; *m_pgamelist = (*m_platform)["gamelist"]; *m_pgamegameitem = (*m_platform)["gamegameitem"]; *m_ppopularizesit = (*m_pvipuserdb)["popularizesit"]; // 推广网站 *m_pmarquee = (*m_pvipuserdb)["marquee"]; // 信用代理跑马灯 *m_pcustomerlink = (*m_pvipuserdb)["customerlink"]; *m_pcredittopwinner = (*m_gamelog)["credittopwinner"]; *m_pplatformini = (*m_platform)["platformini"]; //task *m_pconfig = (*m_ptask)["config"]; *m_pipwhite = (*m_pvipuserdb)["ipwhite"]; //viplog *m_papplyviplog = (*m_viplog)["applyviplog"]; *m_pvipmsglog = (*m_viplog)["vipmsglog"]; //freegame *m_activityfreegame = (*m_pvipuserdb)["freegameactivity"]; //areagame *m_pareagames = (*m_platform)["areagames"]; *m_pplayrecordini = (*m_platform)["playrecordini"]; // 游戏投注配置 *m_pgamebet = (*m_platform)["gamebet"]; // 游戏活动配置 *m_pactivityini = (*m_webdata)["activeini"]; // 第三方厂商 *m_pthirddeveloper = (*m_platform)["thirddeveloper"]; // 第三方厂商游戏 *m_pthirdgames = (*m_platform)["thirdgames"]; m_strshahead = "X-REQUEST-SIGN"; m_strshaheadcontent = "6e4c91001979851a97c2b5360f044ff67b48e186d6ecd4394532851bffdeeae9"; m_psendhttp = weblib::httpini(); m_brun = false; m_msgthread = thread([this] { m_ucurid = funclib::getcurid(); m_brun = true; m_pmsgservice->run(); m_brun = false; }); m_msgthread.detach(); while (!m_brun) { this_thread::yield(); } } HttpSocket::~HttpSocket() { } // 毫秒时间戳 → ISO-8601 UTC 时间字符串 string to_iso8601_utc_string(chrono::system_clock::time_point tp) { auto ms = duration_cast(tp.time_since_epoch()) % 1000; time_t t = system_clock::to_time_t(tp); tm tm = *gmtime(&t); ostringstream oss; oss << put_time(&tm, "%Y-%m-%d %H:%M:%S"); oss << '.' << setfill('0') << setw(3) << ms.count(); return oss.str(); } struct stUserInfo { __int64 ispreaderid = 0; __int32 iareaid = 0; __int64 iuserid = 0; }; static map map_acctouserinfo; static bool bAllPageDone = false; //这次请求是否请求完所有分页 void HttpSocket::init() { __int64 itimefrom = 0; __int64 itimeto = 0; __int32 ipage = 0; __int64 inowtime = 0; bool bFirst = true; loaddbsynctime(itimefrom, itimeto, ipage, bAllPageDone); while (true) { inowtime = funclib::gettimestamp(); if (itimeto == 0) { itimeto = inowtime - 1000; itimefrom = itimeto - 1000; } else if (inowtime - itimeto < 1000) { this_thread::sleep_for(milliseconds(itimeto + 1000 - inowtime)); } if (!bFirst && !bAllPageDone) continue; else ipage = 0; bAllPageDone = false; bFirst = false; itimefrom = itimeto; itimeto = funclib::gettimestamp(); getgamehistory(itimefrom, itimeto, ipage); } } //拉取日志 void HttpSocket::getgamehistory(__int64 itimefrom, __int64 itimeto, __int32 ifirstpage) { m_writelog("--------------------getgamehistory-------------------------"); __int32 ipage = ifirstpage; __int32 inextpage = 0; __int32 ipagesize = 500; rapidjson::Document doc; doc.SetObject(); rapidjson::Document::AllocatorType& allocator = doc.GetAllocator(); string datefrom = to_iso8601_utc_string(system_clock::time_point(seconds(itimefrom / 1000))); string dateto = to_iso8601_utc_string(system_clock::time_point(seconds(itimeto / 1000))); doc.AddMember("casino", rapidjson::Value("luckybet777", allocator), allocator); doc.AddMember("page", ipage, allocator); doc.AddMember("size", ipagesize, allocator); doc.AddMember("dateFrom", rapidjson::Value(datefrom.c_str(), allocator), allocator); doc.AddMember("dateTo", rapidjson::Value(dateto.c_str(), allocator), allocator); string strjson = funclib::doctojson(doc); string strtarget = "v2/tw/getGameHistory"; sendreelmsg("luckybet777", strtarget, strjson, move([this, ipage, ipagesize, itimefrom, itimeto](string strdata, int iret) { if (iret == 200) { rettype::type tret{ rettype::type::ini }; rapidjson::Document docdata; rapidjson::ParseResult ok = docdata.Parse(strdata.c_str()); string stronlyid = ""; string strgid = ""; string straccount = ""; string strroundData = ""; string strgameResultUrl = ""; string strcurrency = ""; string strdbquestid = ""; __int32 ibrandid = 2; __int64 iwinscore = 0; __int64 ibetscore = 0; double_t dwinmul = 0; if (ok) { static int itotalsize = 0; if (docdata.HasMember("total")) itotalsize = docdata["total"].GetInt(); rapidjson::Value arrbets(rapidjson::kArrayType); if (docdata.HasMember("records") && docdata["records"].IsArray()) { arrbets = docdata["records"].GetArray(); for (rapidjson::SizeType i = 0; i < arrbets.Size(); ++i) { const rapidjson::Value& recorditem = arrbets[i]; if (!recorditem.IsObject()) continue; if (!recorditem.HasMember("roundFinished") || !recorditem["roundFinished"].GetBool()) continue; if (!recorditem.HasMember("id")) continue; if(!recorditem.HasMember("launchAlias")) continue; if (!recorditem.HasMember("playerId")) continue; if (!recorditem.HasMember("totalWin")) continue; if (!recorditem.HasMember("totalBet")) continue; if (!recorditem.HasMember("roundDate")) continue; if (!recorditem.HasMember("gameResultUrl")) continue; if(!recorditem.HasMember("currency")) continue; stronlyid = recorditem["id"].GetString(); //auto findrecord = make_document(kvp("onlyid", stronlyid.c_str()), kvp("brandid", ibrandid)); //auto result = m_pgameprizelog->find_one(findrecord.view()); //if (result) // continue; // 重复 straccount = recorditem["playerId"].GetString(); stUserInfo stuserinfo = map_acctouserinfo[straccount]; if (!stuserinfo.iuserid) { auto finduser = m_pcoll->find_one(make_document(kvp("account", straccount.c_str()))); if (!finduser || !finduser->view()["userid"]) continue; stuserinfo.iuserid = finduser->view()["userid"].get_int64(); if(finduser->view()["spreaderid"]) stuserinfo.ispreaderid = finduser->view()["spreaderid"].get_int64(); if(finduser->view()["areaid"]) stuserinfo.iareaid = finduser->view()["areaid"].get_int32(); map_acctouserinfo[straccount] = stuserinfo; } iwinscore = static_cast<__int64>(std::stod(recorditem["totalWin"].GetString()) * 100); ibetscore = static_cast<__int64>(std::stod(recorditem["totalBet"].GetString()) * 100); if(ibetscore > 0) dwinmul = iwinscore / ibetscore; strgid = recorditem["launchAlias"].GetString(); strroundData = recorditem["roundDate"].GetString(); strgameResultUrl = recorditem["gameResultUrl"].GetString(); strcurrency = recorditem["currency"].GetString(); document recorddoc; //convertJsonToBson(recorddoc, recorditem); recorddoc.append(kvp("onlyid", stronlyid.c_str())); recorddoc.append(kvp("dbquestid", stronlyid.c_str())); recorddoc.append(kvp("brandid", ibrandid)); recorddoc.append(kvp("gid", strgid.c_str())); recorddoc.append(kvp("userid", stuserinfo.iuserid)); recorddoc.append(kvp("areaid", stuserinfo.iareaid)); recorddoc.append(kvp("spreaderid", stuserinfo.ispreaderid)); recorddoc.append(kvp("roundDate", strroundData.c_str())); recorddoc.append(kvp("gameResultUrl", strgameResultUrl.c_str())); recorddoc.append(kvp("winscore", iwinscore)); recorddoc.append(kvp("betscore", ibetscore)); recorddoc.append(kvp("dwinmul", dwinmul)); recorddoc.append(kvp("account", straccount.c_str())); recorddoc.append(kvp("currency", strcurrency.c_str())); recorddoc.append(kvp("scoretype", 1)); std::int64_t icreateime = getstrfromstamp(strroundData); recorddoc.append(kvp("inserttime", icreateime)); m_pgameprizelog->insert_one(recorddoc.view()); } } if((ipage + 1) * ipagesize >= itotalsize) bAllPageDone = true; setdbsynctime(itimefrom, itimeto, ipage + 1, bAllPageDone); if(!bAllPageDone) getgamehistory(itimefrom, itimeto, ipage + 1); } } })); } void HttpSocket::sendreelmsg(string stronly, string strtarget, string strjson, function&& dofun) { weblib::httpinfo thttpinfo; thttpinfo.strurl = "https://transfer-wallet-service.stage.iconic-21.com"; // thttpinfo.strurl = "http://127.0.0.1:6000"; thttpinfo.strtarget = strtarget; thttpinfo.stronly = stronly; string strshaheadcontent = m_strshaheadcontent + strjson; strshaheadcontent = sha256_to_base16(strshaheadcontent); thttpinfo.addheadinfo.insert(make_pair(m_strshahead, strshaheadcontent)); thttpinfo.strjson = strjson; thttpinfo.setresult_int.insert(404); stringstream sss; sss << "strurl=" << thttpinfo.strurl << " strtarget=" << strtarget << " strshaheadcontent=" << strshaheadcontent << " strjson=" << strjson; m_writelog(sss.str()); weblib::sendhttp(m_psendhttp, move(thttpinfo), move(dofun)); } void HttpSocket::loaddbsynctime(__int64& itimefrom, __int64& itimeto, __int32& ipage, bool& bdone) { itimefrom = 0; itimeto = 0; ipage = 0; auto finddeveloper = m_pthirddeveloper->find_one(make_document(kvp("id", 2))); if (finddeveloper && finddeveloper->view()["logtimefrom"] && finddeveloper->view()["logtimeto"] && finddeveloper->view()["logpage"] && finddeveloper->view()["logdone"]) { itimefrom = finddeveloper->view()["logtimefrom"].get_int64(); itimeto = finddeveloper->view()["logtimeto"].get_int64(); ipage = finddeveloper->view()["logpage"].get_int32(); bdone = finddeveloper->view()["logdone"].get_bool(); } } void HttpSocket::setdbsynctime(__int64 itimefrom, __int64 itimeto, __int32 ipage, bool bdone) { m_pthirddeveloper->find_one_and_update(make_document(kvp("id", 2)), make_document(kvp("$set" , make_document(kvp("logtimefrom", itimefrom), kvp("logtimeto", itimeto), kvp("logpage", ipage), kvp("logdone", bdone))))); } ///////////////////////////////////////// JsonToBson/////////////////////////////////////////// // 主转换函数(处理JSON对象) void convertJsonToBson(bsoncxx::builder::basic::document& doc, const rapidjson::Value& jsonValue) { if (!jsonValue.IsObject()) { throw runtime_error("Expected JSON object"); } for (auto it = jsonValue.MemberBegin(); it != jsonValue.MemberEnd(); ++it) { const string key = it->name.GetString(); const rapidjson::Value& value = it->value; if (value.IsInt()) { doc.append(kvp(key, value.GetInt())); } else if (value.IsUint()) { doc.append(kvp(key, static_cast(value.GetUint()))); } else if (value.IsInt64()) { doc.append(kvp(key, static_cast(value.GetInt64()))); } else if (value.IsUint64()) { doc.append(kvp(key, static_cast(value.GetUint64()))); } else if (value.IsDouble()) { doc.append(kvp(key, value.GetDouble())); } else if (value.IsString()) { doc.append(kvp(key, value.GetString())); } else if (value.IsBool()) { doc.append(kvp(key, value.GetBool())); } else if (value.IsNull()) { doc.append(kvp(key, bsoncxx::types::b_null())); } else if (value.IsArray()) { bsoncxx::builder::basic::array arr; for (int i = 0; i < value.Size(); i++) { convertJsonValueToBson(arr, value[i]); } doc.append(kvp(key, arr)); } else if (value.IsObject()) { bsoncxx::builder::basic::document sub_doc; convertJsonToBson(sub_doc, value); doc.append(kvp(key, sub_doc)); } } } // 处理JSON值(可用于数组元素) void convertJsonValueToBson(bsoncxx::builder::basic::array& arr, const rapidjson::Value& jsonValue) { if (jsonValue.IsInt()) { arr.append(jsonValue.GetInt()); } else if (jsonValue.IsUint()) { arr.append(static_cast(jsonValue.GetUint())); } else if (jsonValue.IsInt64()) { arr.append(static_cast(jsonValue.GetInt64())); } else if (jsonValue.IsUint64()) { arr.append(static_cast(jsonValue.GetUint64())); } else if (jsonValue.IsDouble()) { arr.append(jsonValue.GetDouble()); } else if (jsonValue.IsString()) { arr.append(jsonValue.GetString()); } else if (jsonValue.IsBool()) { arr.append(jsonValue.GetBool()); } else if (jsonValue.IsNull()) { arr.append(bsoncxx::types::b_null()); } else if (jsonValue.IsArray()) { for (int i = 0; i < jsonValue.Size(); i++) { convertJsonValueToBson(arr, jsonValue[i]); } } else if (jsonValue.IsObject()) { bsoncxx::builder::basic::document sub_doc; convertJsonToBson(sub_doc, jsonValue); arr.append(sub_doc); } } // 处理JSON数组(入口函数) void handleArray(bsoncxx::builder::basic::document& doc, const string& key, const rapidjson::Value& arrayValue) { if (!arrayValue.IsArray()) { throw runtime_error("Expected JSON array"); } bsoncxx::builder::basic::array arr; for (int i = 0; i < arrayValue.Size(); i++) { convertJsonValueToBson(arr, arrayValue[i]); } doc.append(kvp(key, arr)); } ///////////////////////////////////////// JsonToBson end/////////////////////////////////////////// // // //game log cnt /*string HttpSocket::gamelogcnt(map getdata) { gamelogcntdata tgamelogcntdata; getvaluedata(getdata, tgamelogcntdata); string strret{}; bool bret = tgamelogcntdata.datavalue(); if (!bret) { strret = funclib::rettojson(1, "param erro"); return strret; } document finddoc; finddoc.append(kvp("$and", make_array(make_document(kvp("inserttime", make_document(kvp("$gte", tgamelogcntdata.ibegintime)))), make_document(kvp("inserttime", make_document(kvp("$lt", tgamelogcntdata.iendtime))))))); //admin 查询所有的 if (tgamelogcntdata.ispreaderid != 1) { finddoc.append(kvp("spreaderid", tgamelogcntdata.ispreaderid)); } else { if (!tgamelogcntdata.strgid.empty()) { finddoc.append(kvp("gid", tgamelogcntdata.strgid.c_str())); if (tgamelogcntdata.iuserid > 0) { finddoc.append(kvp("playercnt", make_document(kvp("$elemMatch", make_document("$eq", 5))))); } } auto findcntall = m_pgameserverloginfo->find(finddoc.view()); for (auto& doc : findcntall) { if (doc["playercnt"]) { bsoncxx::document::element arrayField = doc["playercnt"]; if (arrayField.type() == bsoncxx::type::k_array) { bsoncxx::builder::basic::array array_wheel{}; bsoncxx::array::view arrayView = arrayField.get_array(); for (auto& element : arrayView) { ijptbet += element["jptbet"].get_int64(); ijptwin += element["jptwin"].get_int64(); ijptcnt += element["jptcnt"].get_int64(); } } } } } }*/