HttpSocket.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611
  1. #include "pch.h"
  2. #include "HttpSocket.h"
  3. #include <set>
  4. #include <chrono>
  5. #include <ctime>
  6. #include <iomanip>
  7. #include <sstream>
  8. #include <string>
  9. #include <utility>
  10. #include <boost/lexical_cast.hpp>
  11. #include <boost/chrono.hpp>
  12. #include <boost/date_time/posix_time/posix_time.hpp>
  13. using namespace bsoncxx::builder::basic;
  14. using namespace chrono;
  15. mongocxx::instance g_instance{};
  16. std::int64_t getstrfromstamp(std::string strtime)
  17. {
  18. __int64 igettime = 0;
  19. boost::replace_all(strtime, " ", "T");
  20. boost::replace_all(strtime, "-", "");
  21. boost::replace_all(strtime, ":", "");
  22. strtime += "000";
  23. boost::posix_time::ptime ssp = boost::posix_time::from_iso_string(strtime);
  24. boost::posix_time::time_duration time_from_epoch = boost::posix_time::microsec_clock::universal_time() - boost::posix_time::microsec_clock::local_time();
  25. boost::posix_time::ptime epoch(boost::gregorian::date(1970, boost::gregorian::Jan, 1));
  26. boost::posix_time::time_duration time_epoch = ssp - epoch;
  27. igettime = time_epoch.total_milliseconds() + time_from_epoch.total_milliseconds() / 360000 * 360000;
  28. return igettime;
  29. }
  30. HttpSocket::HttpSocket()
  31. {
  32. __int64 itime = funclib::gettimestamp();
  33. m_random = default_random_engine(itime);
  34. m_pmsgservice = boost::make_shared<boost::asio::io_context>();
  35. m_pmsgwork = boost::make_shared<boost::asio::io_context::work>(*m_pmsgservice);
  36. data_time = boost::make_shared<boost::asio::deadline_timer>(*m_pmsgservice);
  37. //数据库配置
  38. string strip = funclib::getdatainfo();
  39. //strip = "mongodb://datzahndle:pajdt873hyu3h37@110.40.64.236:37317";
  40. //strip = "mongodb://110.40.59.112:37317";
  41. //strip = "mongodb://192.168.0.220:27017";
  42. m_puri = boost::make_shared<mongocxx::uri>(strip.c_str());
  43. m_pclient = boost::make_shared <mongocxx::client>(*m_puri);
  44. m_pdb = boost::make_shared<mongocxx::database>();
  45. m_platform = boost::make_shared<mongocxx::database>();
  46. m_gamelog = boost::make_shared<mongocxx::database>();
  47. m_pvipuserdb = boost::make_shared<mongocxx::database>();
  48. m_ptask = boost::make_shared<mongocxx::database>();
  49. m_viplog = boost::make_shared<mongocxx::database>();
  50. m_webdata = boost::make_shared<mongocxx::database>();
  51. m_pallindex = boost::make_shared<mongocxx::collection>();
  52. m_pcoll = boost::make_shared<mongocxx::collection>();
  53. m_plosewincoll = boost::make_shared<mongocxx::collection>();
  54. m_pvipuser = boost::make_shared<mongocxx::collection>();
  55. m_pplayuseronline = boost::make_shared<mongocxx::collection>();
  56. m_userloginonline = boost::make_shared<mongocxx::collection>();
  57. m_pusertradeinfo = boost::make_shared<mongocxx::collection>();
  58. m_pviptradeinfo = boost::make_shared<mongocxx::collection>();
  59. m_poperatinfo = boost::make_shared<mongocxx::collection>();
  60. m_pgameviploginfo = boost::make_shared<mongocxx::collection>();
  61. m_pgameserverloginfo = boost::make_shared<mongocxx::collection>();
  62. m_pvipalllogcache = boost::make_shared<mongocxx::collection>();
  63. m_pserverlogcache = boost::make_shared<mongocxx::collection>();
  64. m_puserlogcache = boost::make_shared<mongocxx::collection>();
  65. m_pgameuserloginfo = boost::make_shared<mongocxx::collection>();
  66. m_pviplogcache = boost::make_shared<mongocxx::collection>();
  67. m_pgameprizelog = boost::make_shared<mongocxx::collection>();
  68. m_puserdowninfo = boost::make_shared<mongocxx::collection>();
  69. m_pvipscore = boost::make_shared<mongocxx::collection>();
  70. m_pvipscoredate = boost::make_shared<mongocxx::collection>();
  71. m_pvipuseraccounts = boost::make_shared<mongocxx::collection>();
  72. m_puserdatainfo = boost::make_shared<mongocxx::collection>();
  73. m_pvipconfigure = boost::make_shared<mongocxx::collection>();
  74. m_pusersub = boost::make_shared<mongocxx::collection>();
  75. m_pexechangerate = boost::make_shared<mongocxx::collection>();
  76. m_pthreeadmin = boost::make_shared<mongocxx::collection>();
  77. m_pconfig = boost::make_shared<mongocxx::collection>();
  78. m_pkinditem = boost::make_shared<mongocxx::collection>();
  79. m_pipwhite = boost::make_shared<mongocxx::collection>();
  80. m_papplyviplog = boost::make_shared<mongocxx::collection>();
  81. m_pvipmsglog = boost::make_shared<mongocxx::collection>();
  82. m_pgamelist = boost::make_shared<mongocxx::collection>();
  83. m_pgamegameitem = boost::make_shared<mongocxx::collection>();
  84. m_ppopularizesit = boost::make_shared<mongocxx::collection>();
  85. m_activityfreegame = boost::make_shared<mongocxx::collection>();
  86. m_pmarquee = boost::make_shared<mongocxx::collection>();
  87. m_pcustomerlink = boost::make_shared<mongocxx::collection>();
  88. m_pcredittopwinner = boost::make_shared<mongocxx::collection>();
  89. m_resendorder = boost::make_shared<mongocxx::collection>();
  90. m_pareagames = boost::make_shared<mongocxx::collection>();
  91. m_pplayrecordini = boost::make_shared<mongocxx::collection>();
  92. m_pgamebet = boost::make_shared<mongocxx::collection>();
  93. m_pactivityini = boost::make_shared<mongocxx::collection>();
  94. m_pplatformini = boost::make_shared<mongocxx::collection>();
  95. m_pthirddeveloper = boost::make_shared<mongocxx::collection>();
  96. m_pthirdgames = boost::make_shared<mongocxx::collection>();
  97. m_pgameprizelog = boost::make_shared<mongocxx::collection>();
  98. *m_pdb = (*m_pclient)["accounts"];
  99. *m_platform = (*m_pclient)["platform"];
  100. *m_gamelog = (*m_pclient)["gamelog"];
  101. *m_ptask = (*m_pclient)["task"];
  102. *m_viplog = (*m_pclient)["viplog"];
  103. *m_webdata = (*m_pclient)["webdata"];
  104. *m_pvipuserdb = (*m_pclient)["vipuser"];
  105. *m_pallindex = (*m_pdb)["allindex"];
  106. *m_pcoll = (*m_pdb)["userinfo"];
  107. *m_plosewincoll = (*m_pdb)["losewin"];
  108. *m_pvipuser = (*m_pvipuserdb)["user"];
  109. *m_pplayuseronline = (*m_gamelog)["playuseronline"];
  110. *m_userloginonline = (*m_gamelog)["userloginonline"];
  111. *m_poperatinfo = (*m_gamelog)["operatinfo"];
  112. *m_puserdowninfo = (*m_gamelog)["userdowninfo"];
  113. *m_pusertradeinfo = (*m_pvipuserdb)["usertradeinfo"];
  114. *m_pgameviploginfo = (*m_gamelog)["gameviploginfo"];
  115. *m_pgameserverloginfo = (*m_gamelog)["gameserverloginfo"];
  116. *m_pvipalllogcache = (*m_gamelog)["vipalllogcache"];
  117. *m_pserverlogcache = (*m_gamelog)["serverlogcache"];
  118. *m_puserlogcache = (*m_gamelog)["userlogcache"];
  119. *m_pgameuserloginfo = (*m_gamelog)["gameuserloginfo"];
  120. *m_pviplogcache = (*m_gamelog)["viplogcache"];
  121. *m_pgameprizelog = (*m_gamelog)["gameprizelog"];
  122. *m_resendorder = (*m_gamelog)["resendorder"];
  123. *m_pviptradeinfo = (*m_pvipuserdb)["viptradeinfo"];
  124. *m_pvipscore = (*m_pvipuserdb)["vipscore"];
  125. *m_pvipscoredate = (*m_pvipuserdb)["vipscoredate"];
  126. *m_pvipuseraccounts = (*m_pvipuserdb)["vipuseraccounts"];
  127. *m_puserdatainfo = (*m_gamelog)["userdatainfo"];
  128. *m_pvipconfigure = (*m_pvipuserdb)["vipconfigure"];
  129. *m_pthreeadmin = (*m_pvipuserdb)["threeadmin"];
  130. *m_pusersub = (*m_pvipuserdb)["usersub"];
  131. *m_pexechangerate = (*m_platform)["exechangerate"];
  132. *m_pkinditem = (*m_platform)["kinditem"];
  133. *m_pgamelist = (*m_platform)["gamelist"];
  134. *m_pgamegameitem = (*m_platform)["gamegameitem"];
  135. *m_ppopularizesit = (*m_pvipuserdb)["popularizesit"]; // 推广网站
  136. *m_pmarquee = (*m_pvipuserdb)["marquee"]; // 信用代理跑马灯
  137. *m_pcustomerlink = (*m_pvipuserdb)["customerlink"];
  138. *m_pcredittopwinner = (*m_gamelog)["credittopwinner"];
  139. *m_pplatformini = (*m_platform)["platformini"];
  140. //task
  141. *m_pconfig = (*m_ptask)["config"];
  142. *m_pipwhite = (*m_pvipuserdb)["ipwhite"];
  143. //viplog
  144. *m_papplyviplog = (*m_viplog)["applyviplog"];
  145. *m_pvipmsglog = (*m_viplog)["vipmsglog"];
  146. //freegame
  147. *m_activityfreegame = (*m_pvipuserdb)["freegameactivity"];
  148. //areagame
  149. *m_pareagames = (*m_platform)["areagames"];
  150. *m_pplayrecordini = (*m_platform)["playrecordini"];
  151. // 游戏投注配置
  152. *m_pgamebet = (*m_platform)["gamebet"];
  153. // 游戏活动配置
  154. *m_pactivityini = (*m_webdata)["activeini"];
  155. // 第三方厂商
  156. *m_pthirddeveloper = (*m_platform)["thirddeveloper"];
  157. // 第三方厂商游戏
  158. *m_pthirdgames = (*m_platform)["thirdgames"];
  159. m_strshahead = "X-REQUEST-SIGN";
  160. m_strshaheadcontent = "6e4c91001979851a97c2b5360f044ff67b48e186d6ecd4394532851bffdeeae9";
  161. m_psendhttp = weblib::httpini();
  162. m_brun = false;
  163. m_msgthread = thread([this] {
  164. m_ucurid = funclib::getcurid();
  165. m_brun = true;
  166. m_pmsgservice->run();
  167. m_brun = false;
  168. });
  169. m_msgthread.detach();
  170. while (!m_brun)
  171. {
  172. this_thread::yield();
  173. }
  174. }
  175. HttpSocket::~HttpSocket()
  176. {
  177. }
  178. // 毫秒时间戳 → ISO-8601 UTC 时间字符串
  179. string to_iso8601_utc_string(chrono::system_clock::time_point tp)
  180. {
  181. auto ms = duration_cast<milliseconds>(tp.time_since_epoch()) % 1000;
  182. time_t t = system_clock::to_time_t(tp);
  183. tm tm = *gmtime(&t);
  184. ostringstream oss;
  185. oss << put_time(&tm, "%Y-%m-%d %H:%M:%S");
  186. oss << '.' << setfill('0') << setw(3) << ms.count();
  187. return oss.str();
  188. }
  189. struct stUserInfo
  190. {
  191. __int64 ispreaderid = 0;
  192. __int32 iareaid = 0;
  193. __int64 iuserid = 0;
  194. };
  195. static map<string, stUserInfo> map_acctouserinfo;
  196. static bool bAllPageDone = false; //这次请求是否请求完所有分页
  197. void HttpSocket::init()
  198. {
  199. __int64 itimefrom = 0;
  200. __int64 itimeto = 0;
  201. __int32 ipage = 0;
  202. __int64 inowtime = 0;
  203. bool bFirst = true;
  204. loaddbsynctime(itimefrom, itimeto, ipage, bAllPageDone);
  205. while (true)
  206. {
  207. inowtime = funclib::gettimestamp();
  208. if (itimeto == 0)
  209. {
  210. itimeto = inowtime - 1000;
  211. itimefrom = itimeto - 1000;
  212. }
  213. else if (inowtime - itimeto < 1000)
  214. {
  215. this_thread::sleep_for(milliseconds(itimeto + 1000 - inowtime));
  216. }
  217. if (!bFirst && !bAllPageDone)
  218. continue;
  219. else
  220. ipage = 0;
  221. bAllPageDone = false;
  222. bFirst = false;
  223. itimefrom = itimeto;
  224. itimeto = funclib::gettimestamp();
  225. getgamehistory(itimefrom, itimeto, ipage);
  226. }
  227. }
  228. //拉取日志
  229. void HttpSocket::getgamehistory(__int64 itimefrom, __int64 itimeto, __int32 ifirstpage)
  230. {
  231. m_writelog("--------------------getgamehistory-------------------------");
  232. __int32 ipage = ifirstpage;
  233. __int32 inextpage = 0;
  234. __int32 ipagesize = 500;
  235. rapidjson::Document doc;
  236. doc.SetObject();
  237. rapidjson::Document::AllocatorType& allocator = doc.GetAllocator();
  238. string datefrom = to_iso8601_utc_string(system_clock::time_point(seconds(itimefrom / 1000)));
  239. string dateto = to_iso8601_utc_string(system_clock::time_point(seconds(itimeto / 1000)));
  240. doc.AddMember("casino", rapidjson::Value("luckybet777", allocator), allocator);
  241. doc.AddMember("page", ipage, allocator);
  242. doc.AddMember("size", ipagesize, allocator);
  243. doc.AddMember("dateFrom", rapidjson::Value(datefrom.c_str(), allocator), allocator);
  244. doc.AddMember("dateTo", rapidjson::Value(dateto.c_str(), allocator), allocator);
  245. string strjson = funclib::doctojson(doc);
  246. string strtarget = "v2/tw/getGameHistory";
  247. sendreelmsg("luckybet777", strtarget, strjson, move([this, ipage, ipagesize, itimefrom, itimeto](string strdata, int iret) {
  248. if (iret == 200)
  249. {
  250. rettype::type tret{ rettype::type::ini };
  251. rapidjson::Document docdata;
  252. rapidjson::ParseResult ok = docdata.Parse(strdata.c_str());
  253. string stronlyid = "";
  254. string strgid = "";
  255. string straccount = "";
  256. string strroundData = "";
  257. string strgameResultUrl = "";
  258. string strcurrency = "";
  259. string strdbquestid = "";
  260. __int32 ibrandid = 2;
  261. __int64 iwinscore = 0;
  262. __int64 ibetscore = 0;
  263. double_t dwinmul = 0;
  264. if (ok)
  265. {
  266. static int itotalsize = 0;
  267. if (docdata.HasMember("total"))
  268. itotalsize = docdata["total"].GetInt();
  269. rapidjson::Value arrbets(rapidjson::kArrayType);
  270. if (docdata.HasMember("records") && docdata["records"].IsArray())
  271. {
  272. arrbets = docdata["records"].GetArray();
  273. for (rapidjson::SizeType i = 0; i < arrbets.Size(); ++i)
  274. {
  275. const rapidjson::Value& recorditem = arrbets[i];
  276. if (!recorditem.IsObject())
  277. continue;
  278. if (!recorditem.HasMember("roundFinished") || !recorditem["roundFinished"].GetBool())
  279. continue;
  280. if (!recorditem.HasMember("id"))
  281. continue;
  282. if(!recorditem.HasMember("launchAlias"))
  283. continue;
  284. if (!recorditem.HasMember("playerId"))
  285. continue;
  286. if (!recorditem.HasMember("totalWin"))
  287. continue;
  288. if (!recorditem.HasMember("totalBet"))
  289. continue;
  290. if (!recorditem.HasMember("roundDate"))
  291. continue;
  292. if (!recorditem.HasMember("gameResultUrl"))
  293. continue;
  294. if(!recorditem.HasMember("currency"))
  295. continue;
  296. stronlyid = recorditem["id"].GetString();
  297. //auto findrecord = make_document(kvp("onlyid", stronlyid.c_str()), kvp("brandid", ibrandid));
  298. //auto result = m_pgameprizelog->find_one(findrecord.view());
  299. //if (result)
  300. // continue; // 重复
  301. straccount = recorditem["playerId"].GetString();
  302. stUserInfo stuserinfo = map_acctouserinfo[straccount];
  303. if (!stuserinfo.iuserid)
  304. {
  305. auto finduser = m_pcoll->find_one(make_document(kvp("account", straccount.c_str())));
  306. if (!finduser || !finduser->view()["userid"])
  307. continue;
  308. stuserinfo.iuserid = finduser->view()["userid"].get_int64();
  309. if(finduser->view()["spreaderid"])
  310. stuserinfo.ispreaderid = finduser->view()["spreaderid"].get_int64();
  311. if(finduser->view()["areaid"])
  312. stuserinfo.iareaid = finduser->view()["areaid"].get_int32();
  313. map_acctouserinfo[straccount] = stuserinfo;
  314. }
  315. iwinscore = static_cast<__int64>(std::stod(recorditem["totalWin"].GetString()) * 100);
  316. ibetscore = static_cast<__int64>(std::stod(recorditem["totalBet"].GetString()) * 100);
  317. if(ibetscore > 0)
  318. dwinmul = iwinscore / ibetscore;
  319. strgid = recorditem["launchAlias"].GetString();
  320. strroundData = recorditem["roundDate"].GetString();
  321. strgameResultUrl = recorditem["gameResultUrl"].GetString();
  322. strcurrency = recorditem["currency"].GetString();
  323. document recorddoc;
  324. //convertJsonToBson(recorddoc, recorditem);
  325. recorddoc.append(kvp("onlyid", stronlyid.c_str()));
  326. recorddoc.append(kvp("dbquestid", stronlyid.c_str()));
  327. recorddoc.append(kvp("brandid", ibrandid));
  328. recorddoc.append(kvp("gid", strgid.c_str()));
  329. recorddoc.append(kvp("userid", stuserinfo.iuserid));
  330. recorddoc.append(kvp("areaid", stuserinfo.iareaid));
  331. recorddoc.append(kvp("spreaderid", stuserinfo.ispreaderid));
  332. recorddoc.append(kvp("roundDate", strroundData.c_str()));
  333. recorddoc.append(kvp("gameResultUrl", strgameResultUrl.c_str()));
  334. recorddoc.append(kvp("winscore", iwinscore));
  335. recorddoc.append(kvp("betscore", ibetscore));
  336. recorddoc.append(kvp("dwinmul", dwinmul));
  337. recorddoc.append(kvp("account", straccount.c_str()));
  338. recorddoc.append(kvp("currency", strcurrency.c_str()));
  339. recorddoc.append(kvp("scoretype", 1));
  340. std::int64_t icreateime = getstrfromstamp(strroundData);
  341. recorddoc.append(kvp("inserttime", icreateime));
  342. m_pgameprizelog->insert_one(recorddoc.view());
  343. }
  344. }
  345. if((ipage + 1) * ipagesize >= itotalsize)
  346. bAllPageDone = true;
  347. setdbsynctime(itimefrom, itimeto, ipage + 1, bAllPageDone);
  348. if(!bAllPageDone)
  349. getgamehistory(itimefrom, itimeto, ipage + 1);
  350. }
  351. }
  352. }));
  353. }
  354. void HttpSocket::sendreelmsg(string stronly, string strtarget, string strjson, function<void(string, int)>&& dofun)
  355. {
  356. weblib::httpinfo thttpinfo;
  357. thttpinfo.strurl = "https://transfer-wallet-service.stage.iconic-21.com";
  358. // thttpinfo.strurl = "http://127.0.0.1:6000";
  359. thttpinfo.strtarget = strtarget;
  360. thttpinfo.stronly = stronly;
  361. string strshaheadcontent = m_strshaheadcontent + strjson;
  362. strshaheadcontent = sha256_to_base16(strshaheadcontent);
  363. thttpinfo.addheadinfo.insert(make_pair(m_strshahead, strshaheadcontent));
  364. thttpinfo.strjson = strjson;
  365. thttpinfo.setresult_int.insert(404);
  366. stringstream sss;
  367. sss << "strurl=" << thttpinfo.strurl << " strtarget=" << strtarget << " strshaheadcontent=" << strshaheadcontent << " strjson=" << strjson;
  368. m_writelog(sss.str());
  369. weblib::sendhttp(m_psendhttp, move(thttpinfo), move(dofun));
  370. }
  371. void HttpSocket::loaddbsynctime(__int64& itimefrom, __int64& itimeto, __int32& ipage, bool& bdone)
  372. {
  373. itimefrom = 0;
  374. itimeto = 0;
  375. ipage = 0;
  376. auto finddeveloper = m_pthirddeveloper->find_one(make_document(kvp("id", 2)));
  377. if (finddeveloper && finddeveloper->view()["logtimefrom"] && finddeveloper->view()["logtimeto"] &&
  378. finddeveloper->view()["logpage"] && finddeveloper->view()["logdone"])
  379. {
  380. itimefrom = finddeveloper->view()["logtimefrom"].get_int64();
  381. itimeto = finddeveloper->view()["logtimeto"].get_int64();
  382. ipage = finddeveloper->view()["logpage"].get_int32();
  383. bdone = finddeveloper->view()["logdone"].get_bool();
  384. }
  385. }
  386. void HttpSocket::setdbsynctime(__int64 itimefrom, __int64 itimeto, __int32 ipage, bool bdone)
  387. {
  388. m_pthirddeveloper->find_one_and_update(make_document(kvp("id", 2)),
  389. make_document(kvp("$set" , make_document(kvp("logtimefrom", itimefrom), kvp("logtimeto", itimeto), kvp("logpage", ipage), kvp("logdone", bdone)))));
  390. }
  391. ///////////////////////////////////////// JsonToBson///////////////////////////////////////////
  392. // 主转换函数(处理JSON对象)
  393. void convertJsonToBson(bsoncxx::builder::basic::document& doc, const rapidjson::Value& jsonValue)
  394. {
  395. if (!jsonValue.IsObject()) {
  396. throw runtime_error("Expected JSON object");
  397. }
  398. for (auto it = jsonValue.MemberBegin(); it != jsonValue.MemberEnd(); ++it)
  399. {
  400. const string key = it->name.GetString();
  401. const rapidjson::Value& value = it->value;
  402. if (value.IsInt()) {
  403. doc.append(kvp(key, value.GetInt()));
  404. }
  405. else if (value.IsUint()) {
  406. doc.append(kvp(key, static_cast<int32_t>(value.GetUint())));
  407. }
  408. else if (value.IsInt64()) {
  409. doc.append(kvp(key, static_cast<int64_t>(value.GetInt64())));
  410. }
  411. else if (value.IsUint64()) {
  412. doc.append(kvp(key, static_cast<int64_t>(value.GetUint64())));
  413. }
  414. else if (value.IsDouble()) {
  415. doc.append(kvp(key, value.GetDouble()));
  416. }
  417. else if (value.IsString()) {
  418. doc.append(kvp(key, value.GetString()));
  419. }
  420. else if (value.IsBool()) {
  421. doc.append(kvp(key, value.GetBool()));
  422. }
  423. else if (value.IsNull()) {
  424. doc.append(kvp(key, bsoncxx::types::b_null()));
  425. }
  426. else if (value.IsArray()) {
  427. bsoncxx::builder::basic::array arr;
  428. for (int i = 0; i < value.Size(); i++) {
  429. convertJsonValueToBson(arr, value[i]);
  430. }
  431. doc.append(kvp(key, arr));
  432. }
  433. else if (value.IsObject()) {
  434. bsoncxx::builder::basic::document sub_doc;
  435. convertJsonToBson(sub_doc, value);
  436. doc.append(kvp(key, sub_doc));
  437. }
  438. }
  439. }
  440. // 处理JSON值(可用于数组元素)
  441. void convertJsonValueToBson(bsoncxx::builder::basic::array& arr, const rapidjson::Value& jsonValue)
  442. {
  443. if (jsonValue.IsInt()) {
  444. arr.append(jsonValue.GetInt());
  445. }
  446. else if (jsonValue.IsUint()) {
  447. arr.append(static_cast<int32_t>(jsonValue.GetUint()));
  448. }
  449. else if (jsonValue.IsInt64()) {
  450. arr.append(static_cast<int64_t>(jsonValue.GetInt64()));
  451. }
  452. else if (jsonValue.IsUint64()) {
  453. arr.append(static_cast<int64_t>(jsonValue.GetUint64()));
  454. }
  455. else if (jsonValue.IsDouble()) {
  456. arr.append(jsonValue.GetDouble());
  457. }
  458. else if (jsonValue.IsString()) {
  459. arr.append(jsonValue.GetString());
  460. }
  461. else if (jsonValue.IsBool()) {
  462. arr.append(jsonValue.GetBool());
  463. }
  464. else if (jsonValue.IsNull()) {
  465. arr.append(bsoncxx::types::b_null());
  466. }
  467. else if (jsonValue.IsArray()) {
  468. for (int i = 0; i < jsonValue.Size(); i++) {
  469. convertJsonValueToBson(arr, jsonValue[i]);
  470. }
  471. }
  472. else if (jsonValue.IsObject()) {
  473. bsoncxx::builder::basic::document sub_doc;
  474. convertJsonToBson(sub_doc, jsonValue);
  475. arr.append(sub_doc);
  476. }
  477. }
  478. // 处理JSON数组(入口函数)
  479. void handleArray(bsoncxx::builder::basic::document& doc, const string& key, const rapidjson::Value& arrayValue)
  480. {
  481. if (!arrayValue.IsArray()) {
  482. throw runtime_error("Expected JSON array");
  483. }
  484. bsoncxx::builder::basic::array arr;
  485. for (int i = 0; i < arrayValue.Size(); i++)
  486. {
  487. convertJsonValueToBson(arr, arrayValue[i]);
  488. }
  489. doc.append(kvp(key, arr));
  490. }
  491. ///////////////////////////////////////// JsonToBson end///////////////////////////////////////////
  492. //
  493. //
  494. //game log cnt
  495. /*string HttpSocket::gamelogcnt(map<string, string> getdata)
  496. {
  497. gamelogcntdata tgamelogcntdata;
  498. getvaluedata(getdata, tgamelogcntdata);
  499. string strret{};
  500. bool bret = tgamelogcntdata.datavalue();
  501. if (!bret)
  502. {
  503. strret = funclib::rettojson(1, "param erro");
  504. return strret;
  505. }
  506. document finddoc;
  507. finddoc.append(kvp("$and", make_array(make_document(kvp("inserttime", make_document(kvp("$gte", tgamelogcntdata.ibegintime)))),
  508. make_document(kvp("inserttime", make_document(kvp("$lt", tgamelogcntdata.iendtime)))))));
  509. //admin 查询所有的
  510. if (tgamelogcntdata.ispreaderid != 1)
  511. {
  512. finddoc.append(kvp("spreaderid", tgamelogcntdata.ispreaderid));
  513. }
  514. else
  515. {
  516. if (!tgamelogcntdata.strgid.empty())
  517. {
  518. finddoc.append(kvp("gid", tgamelogcntdata.strgid.c_str()));
  519. if (tgamelogcntdata.iuserid > 0)
  520. {
  521. finddoc.append(kvp("playercnt", make_document(kvp("$elemMatch", make_document("$eq", 5)))));
  522. }
  523. }
  524. auto findcntall = m_pgameserverloginfo->find(finddoc.view());
  525. for (auto& doc : findcntall)
  526. {
  527. if (doc["playercnt"])
  528. {
  529. bsoncxx::document::element arrayField = doc["playercnt"];
  530. if (arrayField.type() == bsoncxx::type::k_array)
  531. {
  532. bsoncxx::builder::basic::array array_wheel{};
  533. bsoncxx::array::view arrayView = arrayField.get_array();
  534. for (auto& element : arrayView)
  535. {
  536. ijptbet += element["jptbet"].get_int64();
  537. ijptwin += element["jptwin"].get_int64();
  538. ijptcnt += element["jptcnt"].get_int64();
  539. }
  540. }
  541. }
  542. }
  543. }
  544. }*/