CNetwork.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. #include "pch.h"
  2. #include "CNetwork.h"
  3. #include <nlohmann/json.hpp>
  4. #include "CUtils.h"
  5. CNetwork::CNetwork() : m_http_ret(http_no), m_http_status(0)
  6. {
  7. Init();
  8. }
  9. CNetwork::~CNetwork(void)
  10. {
  11. UnInit();
  12. }
  13. void CNetwork::SetUrl(LPCSTR url)
  14. {
  15. m_url = url;
  16. }
  17. char* mystrcpy(char* dst, const char* src)
  18. {
  19. assert(dst != NULL);
  20. assert(src != NULL);
  21. if (dst == src)
  22. return dst;
  23. int size = strlen(src) + 1;
  24. if (dst < src || src + size <= dst)
  25. {
  26. char* d = dst;
  27. const char* s = src;
  28. while (size--)
  29. *d++ = *s++;
  30. }
  31. else
  32. {
  33. char* d = dst + size - 1;
  34. const char* s = src + size - 1;
  35. while (size--)
  36. *d-- = *s--;
  37. }
  38. return dst;
  39. }
  40. HTTPRET CNetwork::PostLogin(LPCSTR username, LPCSTR password, LPCSTR& data)
  41. {
  42. CUtils cutils;
  43. //请求
  44. std::vector<cpr::Pair> p;
  45. p.push_back({ "email",username });
  46. p.push_back({ "password",password });
  47. std::string text = PostUrl("/api/client/v2/login", p);
  48. if (text.empty()) {
  49. return http_f;
  50. }
  51. data = text.c_str();
  52. //USERINFO userinf;
  53. ////memset(&userinf, NULL, sizeof(USERINFO));
  54. ////解析
  55. //try
  56. //{
  57. // nlohmann::json j = nlohmann::json::parse(data.begin(), data.end());
  58. // if (j["ret"].get<int>() == 1)
  59. // {
  60. //
  61. // auto userinfo = j["data"]["user"];
  62. //
  63. // userinf.id = userinfo.at("id").get<int>();
  64. // /*auto username = S_CA2W(userinfo.at("account").get<std::string>(), CP_UTF8);
  65. // memset(userinf.username, NULL, sizeof(wchar_t));*/
  66. // mystrcpy(userinf.username, userinfo.at("account").get<std::string>().c_str());
  67. // mystrcpy(userinf.password, userinfo.at("passwd").get<std::string>().c_str());
  68. // mystrcpy(userinf.expiretime, userinfo.at("expired_at").get<std::string>().c_str());
  69. // mystrcpy(userinf.unusedTraffic, userinfo.at("unusedTraffic").get<std::string>().c_str());
  70. // mystrcpy(userinf.uuid, userinfo.at("uuid").get<std::string>().c_str());
  71. // mystrcpy(userinf.level, userinfo.at("level").get<std::string>().c_str());
  72. //
  73. // //strcpy(userinf.port, userinfo.at("port").get<std::string>().c_str());
  74. // userinf.port = userinfo.at("port").get<int>();
  75. // //lstrcpynW(username.c_str(), userinf.username, username.length());
  76. // //std::copy(username.begin(), username.end(), userinf.username);
  77. //
  78. // /*userinf.username = userinfo.at("account").get<std::string>();
  79. // userinf.expiretime = userinfo.at("expired_at").get<std::string>();
  80. // userinf.password = userinfo.at("passwd").get<std::string>();
  81. // userinf.unusedTraffic = userinfo.at("unusedTraffic").get<std::string>();
  82. // userinf.uuid = userinfo.at("uuid").get<std::string>();
  83. // userinf.level = userinfo.at("level").get<std::string>();
  84. // userinf.port = userinfo.at("port").get<int>();*/
  85. //
  86. // mystrcpy(userinf.access_token, j["data"]["access_token"].get<std::string>().c_str());
  87. //
  88. // return &userinf;
  89. // }
  90. // else
  91. // {
  92. // wchar_t* msg = NULL;
  93. // m_error_msg = S_CA2A(j["msg"].get<std::string>(),CP_UTF8);
  94. // return NULL;
  95. // }
  96. //}
  97. //catch (...)
  98. //{
  99. // m_error_msg = "解析登录数据错误,未知错误";
  100. // return NULL;
  101. //}
  102. /*char* p = data.c_str();*/
  103. return http_yes;
  104. }
  105. //void CNetwork::GetServerlist()
  106. //{
  107. // keymap.clear();
  108. // std::string path = "v1/nodeproess";
  109. // m_http_ret = http_start;
  110. // auto data = GetUrl(path, keymap);
  111. // if (data.empty())
  112. // {
  113. // m_http_ret = http_f;
  114. // m_error_msg = "";
  115. // m_error_msg.append("请求错误:网络问题,错误码:").append(std::to_string(m_http_status));
  116. // return;
  117. // }
  118. // //解析数据
  119. // /*if (!m_server_list)
  120. // {
  121. // m_error_msg = "server_list为空";
  122. // return;
  123. // }
  124. //
  125. //
  126. // if (!m_server_list->parse(data))
  127. // {
  128. // m_error_msg = m_server_list->GetLastErrorA();
  129. // m_http_ret = http_f;
  130. // return;
  131. // }*/
  132. //
  133. //
  134. // m_http_ret = http_end;
  135. //}
  136. void CNetwork::Init() {
  137. }
  138. void CNetwork::UnInit()
  139. {
  140. /*if (m_server_list)
  141. {
  142. delete m_server_list;
  143. m_server_list = NULL;
  144. }
  145. if (m_userinfo)
  146. {
  147. delete m_userinfo;
  148. m_userinfo = NULL;
  149. }*/
  150. }
  151. HTTPRET CNetwork::GetHttpConnectstatus()
  152. {
  153. return m_http_ret;
  154. }
  155. LPCSTR CNetwork::GetLastErrorA()
  156. {
  157. return m_error_msg.c_str();
  158. }
  159. LPCWSTR CNetwork::GetLastErrorW()
  160. {
  161. return S_CA2W(m_error_msg,CP_UTF8).c_str();
  162. }
  163. std::string CNetwork::GetUrl(std::string path, std::vector<cpr::Parameter> parame)
  164. {
  165. cpr::Parameters p;
  166. for (auto it = parame.begin(); it != parame.end(); ++it) {
  167. //auto s = fmt::format("{0}", it->first);
  168. /*p.AddParameter({ it->first, it->second }, {});*/
  169. p.Add(*it);
  170. }
  171. for (auto it = vectorBaseurl.begin(); it != vectorBaseurl.end(); ++it) {
  172. auto s = fmt::format("{0}/{1}", *it, path.c_str());
  173. cpr::Response r;
  174. if (parame.empty())
  175. {
  176. r = cpr::Get(cpr::Url{ s.c_str() });
  177. }
  178. else {
  179. r = cpr::Get(cpr::Url{ s.c_str() }, p,cpr::Header{ {"accept", "application/json"} });
  180. }
  181. if (r.status_code == 200 || r.status_code == 201)
  182. {
  183. return r.text;
  184. }
  185. else {
  186. m_http_status = r.status_code;
  187. return "";
  188. }
  189. }
  190. return std::string("");
  191. }
  192. std::string CNetwork::PostUrl(std::string path, std::vector<cpr::Pair> parame)
  193. {
  194. auto s = fmt::format("{0}{1}", m_url, path.c_str());
  195. cpr::Response r;
  196. if (parame.empty())
  197. {
  198. r = cpr::Get(cpr::Url{ s.c_str() });
  199. }
  200. else {
  201. r = cpr::Post(cpr::Url{ s.c_str() }, cpr::Payload{ parame.begin(),parame.end() }, cpr::Header{ {"accept", "application/json"} });
  202. }
  203. if (r.status_code == 200 || r.status_code == 201)
  204. {
  205. return std::move(r.text);
  206. }
  207. else {
  208. m_http_status = r.status_code;
  209. if (m_http_status == 0)
  210. {
  211. m_error_msg = "获取数据失败,可能是您的网络问题更换备用尝试";
  212. }
  213. return "";
  214. }
  215. return std::string("");
  216. }