mirror of
https://github.com/zhuzichu520/FluentUI.git
synced 2024-11-23 11:17:15 +08:00
update
This commit is contained in:
parent
d0e283747e
commit
db805ef85d
153
src/FluHttp.cpp
153
src/FluHttp.cpp
@ -5,12 +5,14 @@
|
|||||||
#include <QNetworkReply>
|
#include <QNetworkReply>
|
||||||
#include <QUrlQuery>
|
#include <QUrlQuery>
|
||||||
#include <QHttpMultiPart>
|
#include <QHttpMultiPart>
|
||||||
|
#include <QJsonDocument>
|
||||||
#include "FluApp.h"
|
#include "FluApp.h"
|
||||||
|
|
||||||
FluHttp::FluHttp(QObject *parent)
|
FluHttp::FluHttp(QObject *parent)
|
||||||
: QObject{parent}
|
: QObject{parent}
|
||||||
{
|
{
|
||||||
|
retry(3);
|
||||||
|
timeout(15000);
|
||||||
}
|
}
|
||||||
|
|
||||||
FluHttp::~FluHttp(){
|
FluHttp::~FluHttp(){
|
||||||
@ -33,12 +35,14 @@ void FluHttp::post(QVariantMap params,QVariantMap headers){
|
|||||||
QVariantMap data = invokeIntercept(params,headers,"post").toMap();
|
QVariantMap data = invokeIntercept(params,headers,"post").toMap();
|
||||||
QThreadPool::globalInstance()->start([=](){
|
QThreadPool::globalInstance()->start([=](){
|
||||||
Q_EMIT start();
|
Q_EMIT start();
|
||||||
|
for (int i = 0; i < retry(); ++i) {
|
||||||
QNetworkAccessManager manager;
|
QNetworkAccessManager manager;
|
||||||
|
manager.setTransferTimeout(timeout());
|
||||||
QUrl url(_url);
|
QUrl url(_url);
|
||||||
QNetworkRequest request(url);
|
QNetworkRequest request(url);
|
||||||
addHeaders(&request,data["headers"].toMap());
|
addHeaders(&request,data["headers"].toMap());
|
||||||
QHttpMultiPart* multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType);
|
QHttpMultiPart multiPart(QHttpMultiPart::FormDataType);
|
||||||
QString contentType = QString("multipart/form-data;boundary=%1").arg(multiPart->boundary());
|
QString contentType = QString("multipart/form-data;boundary=%1").arg(multiPart.boundary());
|
||||||
request.setHeader(QNetworkRequest::ContentTypeHeader, contentType);
|
request.setHeader(QNetworkRequest::ContentTypeHeader, contentType);
|
||||||
for (const auto& each : data["params"].toMap().toStdMap())
|
for (const auto& each : data["params"].toMap().toStdMap())
|
||||||
{
|
{
|
||||||
@ -48,19 +52,104 @@ void FluHttp::post(QVariantMap params,QVariantMap headers){
|
|||||||
QHttpPart part;
|
QHttpPart part;
|
||||||
part.setHeader(QNetworkRequest::ContentDispositionHeader, dispositionHeader);
|
part.setHeader(QNetworkRequest::ContentDispositionHeader, dispositionHeader);
|
||||||
part.setBody(value.toUtf8());
|
part.setBody(value.toUtf8());
|
||||||
multiPart->append(part);
|
multiPart.append(part);
|
||||||
}
|
}
|
||||||
QEventLoop loop;
|
QEventLoop loop;
|
||||||
QNetworkReply* reply = manager.post(request,multiPart);
|
QNetworkReply* reply = manager.post(request,&multiPart);
|
||||||
connect(reply, &QNetworkReply::finished, &loop, &QEventLoop::quit);
|
_cache.append(reply);
|
||||||
|
connect(&manager,&QNetworkAccessManager::finished,this,[&loop](QNetworkReply *reply){
|
||||||
|
loop.quit();
|
||||||
|
});
|
||||||
loop.exec();
|
loop.exec();
|
||||||
if (reply->error() == QNetworkReply::NoError) {
|
QString result = QString::fromUtf8(reply->readAll());
|
||||||
Q_EMIT success(QString::fromUtf8(reply->readAll()));
|
int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
|
||||||
}else{
|
QString errorString = reply->errorString();
|
||||||
Q_EMIT error(reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(),reply->errorString());
|
bool isSuccess = reply->error() == QNetworkReply::NoError;
|
||||||
}
|
|
||||||
reply->deleteLater();
|
reply->deleteLater();
|
||||||
reply = nullptr;
|
reply = nullptr;
|
||||||
|
if (isSuccess) {
|
||||||
|
Q_EMIT success(result);
|
||||||
|
break;
|
||||||
|
}else{
|
||||||
|
if(i == retry()-1){
|
||||||
|
Q_EMIT error(status,errorString);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Q_EMIT finish();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
void FluHttp::postString(QString params,QVariantMap headers){
|
||||||
|
QVariantMap data = invokeIntercept(params,headers,"postString").toMap();
|
||||||
|
QThreadPool::globalInstance()->start([=](){
|
||||||
|
Q_EMIT start();
|
||||||
|
for (int i = 0; i < retry(); ++i) {
|
||||||
|
QNetworkAccessManager manager;
|
||||||
|
manager.setTransferTimeout(timeout());
|
||||||
|
QUrl url(_url);
|
||||||
|
QNetworkRequest request(url);
|
||||||
|
addHeaders(&request,data["headers"].toMap());
|
||||||
|
QEventLoop loop;
|
||||||
|
QNetworkReply* reply = manager.post(request,params.toUtf8());
|
||||||
|
_cache.append(reply);
|
||||||
|
connect(&manager,&QNetworkAccessManager::finished,this,[&loop](QNetworkReply *reply){
|
||||||
|
loop.quit();
|
||||||
|
});
|
||||||
|
loop.exec();
|
||||||
|
QString result = QString::fromUtf8(reply->readAll());
|
||||||
|
int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
|
||||||
|
QString errorString = reply->errorString();
|
||||||
|
bool isSuccess = reply->error() == QNetworkReply::NoError;
|
||||||
|
reply->deleteLater();
|
||||||
|
reply = nullptr;
|
||||||
|
if (isSuccess) {
|
||||||
|
Q_EMIT success(result);
|
||||||
|
break;
|
||||||
|
}else{
|
||||||
|
if(i == retry()-1){
|
||||||
|
Q_EMIT error(status,errorString);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Q_EMIT finish();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
void FluHttp::postJson(QVariantMap params,QVariantMap headers){
|
||||||
|
QVariantMap data = invokeIntercept(params,headers,"postJson").toMap();
|
||||||
|
QThreadPool::globalInstance()->start([=](){
|
||||||
|
Q_EMIT start();
|
||||||
|
for (int i = 0; i < retry(); ++i) {
|
||||||
|
QNetworkAccessManager manager;
|
||||||
|
manager.setTransferTimeout(timeout());
|
||||||
|
QUrl url(_url);
|
||||||
|
QNetworkRequest request(url);
|
||||||
|
addHeaders(&request,data["headers"].toMap());
|
||||||
|
QString contentType = QString("application/json;charset=utf-8");
|
||||||
|
request.setHeader(QNetworkRequest::ContentTypeHeader, contentType);
|
||||||
|
QEventLoop loop;
|
||||||
|
QNetworkReply* reply = manager.post(request,QJsonDocument::fromVariant(data["params"]).toJson());
|
||||||
|
_cache.append(reply);
|
||||||
|
connect(&manager,&QNetworkAccessManager::finished,this,[&loop](QNetworkReply *reply){
|
||||||
|
loop.quit();
|
||||||
|
});
|
||||||
|
loop.exec();
|
||||||
|
QString result = QString::fromUtf8(reply->readAll());
|
||||||
|
int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
|
||||||
|
QString errorString = reply->errorString();
|
||||||
|
bool isSuccess = reply->error() == QNetworkReply::NoError;
|
||||||
|
reply->deleteLater();
|
||||||
|
reply = nullptr;
|
||||||
|
if (isSuccess) {
|
||||||
|
Q_EMIT success(result);
|
||||||
|
break;
|
||||||
|
}else{
|
||||||
|
if(i == retry()-1){
|
||||||
|
Q_EMIT error(status,errorString);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Q_EMIT finish();
|
Q_EMIT finish();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -69,27 +158,35 @@ void FluHttp::get(QVariantMap params,QVariantMap headers){
|
|||||||
QVariantMap data = invokeIntercept(params,headers,"get").toMap();
|
QVariantMap data = invokeIntercept(params,headers,"get").toMap();
|
||||||
QThreadPool::globalInstance()->start([=](){
|
QThreadPool::globalInstance()->start([=](){
|
||||||
Q_EMIT start();
|
Q_EMIT start();
|
||||||
|
for (int i = 0; i < retry(); ++i) {
|
||||||
QNetworkAccessManager manager;
|
QNetworkAccessManager manager;
|
||||||
|
manager.setTransferTimeout(timeout());
|
||||||
QUrl url(_url);
|
QUrl url(_url);
|
||||||
addQueryParam(&url,data["params"].toMap());
|
addQueryParam(&url,data["params"].toMap());
|
||||||
QNetworkRequest request(url);
|
QNetworkRequest request(url);
|
||||||
addHeaders(&request,data["headers"].toMap());
|
addHeaders(&request,data["headers"].toMap());
|
||||||
QEventLoop *loop = new QEventLoop();
|
QEventLoop loop;
|
||||||
connect(&manager,&QNetworkAccessManager::finished,this,[=](QNetworkReply *reply){
|
|
||||||
loop->quit();
|
|
||||||
});
|
|
||||||
QNetworkReply* reply = manager.get(request);
|
QNetworkReply* reply = manager.get(request);
|
||||||
_cache.append(reply);
|
_cache.append(reply);
|
||||||
loop->exec();
|
connect(&manager,&QNetworkAccessManager::finished,this,[&loop](QNetworkReply *reply){
|
||||||
if (reply->error() == QNetworkReply::NoError) {
|
loop.quit();
|
||||||
Q_EMIT success(QString::fromUtf8(reply->readAll()));
|
});
|
||||||
}else{
|
loop.exec();
|
||||||
Q_EMIT error(reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(),reply->errorString());
|
QString result = QString::fromUtf8(reply->readAll());
|
||||||
}
|
int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
|
||||||
loop->deleteLater();
|
QString errorString = reply->errorString();
|
||||||
|
bool isSuccess = reply->error() == QNetworkReply::NoError;
|
||||||
reply->deleteLater();
|
reply->deleteLater();
|
||||||
loop = nullptr;
|
|
||||||
reply = nullptr;
|
reply = nullptr;
|
||||||
|
if (isSuccess) {
|
||||||
|
Q_EMIT success(result);
|
||||||
|
break;
|
||||||
|
}else{
|
||||||
|
if(i == retry()-1){
|
||||||
|
Q_EMIT error(status,errorString);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Q_EMIT finish();
|
Q_EMIT finish();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -113,9 +210,9 @@ void FluHttp::download(QString path,QVariantMap params,QVariantMap headers){
|
|||||||
Q_EMIT finish();
|
Q_EMIT finish();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
QEventLoop *loop = new QEventLoop();
|
QEventLoop loop;
|
||||||
connect(&manager,&QNetworkAccessManager::finished,this,[=](QNetworkReply *reply){
|
connect(&manager,&QNetworkAccessManager::finished,this,[&loop](QNetworkReply *reply){
|
||||||
loop->quit();
|
loop.quit();
|
||||||
});
|
});
|
||||||
QPointer<QNetworkReply> reply = manager.get(request);
|
QPointer<QNetworkReply> reply = manager.get(request);
|
||||||
_cache.append(reply);
|
_cache.append(reply);
|
||||||
@ -125,17 +222,15 @@ void FluHttp::download(QString path,QVariantMap params,QVariantMap headers){
|
|||||||
connect(reply,&QNetworkReply::readyRead,this,[=](){
|
connect(reply,&QNetworkReply::readyRead,this,[=](){
|
||||||
file->write(reply->readAll());
|
file->write(reply->readAll());
|
||||||
});
|
});
|
||||||
loop->exec();
|
loop.exec();
|
||||||
if (reply->error() == QNetworkReply::NoError) {
|
if (reply->error() == QNetworkReply::NoError) {
|
||||||
Q_EMIT success(path);
|
Q_EMIT success(path);
|
||||||
}else{
|
}else{
|
||||||
Q_EMIT error(reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(),reply->errorString());
|
Q_EMIT error(reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(),reply->errorString());
|
||||||
}
|
}
|
||||||
file->close();
|
file->close();
|
||||||
loop->deleteLater();
|
|
||||||
file->deleteLater();
|
file->deleteLater();
|
||||||
reply->deleteLater();
|
reply->deleteLater();
|
||||||
loop = nullptr;
|
|
||||||
file = nullptr;
|
file = nullptr;
|
||||||
reply = nullptr;
|
reply = nullptr;
|
||||||
Q_EMIT finish();
|
Q_EMIT finish();
|
||||||
|
@ -11,6 +11,8 @@ class FluHttp : public QObject
|
|||||||
{
|
{
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
Q_PROPERTY_AUTO(QString,url);
|
Q_PROPERTY_AUTO(QString,url);
|
||||||
|
Q_PROPERTY_AUTO(int,retry);
|
||||||
|
Q_PROPERTY_AUTO(int,timeout)
|
||||||
QML_NAMED_ELEMENT(FluHttp)
|
QML_NAMED_ELEMENT(FluHttp)
|
||||||
private:
|
private:
|
||||||
QVariant invokeIntercept(const QVariant& params,const QVariant& headers,const QString& method);
|
QVariant invokeIntercept(const QVariant& params,const QVariant& headers,const QString& method);
|
||||||
@ -27,6 +29,8 @@ public:
|
|||||||
Q_SIGNAL void downloadProgress(qint64 recv, qint64 total);
|
Q_SIGNAL void downloadProgress(qint64 recv, qint64 total);
|
||||||
Q_INVOKABLE void get(QVariantMap params = {},QVariantMap headers = {});
|
Q_INVOKABLE void get(QVariantMap params = {},QVariantMap headers = {});
|
||||||
Q_INVOKABLE void post(QVariantMap params = {},QVariantMap headers = {});
|
Q_INVOKABLE void post(QVariantMap params = {},QVariantMap headers = {});
|
||||||
|
Q_INVOKABLE void postString(QString params = "",QVariantMap headers = {});
|
||||||
|
Q_INVOKABLE void postJson(QVariantMap params = {},QVariantMap headers = {});
|
||||||
Q_INVOKABLE void download(QString path,QVariantMap params = {},QVariantMap headers = {});
|
Q_INVOKABLE void download(QString path,QVariantMap params = {},QVariantMap headers = {});
|
||||||
Q_INVOKABLE void cancel();
|
Q_INVOKABLE void cancel();
|
||||||
private:
|
private:
|
||||||
|
Loading…
Reference in New Issue
Block a user