This commit is contained in:
朱子楚\zhuzi 2023-11-30 01:12:57 +08:00
parent 394d0ab244
commit 1b3344e9f9
7 changed files with 64 additions and 54 deletions

View File

@ -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)
}

View File

@ -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)
}

View File

@ -12,7 +12,10 @@
#include <QJSEngine>
#include <QJsonArray>
#include <QStandardPaths>
#include <QThreadPool>
#include <QDir>
#include <QEventLoop>
#include <QGuiApplication>
NetworkCallable::NetworkCallable(QObject *parent):QObject{parent}{
@ -158,6 +161,7 @@ void NetworkParams::go(NetworkCallable* callable){
void FluNetwork::handle(NetworkParams* params,NetworkCallable* c){
QPointer<NetworkCallable> callable(c);
QThreadPool::globalInstance()->start([=](){
if(!callable.isNull()){
callable->start();
}
@ -175,20 +179,29 @@ void FluNetwork::handle(NetworkParams* params,NetworkCallable* c){
}
return;
}
std::shared_ptr<int> times = std::make_shared<int>(0);
QNetworkAccessManager manager;
manager.setTransferTimeout(params->getTimeout());
QEventLoop loop;
for (int i = 0; i < params->getRetry(); ++i) {
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());
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,7 +210,9 @@ void FluNetwork::handle(NetworkParams* params,NetworkCallable* c){
}
callable->success(response);
}
break;
}else{
if(i == params->getRetry()-1){
if(!callable.isNull()){
int httpStatus = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
if(params->_cacheMode == FluNetworkType::CacheMode::RequestFailedReadCache && cacheExists(cacheKey)){
@ -208,15 +223,14 @@ void FluNetwork::handle(NetworkParams* params,NetworkCallable* c){
callable->error(httpStatus,reply->errorString(),response);
}
}
params->deleteLater();
}
reply->deleteLater();
manager->deleteLater();
}
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<NetworkCallable> callable){
if(reply){
reply->deleteLater();
}
QByteArray verb = params->method2String().toUtf8();
switch (params->_type) {
case NetworkParams::TYPE_FORM:{

View File

@ -131,7 +131,7 @@ Window {
Item{
data: window.appBar
height: {
if(window.useSystemAppBar){
if(FluApp.useSystemAppBar){
return 0
}
return window.fitsAppBarWindows ? 0 : childrenRect.height

View File

@ -130,7 +130,7 @@ Window {
Item{
data: window.appBar
height: {
if(window.useSystemAppBar){
if(FluApp.useSystemAppBar){
return 0
}
return window.fitsAppBarWindows ? 0 : childrenRect.height