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)=>{ (status,errorString,result)=>{
btn_upload.progress = 0 btn_upload.progress = 0
text_info.text = result text_info.text = result
console.debug(result) console.debug(status+";"+errorString+";"+result)
} }
onSuccess: onSuccess:
(result)=>{ (result)=>{
@ -480,9 +480,9 @@ FluContentPage{
id: file_dialog id: file_dialog
onAccepted: { onAccepted: {
FluNetwork.postForm("https://httpbingo.org/post") FluNetwork.postForm("https://httpbingo.org/post")
.setRetry(0)// .setRetry(1)//
.add("accessToken","12345678") .add("accessToken","12345678")
.addFile("file",FluTools.toLocalPath(file_dialog.selectedFile)) .addFile("file",FluTools.toLocalPath(file_dialog.currentFile))
.bind(root) .bind(root)
.go(callable_upload_file) .go(callable_upload_file)
} }

View File

@ -360,7 +360,7 @@ FluWindow {
function checkUpdate(silent){ function checkUpdate(silent){
callable.silent = silent callable.silent = silent
FluNetwork.get("https://api.github.com/repos/zhuzichu520/FluentUI/releases/latest") FluNetwork.get("https://api.github.com/repos/zhuzichu520/FluentUI/releases/latest")
.go(callable) .go(callable)
} }
} }

View File

@ -415,7 +415,7 @@ FluContentPage{
(status,errorString,result)=>{ (status,errorString,result)=>{
btn_upload.progress = 0 btn_upload.progress = 0
text_info.text = result text_info.text = result
console.debug(result) console.debug(status+";"+errorString+";"+result)
} }
onSuccess: onSuccess:
(result)=>{ (result)=>{
@ -481,9 +481,9 @@ FluContentPage{
id: file_dialog id: file_dialog
onAccepted: { onAccepted: {
FluNetwork.postForm("https://httpbingo.org/post") FluNetwork.postForm("https://httpbingo.org/post")
.setRetry(0)// .setRetry(1)//
.add("accessToken","12345678") .add("accessToken","12345678")
.addFile("file",FluTools.toLocalPath(file_dialog.selectedFile)) .addFile("file",FluTools.toLocalPath(file_dialog.currentFile))
.bind(root) .bind(root)
.go(callable_upload_file) .go(callable_upload_file)
} }

View File

@ -363,7 +363,7 @@ FluWindow {
function checkUpdate(silent){ function checkUpdate(silent){
callable.silent = silent callable.silent = silent
FluNetwork.get("https://api.github.com/repos/zhuzichu520/FluentUI/releases/latest") FluNetwork.get("https://api.github.com/repos/zhuzichu520/FluentUI/releases/latest")
.go(callable) .go(callable)
} }
} }

View File

@ -12,7 +12,10 @@
#include <QJSEngine> #include <QJSEngine>
#include <QJsonArray> #include <QJsonArray>
#include <QStandardPaths> #include <QStandardPaths>
#include <QThreadPool>
#include <QDir> #include <QDir>
#include <QEventLoop>
#include <QGuiApplication>
NetworkCallable::NetworkCallable(QObject *parent):QObject{parent}{ NetworkCallable::NetworkCallable(QObject *parent):QObject{parent}{
@ -158,37 +161,47 @@ void NetworkParams::go(NetworkCallable* callable){
void FluNetwork::handle(NetworkParams* params,NetworkCallable* c){ void FluNetwork::handle(NetworkParams* params,NetworkCallable* c){
QPointer<NetworkCallable> callable(c); QPointer<NetworkCallable> callable(c);
if(!callable.isNull()){ QThreadPool::globalInstance()->start([=](){
callable->start();
}
QString cacheKey = params->buildCacheKey();
if(params->_cacheMode == FluNetworkType::CacheMode::FirstCacheThenRequest && cacheExists(cacheKey)){
if(!callable.isNull()){ if(!callable.isNull()){
callable->cache(readCache(cacheKey)); callable->start();
} }
} QString cacheKey = params->buildCacheKey();
if(params->_cacheMode == FluNetworkType::CacheMode::IfNoneCacheRequest && cacheExists(cacheKey)){ if(params->_cacheMode == FluNetworkType::CacheMode::FirstCacheThenRequest && cacheExists(cacheKey)){
if(!callable.isNull()){ if(!callable.isNull()){
callable->cache(readCache(cacheKey)); callable->cache(readCache(cacheKey));
callable->finish(); }
params->deleteLater();
} }
return; if(params->_cacheMode == FluNetworkType::CacheMode::IfNoneCacheRequest && cacheExists(cacheKey)){
} if(!callable.isNull()){
std::shared_ptr<int> times = std::make_shared<int>(0); callable->cache(readCache(cacheKey));
QUrl url(params->_url); callable->finish();
QNetworkAccessManager* manager = new QNetworkAccessManager(); params->deleteLater();
QNetworkReply *reply = nullptr; }
manager->setTransferTimeout(params->getTimeout()); return;
addQueryParam(&url,params->_queryMap); }
QNetworkRequest request(url); QNetworkAccessManager manager;
addHeaders(&request,params->_headerMap); manager.setTransferTimeout(params->getTimeout());
connect(manager,&QNetworkAccessManager::finished,this,[this,params,request,callable,manager,times,cacheKey](QNetworkReply *reply){ QEventLoop loop;
if(reply->error() != QNetworkReply::NoError && *times < params->getRetry()) { for (int i = 0; i < params->getRetry(); ++i) {
(*times)++; QUrl url(params->_url);
sendRequest(manager,request,params,reply,callable); addQueryParam(&url,params->_queryMap);
} else { QNetworkRequest request(url);
QString response = QString::fromUtf8(reply->readAll()); 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(); QNetworkReply::NetworkError error = reply->error();
if(error == QNetworkReply::NoError){ if(error == QNetworkReply::NoError){
if(!callable.isNull()){ if(!callable.isNull()){
@ -197,26 +210,27 @@ void FluNetwork::handle(NetworkParams* params,NetworkCallable* c){
} }
callable->success(response); callable->success(response);
} }
break;
}else{ }else{
if(!callable.isNull()){ if(i == params->getRetry()-1){
int httpStatus = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); if(!callable.isNull()){
if(params->_cacheMode == FluNetworkType::CacheMode::RequestFailedReadCache && cacheExists(cacheKey)){ int httpStatus = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
if(!callable.isNull()){ if(params->_cacheMode == FluNetworkType::CacheMode::RequestFailedReadCache && cacheExists(cacheKey)){
callable->cache(readCache(cacheKey)); if(!callable.isNull()){
callable->cache(readCache(cacheKey));
}
} }
callable->error(httpStatus,reply->errorString(),response);
} }
callable->error(httpStatus,reply->errorString(),response);
} }
} }
params->deleteLater();
reply->deleteLater(); reply->deleteLater();
manager->deleteLater(); }
if(!callable.isNull()){ params->deleteLater();
callable->finish(); if(!callable.isNull()){
} callable->finish();
} }
}); });
sendRequest(manager,request,params,reply,callable);
} }
void FluNetwork::handleDownload(NetworkParams* params,NetworkCallable* c){ void FluNetwork::handleDownload(NetworkParams* params,NetworkCallable* c){
@ -267,7 +281,6 @@ void FluNetwork::handleDownload(NetworkParams* params,NetworkCallable* c){
} }
return; return;
} }
if(params->_downloadParam->_append){ if(params->_downloadParam->_append){
if (!cacheFile->open(QIODevice::WriteOnly|QIODevice::Truncate)) 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){ void FluNetwork::sendRequest(QNetworkAccessManager* manager,QNetworkRequest request,NetworkParams* params,QNetworkReply*& reply,QPointer<NetworkCallable> callable){
if(reply){
reply->deleteLater();
}
QByteArray verb = params->method2String().toUtf8(); QByteArray verb = params->method2String().toUtf8();
switch (params->_type) { switch (params->_type) {
case NetworkParams::TYPE_FORM:{ case NetworkParams::TYPE_FORM:{

View File

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

View File

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