|
|
@@ -1,4 +1,5 @@
|
|
|
#include "networkaccessmanager.h"
|
|
|
+#include "../config/networkconfig.h"
|
|
|
#include "appevent.h"
|
|
|
|
|
|
#include <QBuffer>
|
|
|
@@ -12,11 +13,6 @@
|
|
|
#include <QUrlQuery>
|
|
|
#include <QtConcurrent>
|
|
|
|
|
|
-// 基础URL配置
|
|
|
-static QString base_url("http://106.55.186.74:8200");
|
|
|
-
|
|
|
-// static QString base_url("http://127.0.0.1:8200");
|
|
|
-
|
|
|
static const QString messageVal = "msg";
|
|
|
|
|
|
static NetworkAccessManager* namInstance = nullptr;
|
|
|
@@ -143,13 +139,13 @@ RequestClient::Ptr RequestClient::create(const QString& baseURL, const RequestCl
|
|
|
|
|
|
RequestClient::Ptr RequestClient::globalInstance()
|
|
|
{
|
|
|
- static RequestClient::Ptr instance = create(base_url, {"", true});
|
|
|
+ static RequestClient::Ptr instance = create(NetworkConfigInstance.getApiBaseUrl(), {"", true});
|
|
|
return instance;
|
|
|
}
|
|
|
|
|
|
RequestClient::Ptr RequestClient::baseGlobalInstance()
|
|
|
{
|
|
|
- static RequestClient::Ptr instance = create(base_url, {"", false});
|
|
|
+ static RequestClient::Ptr instance = create(NetworkConfigInstance.getApiBaseUrl(), {"", false});
|
|
|
return instance;
|
|
|
}
|
|
|
|
|
|
@@ -243,17 +239,20 @@ QFuture<HttpResponse> RequestClient::getAsync(const QString& url, const QVariant
|
|
|
{
|
|
|
auto promise = std::make_shared<QPromise<HttpResponse>>();
|
|
|
auto future = promise->future();
|
|
|
-
|
|
|
- QMetaObject::invokeMethod(QCoreApplication::instance(), [this, url, params, promise]() {
|
|
|
- try {
|
|
|
- auto result = get(url, params);
|
|
|
- promise->addResult(result);
|
|
|
- promise->finish();
|
|
|
- } catch (...) {
|
|
|
- promise->finish();
|
|
|
- }
|
|
|
- }, Qt::QueuedConnection);
|
|
|
-
|
|
|
+
|
|
|
+ QMetaObject::invokeMethod(
|
|
|
+ QCoreApplication::instance(),
|
|
|
+ [this, url, params, promise]() {
|
|
|
+ try {
|
|
|
+ auto result = get(url, params);
|
|
|
+ promise->addResult(result);
|
|
|
+ promise->finish();
|
|
|
+ } catch (...) {
|
|
|
+ promise->finish();
|
|
|
+ }
|
|
|
+ },
|
|
|
+ Qt::QueuedConnection);
|
|
|
+
|
|
|
return future;
|
|
|
}
|
|
|
|
|
|
@@ -261,17 +260,20 @@ QFuture<HttpResponse> RequestClient::postAsync(const QString& url, const QJsonDo
|
|
|
{
|
|
|
auto promise = std::make_shared<QPromise<HttpResponse>>();
|
|
|
auto future = promise->future();
|
|
|
-
|
|
|
- QMetaObject::invokeMethod(QCoreApplication::instance(), [this, url, data, promise]() {
|
|
|
- try {
|
|
|
- auto result = post(url, data);
|
|
|
- promise->addResult(result);
|
|
|
- promise->finish();
|
|
|
- } catch (...) {
|
|
|
- promise->finish();
|
|
|
- }
|
|
|
- }, Qt::QueuedConnection);
|
|
|
-
|
|
|
+
|
|
|
+ QMetaObject::invokeMethod(
|
|
|
+ QCoreApplication::instance(),
|
|
|
+ [this, url, data, promise]() {
|
|
|
+ try {
|
|
|
+ auto result = post(url, data);
|
|
|
+ promise->addResult(result);
|
|
|
+ promise->finish();
|
|
|
+ } catch (...) {
|
|
|
+ promise->finish();
|
|
|
+ }
|
|
|
+ },
|
|
|
+ Qt::QueuedConnection);
|
|
|
+
|
|
|
return future;
|
|
|
}
|
|
|
|
|
|
@@ -279,17 +281,20 @@ QFuture<HttpResponse> RequestClient::putAsync(const QString& url, const QJsonDoc
|
|
|
{
|
|
|
auto promise = std::make_shared<QPromise<HttpResponse>>();
|
|
|
auto future = promise->future();
|
|
|
-
|
|
|
- QMetaObject::invokeMethod(QCoreApplication::instance(), [this, url, data, promise]() {
|
|
|
- try {
|
|
|
- auto result = put(url, data);
|
|
|
- promise->addResult(result);
|
|
|
- promise->finish();
|
|
|
- } catch (...) {
|
|
|
- promise->finish();
|
|
|
- }
|
|
|
- }, Qt::QueuedConnection);
|
|
|
-
|
|
|
+
|
|
|
+ QMetaObject::invokeMethod(
|
|
|
+ QCoreApplication::instance(),
|
|
|
+ [this, url, data, promise]() {
|
|
|
+ try {
|
|
|
+ auto result = put(url, data);
|
|
|
+ promise->addResult(result);
|
|
|
+ promise->finish();
|
|
|
+ } catch (...) {
|
|
|
+ promise->finish();
|
|
|
+ }
|
|
|
+ },
|
|
|
+ Qt::QueuedConnection);
|
|
|
+
|
|
|
return future;
|
|
|
}
|
|
|
|
|
|
@@ -297,17 +302,20 @@ QFuture<HttpResponse> RequestClient::deleteAsync(const QString& url)
|
|
|
{
|
|
|
auto promise = std::make_shared<QPromise<HttpResponse>>();
|
|
|
auto future = promise->future();
|
|
|
-
|
|
|
- QMetaObject::invokeMethod(QCoreApplication::instance(), [this, url, promise]() {
|
|
|
- try {
|
|
|
- auto result = deleteResource(url);
|
|
|
- promise->addResult(result);
|
|
|
- promise->finish();
|
|
|
- } catch (...) {
|
|
|
- promise->finish();
|
|
|
- }
|
|
|
- }, Qt::QueuedConnection);
|
|
|
-
|
|
|
+
|
|
|
+ QMetaObject::invokeMethod(
|
|
|
+ QCoreApplication::instance(),
|
|
|
+ [this, url, promise]() {
|
|
|
+ try {
|
|
|
+ auto result = deleteResource(url);
|
|
|
+ promise->addResult(result);
|
|
|
+ promise->finish();
|
|
|
+ } catch (...) {
|
|
|
+ promise->finish();
|
|
|
+ }
|
|
|
+ },
|
|
|
+ Qt::QueuedConnection);
|
|
|
+
|
|
|
return future;
|
|
|
}
|
|
|
|
|
|
@@ -315,17 +323,20 @@ QFuture<HttpResponse> RequestClient::uploadAsync(const QString& url, QHttpMultiP
|
|
|
{
|
|
|
auto promise = std::make_shared<QPromise<HttpResponse>>();
|
|
|
auto future = promise->future();
|
|
|
-
|
|
|
- QMetaObject::invokeMethod(QCoreApplication::instance(), [this, url, multiPart, promise]() {
|
|
|
- try {
|
|
|
- auto result = upload(url, multiPart);
|
|
|
- promise->addResult(result);
|
|
|
- promise->finish();
|
|
|
- } catch (...) {
|
|
|
- promise->finish();
|
|
|
- }
|
|
|
- }, Qt::QueuedConnection);
|
|
|
-
|
|
|
+
|
|
|
+ QMetaObject::invokeMethod(
|
|
|
+ QCoreApplication::instance(),
|
|
|
+ [this, url, multiPart, promise]() {
|
|
|
+ try {
|
|
|
+ auto result = upload(url, multiPart);
|
|
|
+ promise->addResult(result);
|
|
|
+ promise->finish();
|
|
|
+ } catch (...) {
|
|
|
+ promise->finish();
|
|
|
+ }
|
|
|
+ },
|
|
|
+ Qt::QueuedConnection);
|
|
|
+
|
|
|
return future;
|
|
|
}
|
|
|
|
|
|
@@ -354,17 +365,20 @@ QFuture<bool> RequestClient::downloadAsync(const QString& url, const QString& sa
|
|
|
{
|
|
|
auto promise = std::make_shared<QPromise<bool>>();
|
|
|
auto future = promise->future();
|
|
|
-
|
|
|
- QMetaObject::invokeMethod(QCoreApplication::instance(), [this, url, saveFilePath, promise]() {
|
|
|
- try {
|
|
|
- auto result = download(url, saveFilePath);
|
|
|
- promise->addResult(result);
|
|
|
- promise->finish();
|
|
|
- } catch (...) {
|
|
|
- promise->finish();
|
|
|
- }
|
|
|
- }, Qt::QueuedConnection);
|
|
|
-
|
|
|
+
|
|
|
+ QMetaObject::invokeMethod(
|
|
|
+ QCoreApplication::instance(),
|
|
|
+ [this, url, saveFilePath, promise]() {
|
|
|
+ try {
|
|
|
+ auto result = download(url, saveFilePath);
|
|
|
+ promise->addResult(result);
|
|
|
+ promise->finish();
|
|
|
+ } catch (...) {
|
|
|
+ promise->finish();
|
|
|
+ }
|
|
|
+ },
|
|
|
+ Qt::QueuedConnection);
|
|
|
+
|
|
|
return future;
|
|
|
}
|
|
|
|
|
|
@@ -372,7 +386,7 @@ QNetworkReply* RequestClient::sendGetRequest(const QString& url, const QVariantM
|
|
|
{
|
|
|
m_networkManager->setTransferTimeout(m_timeout);
|
|
|
|
|
|
- QString base = m_baseUrl.isEmpty() ? base_url : m_baseUrl;
|
|
|
+ QString base = m_baseUrl.isEmpty() ? NetworkConfig::instance().getApiBaseUrl() : m_baseUrl;
|
|
|
QUrl fullUrl(base + url);
|
|
|
|
|
|
if (!params.isEmpty()) {
|
|
|
@@ -395,7 +409,7 @@ QNetworkReply* RequestClient::sendPostRequest(const QString& url, const QJsonDoc
|
|
|
{
|
|
|
m_networkManager->setTransferTimeout(m_timeout);
|
|
|
|
|
|
- QString base = m_baseUrl.isEmpty() ? base_url : m_baseUrl;
|
|
|
+ QString base = m_baseUrl.isEmpty() ? NetworkConfig::instance().getApiBaseUrl() : m_baseUrl;
|
|
|
QUrl fullUrl(base + url);
|
|
|
|
|
|
QBuffer* buffer = new QBuffer;
|
|
|
@@ -417,7 +431,7 @@ QNetworkReply* RequestClient::sendPutRequest(const QString& url, const QJsonDocu
|
|
|
{
|
|
|
m_networkManager->setTransferTimeout(m_timeout);
|
|
|
|
|
|
- QString base = m_baseUrl.isEmpty() ? base_url : m_baseUrl;
|
|
|
+ QString base = m_baseUrl.isEmpty() ? NetworkConfig::instance().getApiBaseUrl() : m_baseUrl;
|
|
|
QUrl fullUrl(base + url);
|
|
|
|
|
|
QBuffer* buffer = new QBuffer;
|
|
|
@@ -438,7 +452,7 @@ QNetworkReply* RequestClient::sendDeleteRequest(const QString& url)
|
|
|
{
|
|
|
m_networkManager->setTransferTimeout(m_timeout);
|
|
|
|
|
|
- QString base = m_baseUrl.isEmpty() ? base_url : m_baseUrl;
|
|
|
+ QString base = m_baseUrl.isEmpty() ? NetworkConfig::instance().getApiBaseUrl() : m_baseUrl;
|
|
|
QUrl fullUrl(base + url);
|
|
|
|
|
|
QNetworkRequest request(fullUrl);
|
|
|
@@ -452,7 +466,7 @@ QNetworkReply* RequestClient::sendUploadRequest(const QString& url, QHttpMultiPa
|
|
|
{
|
|
|
m_networkManager->setTransferTimeout(m_timeout);
|
|
|
|
|
|
- QString base = m_baseUrl.isEmpty() ? base_url : m_baseUrl;
|
|
|
+ QString base = m_baseUrl.isEmpty() ? NetworkConfig::instance().getApiBaseUrl() : m_baseUrl;
|
|
|
QUrl fullUrl(base + url);
|
|
|
|
|
|
QNetworkRequest request(fullUrl);
|