diff --git a/example/qml-Qt6/page/T_Network.qml b/example/qml-Qt6/page/T_Network.qml index 39818745..71baa3cb 100644 --- a/example/qml-Qt6/page/T_Network.qml +++ b/example/qml-Qt6/page/T_Network.qml @@ -414,7 +414,7 @@ FluContentPage{ (status,errorString,result)=>{ btn_upload.progress = 0 text_info.text = result - console.debug(result) + console.debug(status+";"+errorString+";"+result) } onSuccess: (result)=>{ @@ -480,9 +480,9 @@ FluContentPage{ id: file_dialog onAccepted: { FluNetwork.postForm("https://httpbingo.org/post") - .setRetry(0)//请求失败后不重复请求 + .setRetry(1)//只请求一次 .add("accessToken","12345678") - .addFile("file",FluTools.toLocalPath(file_dialog.selectedFile)) + .addFile("file",FluTools.toLocalPath(file_dialog.currentFile)) .bind(root) .go(callable_upload_file) } diff --git a/example/qml-Qt6/window/MainWindow.qml b/example/qml-Qt6/window/MainWindow.qml index 13a79267..55222ef7 100644 --- a/example/qml-Qt6/window/MainWindow.qml +++ b/example/qml-Qt6/window/MainWindow.qml @@ -360,7 +360,7 @@ FluWindow { function checkUpdate(silent){ callable.silent = silent FluNetwork.get("https://api.github.com/repos/zhuzichu520/FluentUI/releases/latest") - .go(callable) + .go(callable) } } diff --git a/example/qml/page/T_Network.qml b/example/qml/page/T_Network.qml index 2c08f206..f3310482 100644 --- a/example/qml/page/T_Network.qml +++ b/example/qml/page/T_Network.qml @@ -415,7 +415,7 @@ FluContentPage{ (status,errorString,result)=>{ btn_upload.progress = 0 text_info.text = result - console.debug(result) + console.debug(status+";"+errorString+";"+result) } onSuccess: (result)=>{ @@ -481,9 +481,9 @@ FluContentPage{ id: file_dialog onAccepted: { FluNetwork.postForm("https://httpbingo.org/post") - .setRetry(0)//请求失败后不重复请求 + .setRetry(1)//只请求一次 .add("accessToken","12345678") - .addFile("file",FluTools.toLocalPath(file_dialog.selectedFile)) + .addFile("file",FluTools.toLocalPath(file_dialog.currentFile)) .bind(root) .go(callable_upload_file) } diff --git a/example/qml/window/MainWindow.qml b/example/qml/window/MainWindow.qml index 082834a8..34d63cfd 100644 --- a/example/qml/window/MainWindow.qml +++ b/example/qml/window/MainWindow.qml @@ -363,7 +363,7 @@ FluWindow { function checkUpdate(silent){ callable.silent = silent FluNetwork.get("https://api.github.com/repos/zhuzichu520/FluentUI/releases/latest") - .go(callable) + .go(callable) } } diff --git a/src/FluNetwork.cpp b/src/FluNetwork.cpp index 79aace2a..245b2cc3 100644 --- a/src/FluNetwork.cpp +++ b/src/FluNetwork.cpp @@ -12,7 +12,10 @@ #include #include #include +#include #include +#include +#include NetworkCallable::NetworkCallable(QObject *parent):QObject{parent}{ @@ -158,37 +161,47 @@ void NetworkParams::go(NetworkCallable* callable){ void FluNetwork::handle(NetworkParams* params,NetworkCallable* c){ QPointer callable(c); - if(!callable.isNull()){ - callable->start(); - } - QString cacheKey = params->buildCacheKey(); - if(params->_cacheMode == FluNetworkType::CacheMode::FirstCacheThenRequest && cacheExists(cacheKey)){ + QThreadPool::globalInstance()->start([=](){ if(!callable.isNull()){ - callable->cache(readCache(cacheKey)); + callable->start(); } - } - if(params->_cacheMode == FluNetworkType::CacheMode::IfNoneCacheRequest && cacheExists(cacheKey)){ - if(!callable.isNull()){ - callable->cache(readCache(cacheKey)); - callable->finish(); - params->deleteLater(); + QString cacheKey = params->buildCacheKey(); + if(params->_cacheMode == FluNetworkType::CacheMode::FirstCacheThenRequest && cacheExists(cacheKey)){ + if(!callable.isNull()){ + callable->cache(readCache(cacheKey)); + } } - return; - } - std::shared_ptr times = std::make_shared(0); - QUrl url(params->_url); - QNetworkAccessManager* manager = new QNetworkAccessManager(); - QNetworkReply *reply = nullptr; - manager->setTransferTimeout(params->getTimeout()); - addQueryParam(&url,params->_queryMap); - QNetworkRequest request(url); - addHeaders(&request,params->_headerMap); - connect(manager,&QNetworkAccessManager::finished,this,[this,params,request,callable,manager,times,cacheKey](QNetworkReply *reply){ - if(reply->error() != QNetworkReply::NoError && *times < params->getRetry()) { - (*times)++; - sendRequest(manager,request,params,reply,callable); - } else { - QString response = QString::fromUtf8(reply->readAll()); + if(params->_cacheMode == FluNetworkType::CacheMode::IfNoneCacheRequest && cacheExists(cacheKey)){ + if(!callable.isNull()){ + callable->cache(readCache(cacheKey)); + callable->finish(); + params->deleteLater(); + } + return; + } + QNetworkAccessManager manager; + manager.setTransferTimeout(params->getTimeout()); + QEventLoop loop; + for (int i = 0; i < params->getRetry(); ++i) { + QUrl url(params->_url); + addQueryParam(&url,params->_queryMap); + QNetworkRequest request(url); + addHeaders(&request,params->_headerMap); + QNetworkReply* reply; + sendRequest(&manager,request,params,reply,callable); + if(!QPointer(qApp)){ + reply->deleteLater(); + reply = nullptr; + return; + } + QEventLoop loop; + connect(&manager,&QNetworkAccessManager::finished,&manager,[&loop](QNetworkReply *reply){loop.quit();}); + connect(qApp,&QGuiApplication::aboutToQuit,&manager, [&loop,reply](){reply->abort(),loop.quit();}); + loop.exec(); + QString response; + if(reply->isOpen()){ + response = QString::fromUtf8(reply->readAll()); + } QNetworkReply::NetworkError error = reply->error(); if(error == QNetworkReply::NoError){ if(!callable.isNull()){ @@ -197,26 +210,27 @@ void FluNetwork::handle(NetworkParams* params,NetworkCallable* c){ } callable->success(response); } + break; }else{ - if(!callable.isNull()){ - int httpStatus = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); - if(params->_cacheMode == FluNetworkType::CacheMode::RequestFailedReadCache && cacheExists(cacheKey)){ - if(!callable.isNull()){ - callable->cache(readCache(cacheKey)); + if(i == params->getRetry()-1){ + if(!callable.isNull()){ + int httpStatus = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); + if(params->_cacheMode == FluNetworkType::CacheMode::RequestFailedReadCache && cacheExists(cacheKey)){ + if(!callable.isNull()){ + callable->cache(readCache(cacheKey)); + } } + callable->error(httpStatus,reply->errorString(),response); } - callable->error(httpStatus,reply->errorString(),response); } } - params->deleteLater(); reply->deleteLater(); - manager->deleteLater(); - if(!callable.isNull()){ - callable->finish(); - } + } + params->deleteLater(); + if(!callable.isNull()){ + callable->finish(); } }); - sendRequest(manager,request,params,reply,callable); } void FluNetwork::handleDownload(NetworkParams* params,NetworkCallable* c){ @@ -267,7 +281,6 @@ void FluNetwork::handleDownload(NetworkParams* params,NetworkCallable* c){ } return; } - if(params->_downloadParam->_append){ if (!cacheFile->open(QIODevice::WriteOnly|QIODevice::Truncate)) { @@ -347,9 +360,6 @@ QString FluNetwork::getCacheFilePath(const QString& key){ } void FluNetwork::sendRequest(QNetworkAccessManager* manager,QNetworkRequest request,NetworkParams* params,QNetworkReply*& reply,QPointer callable){ - if(reply){ - reply->deleteLater(); - } QByteArray verb = params->method2String().toUtf8(); switch (params->_type) { case NetworkParams::TYPE_FORM:{ diff --git a/src/Qt5/imports/FluentUI/Controls/FluWindow.qml b/src/Qt5/imports/FluentUI/Controls/FluWindow.qml index f6a244b8..c33f9443 100644 --- a/src/Qt5/imports/FluentUI/Controls/FluWindow.qml +++ b/src/Qt5/imports/FluentUI/Controls/FluWindow.qml @@ -131,7 +131,7 @@ Window { Item{ data: window.appBar height: { - if(window.useSystemAppBar){ + if(FluApp.useSystemAppBar){ return 0 } return window.fitsAppBarWindows ? 0 : childrenRect.height diff --git a/src/Qt6/imports/FluentUI/Controls/FluWindow.qml b/src/Qt6/imports/FluentUI/Controls/FluWindow.qml index e017fb66..d7579b85 100644 --- a/src/Qt6/imports/FluentUI/Controls/FluWindow.qml +++ b/src/Qt6/imports/FluentUI/Controls/FluWindow.qml @@ -130,7 +130,7 @@ Window { Item{ data: window.appBar height: { - if(window.useSystemAppBar){ + if(FluApp.useSystemAppBar){ return 0 } return window.fitsAppBarWindows ? 0 : childrenRect.height