|
@@ -1,6 +1,8 @@
|
|
#include "pch.h"
|
|
#include "pch.h"
|
|
#include "CNetwork.h"
|
|
#include "CNetwork.h"
|
|
#include <nlohmann/json.hpp>
|
|
#include <nlohmann/json.hpp>
|
|
|
|
+#include "CUtils.h"
|
|
|
|
+
|
|
CNetwork::CNetwork() : m_http_ret(http_no), m_http_status(0)
|
|
CNetwork::CNetwork() : m_http_ret(http_no), m_http_status(0)
|
|
{
|
|
{
|
|
Init();
|
|
Init();
|
|
@@ -11,6 +13,126 @@ CNetwork::~CNetwork(void)
|
|
UnInit();
|
|
UnInit();
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+void CNetwork::SetUrl(LPCSTR url)
|
|
|
|
+{
|
|
|
|
+ m_url = url;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+char* mystrcpy(char* dst, const char* src)
|
|
|
|
+{
|
|
|
|
+ assert(dst != NULL);
|
|
|
|
+ assert(src != NULL);
|
|
|
|
+
|
|
|
|
+ if (dst == src)
|
|
|
|
+ return dst;
|
|
|
|
+
|
|
|
|
+ int size = strlen(src) + 1;
|
|
|
|
+
|
|
|
|
+ if (dst < src || src + size <= dst)
|
|
|
|
+ {
|
|
|
|
+ char* d = dst;
|
|
|
|
+ const char* s = src;
|
|
|
|
+
|
|
|
|
+ while (size--)
|
|
|
|
+ *d++ = *s++;
|
|
|
|
+ }
|
|
|
|
+ else
|
|
|
|
+ {
|
|
|
|
+ char* d = dst + size - 1;
|
|
|
|
+ const char* s = src + size - 1;
|
|
|
|
+
|
|
|
|
+ while (size--)
|
|
|
|
+ *d-- = *s--;
|
|
|
|
+ }
|
|
|
|
+ return dst;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+HTTPRET CNetwork::PostLogin(LPCSTR username, LPCSTR password, LPCSTR& data)
|
|
|
|
+{
|
|
|
|
+ CUtils cutils;
|
|
|
|
+ //请求
|
|
|
|
+ std::vector<cpr::Pair> p;
|
|
|
|
+ p.push_back({ "email",username });
|
|
|
|
+ p.push_back({ "password",password });
|
|
|
|
+ std::string text = PostUrl("/api/client/v2/login", p);
|
|
|
|
+ if (text.empty()) {
|
|
|
|
+
|
|
|
|
+ return http_f;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ data = text.c_str();
|
|
|
|
+ //USERINFO userinf;
|
|
|
|
+ ////memset(&userinf, NULL, sizeof(USERINFO));
|
|
|
|
+ ////解析
|
|
|
|
+ //try
|
|
|
|
+ //{
|
|
|
|
+ // nlohmann::json j = nlohmann::json::parse(data.begin(), data.end());
|
|
|
|
+
|
|
|
|
+ // if (j["ret"].get<int>() == 1)
|
|
|
|
+ // {
|
|
|
|
+ //
|
|
|
|
+ // auto userinfo = j["data"]["user"];
|
|
|
|
+ //
|
|
|
|
+ // userinf.id = userinfo.at("id").get<int>();
|
|
|
|
+
|
|
|
|
+ // /*auto username = S_CA2W(userinfo.at("account").get<std::string>(), CP_UTF8);
|
|
|
|
+
|
|
|
|
+ // memset(userinf.username, NULL, sizeof(wchar_t));*/
|
|
|
|
+ // mystrcpy(userinf.username, userinfo.at("account").get<std::string>().c_str());
|
|
|
|
+ // mystrcpy(userinf.password, userinfo.at("passwd").get<std::string>().c_str());
|
|
|
|
+ // mystrcpy(userinf.expiretime, userinfo.at("expired_at").get<std::string>().c_str());
|
|
|
|
+ // mystrcpy(userinf.unusedTraffic, userinfo.at("unusedTraffic").get<std::string>().c_str());
|
|
|
|
+ // mystrcpy(userinf.uuid, userinfo.at("uuid").get<std::string>().c_str());
|
|
|
|
+ // mystrcpy(userinf.level, userinfo.at("level").get<std::string>().c_str());
|
|
|
|
+ //
|
|
|
|
+ // //strcpy(userinf.port, userinfo.at("port").get<std::string>().c_str());
|
|
|
|
+
|
|
|
|
+ // userinf.port = userinfo.at("port").get<int>();
|
|
|
|
+
|
|
|
|
+ // //lstrcpynW(username.c_str(), userinf.username, username.length());
|
|
|
|
+ // //std::copy(username.begin(), username.end(), userinf.username);
|
|
|
|
+ //
|
|
|
|
+ // /*userinf.username = userinfo.at("account").get<std::string>();
|
|
|
|
+ // userinf.expiretime = userinfo.at("expired_at").get<std::string>();
|
|
|
|
+ // userinf.password = userinfo.at("passwd").get<std::string>();
|
|
|
|
+ // userinf.unusedTraffic = userinfo.at("unusedTraffic").get<std::string>();
|
|
|
|
+ // userinf.uuid = userinfo.at("uuid").get<std::string>();
|
|
|
|
+ // userinf.level = userinfo.at("level").get<std::string>();
|
|
|
|
+ // userinf.port = userinfo.at("port").get<int>();*/
|
|
|
|
+
|
|
|
|
+ //
|
|
|
|
+ // mystrcpy(userinf.access_token, j["data"]["access_token"].get<std::string>().c_str());
|
|
|
|
+
|
|
|
|
+ //
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ // return &userinf;
|
|
|
|
+
|
|
|
|
+ // }
|
|
|
|
+ // else
|
|
|
|
+ // {
|
|
|
|
+ // wchar_t* msg = NULL;
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ // m_error_msg = S_CA2A(j["msg"].get<std::string>(),CP_UTF8);
|
|
|
|
+ // return NULL;
|
|
|
|
+ // }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ //}
|
|
|
|
+ //catch (...)
|
|
|
|
+ //{
|
|
|
|
+ // m_error_msg = "解析登录数据错误,未知错误";
|
|
|
|
+ // return NULL;
|
|
|
|
+ //}
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ /*char* p = data.c_str();*/
|
|
|
|
+ return http_yes;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
|
|
|
|
|
|
//void CNetwork::GetServerlist()
|
|
//void CNetwork::GetServerlist()
|
|
@@ -81,9 +203,14 @@ LPCSTR CNetwork::GetLastErrorA()
|
|
return m_error_msg.c_str();
|
|
return m_error_msg.c_str();
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+LPCWSTR CNetwork::GetLastErrorW()
|
|
|
|
+{
|
|
|
|
+ return S_CA2W(m_error_msg,CP_UTF8).c_str();
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
|
|
|
|
|
|
-std::string CNetwork::GetUrl(std::string path,std::unordered_map<std::string, std::string> parame)
|
|
|
|
|
|
+std::string CNetwork::GetUrl(std::string path, std::vector<cpr::Parameter> parame)
|
|
{
|
|
{
|
|
|
|
|
|
|
|
|
|
@@ -92,6 +219,7 @@ std::string CNetwork::GetUrl(std::string path,std::unordered_map<std::string, st
|
|
for (auto it = parame.begin(); it != parame.end(); ++it) {
|
|
for (auto it = parame.begin(); it != parame.end(); ++it) {
|
|
//auto s = fmt::format("{0}", it->first);
|
|
//auto s = fmt::format("{0}", it->first);
|
|
/*p.AddParameter({ it->first, it->second }, {});*/
|
|
/*p.AddParameter({ it->first, it->second }, {});*/
|
|
|
|
+ p.Add(*it);
|
|
}
|
|
}
|
|
|
|
|
|
for (auto it = vectorBaseurl.begin(); it != vectorBaseurl.end(); ++it) {
|
|
for (auto it = vectorBaseurl.begin(); it != vectorBaseurl.end(); ++it) {
|
|
@@ -123,39 +251,37 @@ std::string CNetwork::GetUrl(std::string path,std::unordered_map<std::string, st
|
|
return std::string("");
|
|
return std::string("");
|
|
}
|
|
}
|
|
|
|
|
|
-std::string CNetwork::PostUrl(std::string path,std::unordered_map<std::string, std::string> parame, std::string data)
|
|
|
|
-{
|
|
|
|
- std::vector<cpr::Pair> p;
|
|
|
|
- for (auto it = parame.begin(); it != parame.end(); ++it) {
|
|
|
|
- //auto s = fmt::format("{0}", it->first);
|
|
|
|
- //p.AddPair(cpr::Pair{ it->first,it->second }, {});
|
|
|
|
- p.push_back({ it->first,it->second });
|
|
|
|
|
|
+std::string CNetwork::PostUrl(std::string path, std::vector<cpr::Pair> parame)
|
|
|
|
+{
|
|
|
|
|
|
- }
|
|
|
|
|
|
+ auto s = fmt::format("{0}{1}", m_url, path.c_str());
|
|
|
|
|
|
- for (auto it = vectorBaseurl.begin(); it != vectorBaseurl.end(); ++it) {
|
|
|
|
- auto s = fmt::format("{0}/{1}", *it, path.c_str());
|
|
|
|
|
|
+ cpr::Response r;
|
|
|
|
|
|
- cpr::Response r;
|
|
|
|
|
|
+ if (parame.empty())
|
|
|
|
+ {
|
|
|
|
+ r = cpr::Get(cpr::Url{ s.c_str() });
|
|
|
|
+ }
|
|
|
|
+ else {
|
|
|
|
+ r = cpr::Post(cpr::Url{ s.c_str() }, cpr::Payload{ parame.begin(),parame.end() }, cpr::Header{ {"accept", "application/json"} });
|
|
|
|
+ }
|
|
|
|
|
|
- if (parame.empty())
|
|
|
|
|
|
+ if (r.status_code == 200 || r.status_code == 201)
|
|
|
|
+ {
|
|
|
|
+ return std::move(r.text);
|
|
|
|
+ }
|
|
|
|
+ else {
|
|
|
|
+ m_http_status = r.status_code;
|
|
|
|
+ if (m_http_status == 0)
|
|
{
|
|
{
|
|
- r = cpr::Get(cpr::Url{ s.c_str() });
|
|
|
|
- }
|
|
|
|
- else {
|
|
|
|
- r = cpr::Post(cpr::Url{ s.c_str() }, cpr::Payload{ p.begin(),p.end() }, cpr::Header{ {"accept", "application/json"} });
|
|
|
|
|
|
+ m_error_msg = "获取数据失败,可能是您的网络问题更换备用尝试";
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ return "";
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
|
|
- if (r.status_code == 200 || r.status_code == 201)
|
|
|
|
- {
|
|
|
|
- return std::move(r.text);
|
|
|
|
- }
|
|
|
|
- else {
|
|
|
|
- m_http_status = r.status_code;
|
|
|
|
- return "";
|
|
|
|
- }
|
|
|
|
|
|
|
|
- }
|
|
|
|
return std::string("");
|
|
return std::string("");
|
|
}
|
|
}
|
|
|
|
|