346 lines
11 KiB
C++
346 lines
11 KiB
C++
#include <iostream>
|
|
#include <list>
|
|
#include <mutex>
|
|
|
|
#include "post_helper.h"
|
|
#include "util_helper.h"
|
|
#include "app_config.h"
|
|
#include "nanolog.h"
|
|
#include <Shlwapi.h>
|
|
|
|
#pragma comment(lib, "libcurl.lib")
|
|
|
|
static size_t ret_data(void* buffer, size_t size, size_t nmemb, void* userp)
|
|
{
|
|
string* ststr = (string*)userp;
|
|
ststr->append((char*)buffer, size * nmemb);
|
|
return nmemb;
|
|
}
|
|
|
|
size_t write_callback(char* ptr, size_t size, size_t nmemb, void* userdata)
|
|
{
|
|
FILE* fp = static_cast<FILE*>(userdata);
|
|
if (!fp) return 0;
|
|
|
|
size_t length = fwrite(ptr, size, nmemb, fp);
|
|
if (length != nmemb) {
|
|
return length;
|
|
}
|
|
|
|
return size * nmemb;
|
|
}
|
|
|
|
bool PostHelper::PostLogin(const std::string& name, const std::string& passwd)
|
|
{
|
|
string strRes;
|
|
struct curl_slist* chunk = NULL;
|
|
|
|
//设置url
|
|
string strUrl = AppConfig::Instance()->DataServerUrl + "/business/system/auth/login";
|
|
curl_easy_setopt(curl_, CURLOPT_URL, strUrl.c_str());
|
|
|
|
curl_easy_setopt(curl_, CURLOPT_SSL_VERIFYPEER, false); //忽略服务器验证
|
|
curl_easy_setopt(curl_, CURLOPT_SSL_VERIFYHOST, false);
|
|
curl_easy_setopt(curl_, CURLOPT_HEADER, TRUE);
|
|
curl_easy_setopt(curl_, CURLOPT_WRITEFUNCTION, ret_data);
|
|
|
|
struct curl_slist* headers = NULL;
|
|
headers = curl_slist_append(headers, "Content-Type:application/json;charset=utf-8");
|
|
curl_easy_setopt(curl_, CURLOPT_POST, 1);
|
|
|
|
Json::Value root;
|
|
root["username"] = name;
|
|
root["password"] = passwd;
|
|
Json::StreamWriterBuilder builder;
|
|
const std::string strLoginPost = Json::writeString(builder, root);
|
|
curl_easy_setopt(curl_, CURLOPT_POSTFIELDS, strLoginPost.c_str());
|
|
|
|
curl_easy_setopt(curl_, CURLOPT_HTTPHEADER, headers);
|
|
curl_easy_setopt(curl_, CURLOPT_TIMEOUT, 2);
|
|
curl_easy_setopt(curl_, CURLOPT_WRITEDATA, &strRes);
|
|
|
|
// 执行一次URL请求
|
|
CURLcode res = curl_easy_perform(curl_);
|
|
curl_slist_free_all(headers);
|
|
|
|
if (!strRes.empty())
|
|
{
|
|
strRes = strRes.substr(strRes.find('{'), strRes.size() - 1);
|
|
|
|
bool res;
|
|
JSONCPP_STRING errs;
|
|
Json::Value root;
|
|
Json::CharReaderBuilder readerBuilder;
|
|
|
|
std::unique_ptr<Json::CharReader> const jsonReader(readerBuilder.newCharReader());
|
|
res = jsonReader->parse(strRes.c_str(), strRes.c_str() + strRes.length(), &root, &errs);
|
|
if (!res || !errs.empty()) {
|
|
std::cout << "parseJson err. " << errs << std::endl;
|
|
return false;
|
|
}
|
|
|
|
if (root["code"].asInt() == 200) {
|
|
token_ = root["data"]["accessToken"].asString();
|
|
refresh_token_ = root["data"]["refreshToken"].asString();
|
|
}
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
bool PostHelper::RefreshToken() {
|
|
std::lock_guard guard(mutex_);
|
|
string strRes;
|
|
|
|
//设置url
|
|
string strUrl = AppConfig::Instance()->DataServerUrl + "/business/system/auth/refresh-token";
|
|
curl_easy_setopt(curl_, CURLOPT_URL, strUrl.c_str());
|
|
|
|
curl_easy_setopt(curl_, CURLOPT_SSL_VERIFYPEER, false); //忽略服务器验证
|
|
curl_easy_setopt(curl_, CURLOPT_SSL_VERIFYHOST, false);
|
|
curl_easy_setopt(curl_, CURLOPT_HEADER, TRUE);
|
|
curl_easy_setopt(curl_, CURLOPT_WRITEFUNCTION, ret_data);
|
|
|
|
struct curl_slist* headers = NULL;
|
|
string headerToken = "GeomativeAuthorization:" + token_;
|
|
headers = curl_slist_append(headers, headerToken.c_str());
|
|
|
|
curl_easy_setopt(curl_, CURLOPT_POST, 1);
|
|
|
|
struct curl_httppost* formpost = NULL;
|
|
struct curl_httppost* lastptr = NULL;
|
|
curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "refreshToken", CURLFORM_COPYCONTENTS, PostHelper::refresh_token_.c_str(), CURLFORM_END);
|
|
curl_easy_setopt(curl_, CURLOPT_HTTPPOST, formpost);
|
|
|
|
curl_easy_setopt(curl_, CURLOPT_HTTPHEADER, headers);
|
|
curl_easy_setopt(curl_, CURLOPT_TIMEOUT, 2);
|
|
curl_easy_setopt(curl_, CURLOPT_WRITEDATA, &strRes);
|
|
|
|
// 执行一次URL请求
|
|
CURLcode res = curl_easy_perform(curl_);
|
|
curl_slist_free_all(headers);
|
|
|
|
if (!strRes.empty())
|
|
{
|
|
strRes = strRes.substr(strRes.find('{'), strRes.size() - 1);
|
|
|
|
bool res;
|
|
JSONCPP_STRING errs;
|
|
Json::Value root;
|
|
Json::CharReaderBuilder readerBuilder;
|
|
|
|
std::unique_ptr<Json::CharReader> const jsonReader(readerBuilder.newCharReader());
|
|
res = jsonReader->parse(strRes.c_str(), strRes.c_str() + strRes.length(), &root, &errs);
|
|
if (!res || !errs.empty()) {
|
|
std::cout << "parseJson err. " << errs << std::endl;
|
|
return false;
|
|
}
|
|
|
|
if (root["code"].asInt() == 200) {
|
|
token_ = root["data"]["accessToken"].asString();
|
|
refresh_token_ = root["data"]["refreshToken"].asString();
|
|
}
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
bool PostHelper::Get(const std::string& url, Json::Value& res) {
|
|
std::lock_guard guard(mutex_);
|
|
|
|
std::string strRes;
|
|
|
|
//设置url
|
|
curl_easy_setopt(curl_, CURLOPT_URL, url.c_str());
|
|
|
|
curl_easy_setopt(curl_, CURLOPT_SSL_VERIFYPEER, false); //忽略服务器验证
|
|
curl_easy_setopt(curl_, CURLOPT_SSL_VERIFYHOST, false);
|
|
curl_easy_setopt(curl_, CURLOPT_HEADER, TRUE);
|
|
curl_easy_setopt(curl_, CURLOPT_WRITEFUNCTION, ret_data);
|
|
|
|
struct curl_slist* headers = NULL;
|
|
headers = curl_slist_append(headers, "Content-Type:application/json;charset=utf-8");
|
|
string headerToken = "GeomativeAuthorization:" + token_;
|
|
headers = curl_slist_append(headers, headerToken.c_str());
|
|
|
|
curl_easy_setopt(curl_, CURLOPT_HTTPGET, 1L);
|
|
|
|
curl_easy_setopt(curl_, CURLOPT_HTTPHEADER, headers);
|
|
curl_easy_setopt(curl_, CURLOPT_TIMEOUT, 10);
|
|
curl_easy_setopt(curl_, CURLOPT_WRITEDATA, &strRes);
|
|
|
|
// 执行一次URL请求
|
|
curl_easy_perform(curl_);
|
|
curl_slist_free_all(headers);
|
|
|
|
if (!strRes.empty())
|
|
{
|
|
if (strRes.find('{') == string::npos)
|
|
return false;
|
|
strRes = strRes.substr(strRes.find('{'), strRes.size() - 1);
|
|
|
|
JSONCPP_STRING errs;
|
|
Json::CharReaderBuilder readerBuilder;
|
|
std::unique_ptr<Json::CharReader> const jsonReader(readerBuilder.newCharReader());
|
|
if (!jsonReader->parse(strRes.c_str(), strRes.c_str() + strRes.length(), &res, &errs) || !errs.empty()) {
|
|
std::cout << "parseJson err. " << errs << std::endl;
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
bool PostHelper::Get(const std::string& url, const std::filesystem::path& file_path) {
|
|
std::lock_guard guard(mutex_);
|
|
|
|
FILE* fp = fopen(file_path.string().c_str(), "wb");
|
|
if (!fp) return false;
|
|
|
|
//设置url
|
|
curl_easy_setopt(curl_, CURLOPT_URL, url.c_str());
|
|
|
|
curl_easy_setopt(curl_, CURLOPT_SSL_VERIFYPEER, false); //忽略服务器验证
|
|
curl_easy_setopt(curl_, CURLOPT_SSL_VERIFYHOST, false);
|
|
curl_easy_setopt(curl_, CURLOPT_HEADER, false);
|
|
curl_easy_setopt(curl_, CURLOPT_WRITEFUNCTION, write_callback);
|
|
|
|
struct curl_slist* headers = NULL;
|
|
string headerToken = "GeomativeAuthorization:" + token_;
|
|
headers = curl_slist_append(headers, headerToken.c_str());
|
|
|
|
curl_easy_setopt(curl_, CURLOPT_HTTPGET, 1L);
|
|
|
|
curl_easy_setopt(curl_, CURLOPT_HTTPHEADER, headers);
|
|
curl_easy_setopt(curl_, CURLOPT_TIMEOUT, 10);
|
|
curl_easy_setopt(curl_, CURLOPT_WRITEDATA, fp);
|
|
|
|
// 执行一次URL请求
|
|
CURLcode res = curl_easy_perform(curl_);
|
|
curl_slist_free_all(headers);
|
|
|
|
if (fp) fclose(fp);
|
|
return res == CURLE_OK;
|
|
}
|
|
|
|
bool PostHelper::Post(const std::string& url, const Json::Value& arg, Json::Value& res)
|
|
{
|
|
std::lock_guard guard(mutex_);
|
|
|
|
string strRes;
|
|
|
|
//设置url
|
|
curl_easy_setopt(curl_, CURLOPT_URL, url.c_str());
|
|
|
|
curl_easy_setopt(curl_, CURLOPT_SSL_VERIFYPEER, false); //忽略服务器验证
|
|
curl_easy_setopt(curl_, CURLOPT_SSL_VERIFYHOST, false);
|
|
curl_easy_setopt(curl_, CURLOPT_HEADER, TRUE);
|
|
curl_easy_setopt(curl_, CURLOPT_WRITEFUNCTION, ret_data);
|
|
|
|
struct curl_slist* headers = NULL;
|
|
headers = curl_slist_append(headers, "Expect:");
|
|
headers = curl_slist_append(headers, "Content-Type:application/json;charset=utf-8");
|
|
if (!token_.empty()) {
|
|
string headerToken = "GeomativeAuthorization:" + token_;
|
|
headers = curl_slist_append(headers, headerToken.c_str());
|
|
}
|
|
|
|
//struct curl_httppost* formpost = NULL;
|
|
//struct curl_httppost* lastptr = NULL;
|
|
//for (auto it = arg.begin(); it != arg.end(); it++) {
|
|
// Json::StreamWriterBuilder builder;
|
|
// std::string& content = it->type() < Json::ValueType::arrayValue ? it->asString() : Json::writeString(builder, *it);
|
|
// curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, it.name().c_str(), CURLFORM_COPYCONTENTS, content.c_str(), CURLFORM_END);
|
|
//}
|
|
//curl_easy_setopt(curl_, CURLOPT_HTTPPOST, formpost);
|
|
|
|
Json::StreamWriterBuilder builder;
|
|
const std::string strLoginPost = Json::writeString(builder, arg);
|
|
curl_easy_setopt(curl_, CURLOPT_POSTFIELDS, strLoginPost.c_str());
|
|
|
|
curl_easy_setopt(curl_, CURLOPT_HTTPHEADER, headers);
|
|
curl_easy_setopt(curl_, CURLOPT_TIMEOUT, 2);
|
|
curl_easy_setopt(curl_, CURLOPT_WRITEDATA, &strRes);
|
|
|
|
// 执行一次URL请求
|
|
CURLcode res1 = curl_easy_perform(curl_);
|
|
curl_slist_free_all(headers);
|
|
|
|
if (!strRes.empty())
|
|
{
|
|
strRes = strRes.substr(strRes.find('{'), strRes.size() - 1);
|
|
|
|
JSONCPP_STRING errs;
|
|
Json::CharReaderBuilder readerBuilder;
|
|
std::unique_ptr<Json::CharReader> const jsonReader(readerBuilder.newCharReader());
|
|
if (!jsonReader->parse(strRes.c_str(), strRes.c_str() + strRes.length(), &res, &errs) || !errs.empty()) {
|
|
std::cout << "parseJson err. " << errs << std::endl;
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
bool PostHelper::Post(const std::string& url, const Json::Value& arg, const std::string& filePath, Json::Value& res) {
|
|
string strRes;
|
|
|
|
//设置url
|
|
curl_easy_setopt(curl_, CURLOPT_URL, url.c_str());
|
|
|
|
curl_easy_setopt(curl_, CURLOPT_SSL_VERIFYPEER, false); //忽略服务器验证
|
|
curl_easy_setopt(curl_, CURLOPT_SSL_VERIFYHOST, false);
|
|
curl_easy_setopt(curl_, CURLOPT_HEADER, TRUE);
|
|
curl_easy_setopt(curl_, CURLOPT_WRITEFUNCTION, ret_data);
|
|
|
|
struct curl_slist* headers = NULL;
|
|
string headerToken = "GeomativeAuthorization:" + token_;
|
|
headers = curl_slist_append(headers, headerToken.c_str());
|
|
|
|
struct curl_httppost* formpost = NULL;
|
|
struct curl_httppost* lastptr = NULL;
|
|
for (auto& it = arg.begin(); it != arg.end(); it++) {
|
|
Json::StreamWriterBuilder builder;
|
|
std::string& content = it->type() < Json::ValueType::arrayValue ? it->asString() : Json::writeString(builder, *it);
|
|
curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, it.name().c_str(), CURLFORM_COPYCONTENTS, content.c_str(), CURLFORM_END);
|
|
}
|
|
if (!filePath.empty()) {
|
|
auto fileName = filePath.substr(filePath.find_last_of('\\') + 1, filePath.length());
|
|
curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "file", CURLFORM_FILENAME, fileName.c_str(), CURLFORM_FILE, filePath.c_str(), CURLFORM_END);
|
|
}
|
|
curl_easy_setopt(curl_, CURLOPT_HTTPPOST, formpost);
|
|
|
|
curl_easy_setopt(curl_, CURLOPT_HTTPHEADER, headers);
|
|
curl_easy_setopt(curl_, CURLOPT_TIMEOUT, 10);
|
|
curl_easy_setopt(curl_, CURLOPT_WRITEDATA, &strRes);
|
|
|
|
// 执行一次URL请求
|
|
curl_easy_perform(curl_);
|
|
curl_slist_free_all(headers);
|
|
|
|
if (!strRes.empty())
|
|
{
|
|
strRes = strRes.substr(strRes.find('{'), strRes.size() - 1);
|
|
|
|
JSONCPP_STRING errs;
|
|
Json::CharReaderBuilder readerBuilder;
|
|
std::unique_ptr<Json::CharReader> const jsonReader(readerBuilder.newCharReader());
|
|
if (!jsonReader->parse(strRes.c_str(), strRes.c_str() + strRes.length(), &res, &errs) || !errs.empty()) {
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
void PostHelper::init_curl() {
|
|
curl_ = curl_easy_init();
|
|
}
|
|
|
|
void PostHelper::exit_curl() {
|
|
if (nullptr != curl_) {
|
|
curl_easy_cleanup(curl_);
|
|
}
|
|
}
|