12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394 |
- #include "stdafx.h"
- #include "CNetWork.h"
- #include <nlohmann/json.hpp>
- #include <fmt/format.h>
- CNetWork::CNetWork() : m_http_ret(http_f)
- {
- }
- CNetWork::~CNetWork(void)
- {
- }
- HTTPRET CNetWork::PostLogin(LPCSTR username, LPCSTR password, std::string& data)
- {
- 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();
- return http_yes;
- }
- void CNetWork::SetUrl(LPCSTR url)
- {
- m_url = url;
- }
- LPCSTR CNetWork::GetLastErrorA()
- {
- return SStringA().Format("%s", m_error_msg.c_str()).GetBuffer(0);
- }
- LPCWSTR CNetWork::GetLastErrorW()
- {
- return S_CA2W(m_error_msg.c_str(),CP_UTF8);
- }
- std::string CNetWork::GetUrl(std::string path, std::vector<cpr::Parameter> parame)
- {
- return std::string();
- }
- std::string CNetWork::PostUrl(std::string path, std::vector<cpr::Pair> parame)
- {
- auto s = fmt::format("{0}{1}", m_url, path.c_str());
- 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 (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)
- {
- m_error_msg = "获取数据失败,可能是您的网络问题更换备用尝试";
- }
- return "";
- }
- return std::string("");
- }
- void CNetWork::Init()
- {
- }
- void CNetWork::UnInit()
- {
- }
|