HttpSocket.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610
  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. static __int64 itimefrom = 0;
  200. static __int64 itimeto = 0;
  201. static __int32 ipage = 0;
  202. getsynctime(itimefrom, itimeto, ipage, bAllPageDone);
  203. while (true)
  204. {
  205. static __int64 inowtime = funclib::gettimestamp();
  206. if (itimeto == 0)
  207. {
  208. itimeto = inowtime - 1000;
  209. itimefrom = itimeto - 1000;
  210. }
  211. else if (inowtime - itimeto < 1000)
  212. {
  213. if(!bAllPageDone)
  214. continue;
  215. this_thread::sleep_for(milliseconds(itimeto + 1000 - inowtime));
  216. }
  217. if (bAllPageDone)
  218. ipage = 0;
  219. bAllPageDone = false;
  220. itimefrom = itimeto;
  221. itimeto = funclib::gettimestamp();
  222. auto pid = std::this_thread::get_id();
  223. getgamehistory(itimefrom, itimeto, ipage);
  224. }
  225. }
  226. //拉取日志
  227. void HttpSocket::getgamehistory(__int64 itimefrom, __int64 itimeto, __int32 ifirstpage)
  228. {
  229. m_writelog("--------------------getgamehistory-------------------------");
  230. __int32 ipage = ifirstpage;
  231. __int32 inextpage = 0;
  232. __int32 ipagesize = 1;
  233. rapidjson::Document doc;
  234. doc.SetObject();
  235. rapidjson::Document::AllocatorType& allocator = doc.GetAllocator();
  236. string datefrom = to_iso8601_utc_string(system_clock::time_point(seconds(itimefrom / 1000)));
  237. string dateto = to_iso8601_utc_string(system_clock::time_point(seconds(itimeto / 1000)));
  238. doc.AddMember("casino", rapidjson::Value("luckybet777", allocator), allocator);
  239. doc.AddMember("page", ipage, allocator);
  240. doc.AddMember("size", ipagesize, allocator);
  241. doc.AddMember("dateFrom", rapidjson::Value(datefrom.c_str(), allocator), allocator);
  242. doc.AddMember("dateTo", rapidjson::Value(dateto.c_str(), allocator), allocator);
  243. string strjson = funclib::doctojson(doc);
  244. string strtarget = "v2/tw/getGameHistory";
  245. sendreelmsg("luckybet777", strtarget, strjson, move([this, ipage, ipagesize, itimefrom, itimeto](string strdata, int iret) {
  246. if (iret == 200)
  247. {
  248. rettype::type tret{ rettype::type::ini };
  249. rapidjson::Document docdata;
  250. rapidjson::ParseResult ok = docdata.Parse(strdata.c_str());
  251. string stronlyid = "";
  252. string strgid = "";
  253. string straccount = "";
  254. string strroundData = "";
  255. string strgameResultUrl = "";
  256. string strcurrency = "";
  257. string strdbquestid = "";
  258. __int32 ibrandid = 2;
  259. __int64 iwinscore = 0;
  260. __int64 ibetscore = 0;
  261. double_t dwinmul = 0;
  262. if (ok)
  263. {
  264. static int itotalsize = 0;
  265. if (docdata.HasMember("total"))
  266. itotalsize = docdata["total"].GetInt();
  267. rapidjson::Value arrbets(rapidjson::kArrayType);
  268. if (docdata.HasMember("records") && docdata["records"].IsArray())
  269. {
  270. arrbets = docdata["records"].GetArray();
  271. for (rapidjson::SizeType i = 0; i < arrbets.Size(); ++i)
  272. {
  273. const rapidjson::Value& recorditem = arrbets[i];
  274. if (!recorditem.IsObject())
  275. continue;
  276. if (!recorditem.HasMember("roundFinished") || !recorditem["roundFinished"].GetBool())
  277. continue;
  278. if (!recorditem.HasMember("id"))
  279. continue;
  280. if(!recorditem.HasMember("launchAlias"))
  281. continue;
  282. if (!recorditem.HasMember("playerId"))
  283. continue;
  284. if (!recorditem.HasMember("totalWin"))
  285. continue;
  286. if (!recorditem.HasMember("totalBet"))
  287. continue;
  288. if (!recorditem.HasMember("roundDate"))
  289. continue;
  290. if (!recorditem.HasMember("gameResultUrl"))
  291. continue;
  292. if(!recorditem.HasMember("currency"))
  293. continue;
  294. stronlyid = recorditem["id"].GetString();
  295. //auto findrecord = make_document(kvp("onlyid", stronlyid.c_str()), kvp("brandid", ibrandid));
  296. //auto result = m_pgameprizelog->find_one(findrecord.view());
  297. //if (result)
  298. // continue; // 重复
  299. straccount = recorditem["playerId"].GetString();
  300. stUserInfo stuserinfo = map_acctouserinfo[straccount];
  301. if (!stuserinfo.iuserid)
  302. {
  303. auto finduser = m_pcoll->find_one(make_document(kvp("account", straccount.c_str())));
  304. if (!finduser || !finduser->view()["userid"])
  305. continue;
  306. stuserinfo.iuserid = finduser->view()["userid"].get_int64();
  307. if(finduser->view()["spreaderid"])
  308. stuserinfo.ispreaderid = finduser->view()["spreaderid"].get_int64();
  309. if(finduser->view()["areaid"])
  310. stuserinfo.iareaid = finduser->view()["areaid"].get_int32();
  311. map_acctouserinfo[straccount] = stuserinfo;
  312. }
  313. iwinscore = static_cast<__int64>(std::stod(recorditem["totalWin"].GetString()) * 100);
  314. ibetscore = static_cast<__int64>(std::stod(recorditem["totalBet"].GetString()) * 100);
  315. if(ibetscore > 0)
  316. dwinmul = iwinscore / ibetscore;
  317. strgid = recorditem["launchAlias"].GetString();
  318. strroundData = recorditem["roundDate"].GetString();
  319. strgameResultUrl = recorditem["gameResultUrl"].GetString();
  320. strcurrency = recorditem["currency"].GetString();
  321. document recorddoc;
  322. //convertJsonToBson(recorddoc, recorditem);
  323. recorddoc.append(kvp("onlyid", stronlyid.c_str()));
  324. recorddoc.append(kvp("dbquestid", stronlyid.c_str()));
  325. recorddoc.append(kvp("brandid", ibrandid));
  326. recorddoc.append(kvp("gid", strgid.c_str()));
  327. recorddoc.append(kvp("userid", stuserinfo.iuserid));
  328. recorddoc.append(kvp("areaid", stuserinfo.iareaid));
  329. recorddoc.append(kvp("spreaderid", stuserinfo.ispreaderid));
  330. recorddoc.append(kvp("roundDate", strroundData.c_str()));
  331. recorddoc.append(kvp("gameResultUrl", strgameResultUrl.c_str()));
  332. recorddoc.append(kvp("winscore", iwinscore));
  333. recorddoc.append(kvp("betscore", ibetscore));
  334. recorddoc.append(kvp("dwinmul", dwinmul));
  335. recorddoc.append(kvp("account", straccount.c_str()));
  336. recorddoc.append(kvp("currency", strcurrency.c_str()));
  337. recorddoc.append(kvp("scoretype", 1));
  338. std::int64_t icreateime = getstrfromstamp(strroundData);
  339. recorddoc.append(kvp("inserttime", icreateime));
  340. m_pgameprizelog->insert_one(recorddoc.view());
  341. }
  342. }
  343. if((ipage + 1) * ipagesize >= itotalsize)
  344. bAllPageDone = true;
  345. setsynctime(itimefrom, itimeto, ipage + 1, bAllPageDone);
  346. if(!bAllPageDone)
  347. getgamehistory(itimefrom, itimeto, ipage + 1);
  348. }
  349. }
  350. }));
  351. }
  352. void HttpSocket::sendreelmsg(string stronly, string strtarget, string strjson, function<void(string, int)>&& dofun)
  353. {
  354. weblib::httpinfo thttpinfo;
  355. thttpinfo.strurl = "https://transfer-wallet-service.stage.iconic-21.com";
  356. // thttpinfo.strurl = "http://127.0.0.1:6000";
  357. thttpinfo.strtarget = strtarget;
  358. thttpinfo.stronly = stronly;
  359. string strshaheadcontent = m_strshaheadcontent + strjson;
  360. strshaheadcontent = sha256_to_base16(strshaheadcontent);
  361. thttpinfo.addheadinfo.insert(make_pair(m_strshahead, strshaheadcontent));
  362. thttpinfo.strjson = strjson;
  363. thttpinfo.setresult_int.insert(404);
  364. stringstream sss;
  365. sss << "strurl=" << thttpinfo.strurl << " strtarget=" << strtarget << " strshaheadcontent=" << strshaheadcontent << " strjson=" << strjson;
  366. m_writelog(sss.str());
  367. weblib::sendhttp(m_psendhttp, move(thttpinfo), move(dofun));
  368. }
  369. void HttpSocket::getsynctime(__int64& itimefrom, __int64& itimeto, __int32& ipage, bool& bdone)
  370. {
  371. itimefrom = 0;
  372. itimeto = 0;
  373. ipage = 0;
  374. auto finddeveloper = m_pthirddeveloper->find_one(make_document(kvp("id", 2)));
  375. if (finddeveloper && finddeveloper->view()["logtimefrom"] && finddeveloper->view()["logtimeto"] &&
  376. finddeveloper->view()["logpage"] && finddeveloper->view()["logdone"])
  377. {
  378. itimefrom = finddeveloper->view()["logtimefrom"].get_int64();
  379. itimeto = finddeveloper->view()["logtimeto"].get_int64();
  380. ipage = finddeveloper->view()["logpage"].get_int32();
  381. bdone = finddeveloper->view()["logdone"].get_bool();
  382. }
  383. }
  384. void HttpSocket::setsynctime(__int64 itimefrom, __int64 itimeto, __int32 ipage, bool bdone)
  385. {
  386. m_pthirddeveloper->find_one_and_update(make_document(kvp("id", 2)),
  387. make_document(kvp("$set" , make_document(kvp("logtimefrom", itimefrom), kvp("logtimeto", itimeto), kvp("logpage", ipage), kvp("logdone", bdone)))));
  388. }
  389. ///////////////////////////////////////// JsonToBson///////////////////////////////////////////
  390. // 主转换函数(处理JSON对象)
  391. void convertJsonToBson(bsoncxx::builder::basic::document& doc, const rapidjson::Value& jsonValue)
  392. {
  393. if (!jsonValue.IsObject()) {
  394. throw runtime_error("Expected JSON object");
  395. }
  396. for (auto it = jsonValue.MemberBegin(); it != jsonValue.MemberEnd(); ++it)
  397. {
  398. const string key = it->name.GetString();
  399. const rapidjson::Value& value = it->value;
  400. if (value.IsInt()) {
  401. doc.append(kvp(key, value.GetInt()));
  402. }
  403. else if (value.IsUint()) {
  404. doc.append(kvp(key, static_cast<int32_t>(value.GetUint())));
  405. }
  406. else if (value.IsInt64()) {
  407. doc.append(kvp(key, static_cast<int64_t>(value.GetInt64())));
  408. }
  409. else if (value.IsUint64()) {
  410. doc.append(kvp(key, static_cast<int64_t>(value.GetUint64())));
  411. }
  412. else if (value.IsDouble()) {
  413. doc.append(kvp(key, value.GetDouble()));
  414. }
  415. else if (value.IsString()) {
  416. doc.append(kvp(key, value.GetString()));
  417. }
  418. else if (value.IsBool()) {
  419. doc.append(kvp(key, value.GetBool()));
  420. }
  421. else if (value.IsNull()) {
  422. doc.append(kvp(key, bsoncxx::types::b_null()));
  423. }
  424. else if (value.IsArray()) {
  425. bsoncxx::builder::basic::array arr;
  426. for (int i = 0; i < value.Size(); i++) {
  427. convertJsonValueToBson(arr, value[i]);
  428. }
  429. doc.append(kvp(key, arr));
  430. }
  431. else if (value.IsObject()) {
  432. bsoncxx::builder::basic::document sub_doc;
  433. convertJsonToBson(sub_doc, value);
  434. doc.append(kvp(key, sub_doc));
  435. }
  436. }
  437. }
  438. // 处理JSON值(可用于数组元素)
  439. void convertJsonValueToBson(bsoncxx::builder::basic::array& arr, const rapidjson::Value& jsonValue)
  440. {
  441. if (jsonValue.IsInt()) {
  442. arr.append(jsonValue.GetInt());
  443. }
  444. else if (jsonValue.IsUint()) {
  445. arr.append(static_cast<int32_t>(jsonValue.GetUint()));
  446. }
  447. else if (jsonValue.IsInt64()) {
  448. arr.append(static_cast<int64_t>(jsonValue.GetInt64()));
  449. }
  450. else if (jsonValue.IsUint64()) {
  451. arr.append(static_cast<int64_t>(jsonValue.GetUint64()));
  452. }
  453. else if (jsonValue.IsDouble()) {
  454. arr.append(jsonValue.GetDouble());
  455. }
  456. else if (jsonValue.IsString()) {
  457. arr.append(jsonValue.GetString());
  458. }
  459. else if (jsonValue.IsBool()) {
  460. arr.append(jsonValue.GetBool());
  461. }
  462. else if (jsonValue.IsNull()) {
  463. arr.append(bsoncxx::types::b_null());
  464. }
  465. else if (jsonValue.IsArray()) {
  466. for (int i = 0; i < jsonValue.Size(); i++) {
  467. convertJsonValueToBson(arr, jsonValue[i]);
  468. }
  469. }
  470. else if (jsonValue.IsObject()) {
  471. bsoncxx::builder::basic::document sub_doc;
  472. convertJsonToBson(sub_doc, jsonValue);
  473. arr.append(sub_doc);
  474. }
  475. }
  476. // 处理JSON数组(入口函数)
  477. void handleArray(bsoncxx::builder::basic::document& doc, const string& key, const rapidjson::Value& arrayValue)
  478. {
  479. if (!arrayValue.IsArray()) {
  480. throw runtime_error("Expected JSON array");
  481. }
  482. bsoncxx::builder::basic::array arr;
  483. for (int i = 0; i < arrayValue.Size(); i++)
  484. {
  485. convertJsonValueToBson(arr, arrayValue[i]);
  486. }
  487. doc.append(kvp(key, arr));
  488. }
  489. ///////////////////////////////////////// JsonToBson end///////////////////////////////////////////
  490. //
  491. //
  492. //game log cnt
  493. /*string HttpSocket::gamelogcnt(map<string, string> getdata)
  494. {
  495. gamelogcntdata tgamelogcntdata;
  496. getvaluedata(getdata, tgamelogcntdata);
  497. string strret{};
  498. bool bret = tgamelogcntdata.datavalue();
  499. if (!bret)
  500. {
  501. strret = funclib::rettojson(1, "param erro");
  502. return strret;
  503. }
  504. document finddoc;
  505. finddoc.append(kvp("$and", make_array(make_document(kvp("inserttime", make_document(kvp("$gte", tgamelogcntdata.ibegintime)))),
  506. make_document(kvp("inserttime", make_document(kvp("$lt", tgamelogcntdata.iendtime)))))));
  507. //admin 查询所有的
  508. if (tgamelogcntdata.ispreaderid != 1)
  509. {
  510. finddoc.append(kvp("spreaderid", tgamelogcntdata.ispreaderid));
  511. }
  512. else
  513. {
  514. if (!tgamelogcntdata.strgid.empty())
  515. {
  516. finddoc.append(kvp("gid", tgamelogcntdata.strgid.c_str()));
  517. if (tgamelogcntdata.iuserid > 0)
  518. {
  519. finddoc.append(kvp("playercnt", make_document(kvp("$elemMatch", make_document("$eq", 5)))));
  520. }
  521. }
  522. auto findcntall = m_pgameserverloginfo->find(finddoc.view());
  523. for (auto& doc : findcntall)
  524. {
  525. if (doc["playercnt"])
  526. {
  527. bsoncxx::document::element arrayField = doc["playercnt"];
  528. if (arrayField.type() == bsoncxx::type::k_array)
  529. {
  530. bsoncxx::builder::basic::array array_wheel{};
  531. bsoncxx::array::view arrayView = arrayField.get_array();
  532. for (auto& element : arrayView)
  533. {
  534. ijptbet += element["jptbet"].get_int64();
  535. ijptwin += element["jptwin"].get_int64();
  536. ijptcnt += element["jptcnt"].get_int64();
  537. }
  538. }
  539. }
  540. }
  541. }
  542. }*/