diff --git a/example/qml-Qt6/global/ItemsOriginal.qml b/example/qml-Qt6/global/ItemsOriginal.qml index 1528a77e..2d066760 100644 --- a/example/qml-Qt6/global/ItemsOriginal.qml +++ b/example/qml-Qt6/global/ItemsOriginal.qml @@ -88,7 +88,10 @@ FluObject{ } url:"qrc:/example/qml/page/T_Text.qml" onDropped:{ FluApp.navigate("/pageWindow",{title:title,url:url}) } - onTap:{ navigationView.push(url) } + onTap:{ + item_text.count = 0 + navigationView.push(url) + } } FluPaneItem{ title:"Image" diff --git a/example/qml-Qt6/window/MainWindow.qml b/example/qml-Qt6/window/MainWindow.qml index cde74729..b1982ef4 100644 --- a/example/qml-Qt6/window/MainWindow.qml +++ b/example/qml-Qt6/window/MainWindow.qml @@ -309,7 +309,6 @@ CustomWindow { } } - HttpCallable{ id:callable onStart: { @@ -335,8 +334,21 @@ CustomWindow { } } + +// HttpRequest{ +// id:reuqest +// url: "https://api.github.com/repos/zhuzichu520/FluentUI/releases/latest" +// } + + function checkUpdate(){ - http.get("https://api.github.com/repos/zhuzichu520/FluentUI/releases/latest",callable) + var request = http.newRequest() + console.debug("-------------------->"+request) + request.url = "https://api.github.com/repos/zhuzichu520/FluentUI/releases/latest" + + console.debug("-------------------->"+request.url) + http.get2(request,callable); +// http.get("https://api.github.com/repos/zhuzichu520/FluentUI/releases/latest",callable) } } diff --git a/example/qml/global/ItemsOriginal.qml b/example/qml/global/ItemsOriginal.qml index b825ada9..8ba1b729 100644 --- a/example/qml/global/ItemsOriginal.qml +++ b/example/qml/global/ItemsOriginal.qml @@ -88,7 +88,10 @@ FluObject{ } url:"qrc:/example/qml/page/T_Text.qml" onDropped:{ FluApp.navigate("/pageWindow",{title:title,url:url}) } - onTap:{ navigationView.push(url) } + onTap:{ + item_text.count = 0 + navigationView.push(url) + } } FluPaneItem{ title:"Image" diff --git a/src/FluHttp.cpp b/src/FluHttp.cpp index 63e5e61d..d026c67f 100644 --- a/src/FluHttp.cpp +++ b/src/FluHttp.cpp @@ -5,6 +5,7 @@ #include #include #include +#include #include #include #include @@ -13,6 +14,13 @@ #include "FluApp.h" #include "FluTools.h" +HttpRequest::HttpRequest(QObject *parent) + : QObject{parent} +{ + params({}); + headers({}); +} + HttpCallable::HttpCallable(QObject *parent) : QObject{parent} { @@ -41,18 +49,18 @@ void FluHttp::cancel(){ } void FluHttp::post(QString url,HttpCallable* callable,QMap params,QMap headers){ + auto requestMap = toRequest(url,params,headers,"post"); + auto httpId = toHttpId(requestMap); + QMap data = invokeIntercept(requestMap).toMap(); QThreadPool::globalInstance()->start([=](){ - auto requestMap = toRequest(url,params,headers,"post"); - auto httpId = toHttpId(requestMap); - QMap data = invokeIntercept(requestMap).toMap(); - Q_EMIT callable->start(); + onStart(callable); if(_cacheMode == FluHttpType::CacheMode::IfNoneCacheRequest && cacheExists(httpId)){ - Q_EMIT callable->cache(readCache(httpId)); - Q_EMIT callable->finish(); + onCache(callable,readCache(httpId)); + onFinish(callable); return; } if(_cacheMode == FluHttpType::CacheMode::FirstCacheThenRequest && cacheExists(httpId)){ - Q_EMIT callable->cache(readCache(httpId)); + onCache(callable,readCache(httpId)); } for (int i = 0; i < retry(); ++i) { QNetworkAccessManager manager; @@ -74,9 +82,8 @@ void FluHttp::post(QString url,HttpCallable* callable,QMap pa QEventLoop loop; QNetworkReply* reply = manager.post(request,&multiPart); _cacheReply.append(reply); - connect(&manager,&QNetworkAccessManager::finished,&manager,[&loop](QNetworkReply *reply){ - loop.quit(); - }); + connect(&manager,&QNetworkAccessManager::finished,&manager,[&loop](QNetworkReply *reply){loop.quit();}); + connect(qApp,&QGuiApplication::aboutToQuit,&manager, [&loop](){loop.quit();}); loop.exec(); QString result = QString::fromUtf8(reply->readAll()); int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); @@ -86,34 +93,34 @@ void FluHttp::post(QString url,HttpCallable* callable,QMap pa reply = nullptr; if (isSuccess) { handleCache(httpId,result); - Q_EMIT callable->success(result); + onSuccess(callable,result); break; }else{ if(i == retry()-1){ if(_cacheMode == FluHttpType::CacheMode::RequestFailedReadCache && cacheExists(httpId)){ - Q_EMIT callable->cache(readCache(httpId)); + onCache(callable,readCache(httpId)); } - Q_EMIT callable->error(status,errorString,result); + onError(callable,status,errorString,result); } } } - Q_EMIT callable->finish(); + onFinish(callable); }); } void FluHttp::postString(QString url,HttpCallable* callable,QString params,QMap headers){ + auto requestMap = toRequest(url,params,headers,"postString"); + auto httpId = toHttpId(requestMap); + QMap data = invokeIntercept(requestMap).toMap(); QThreadPool::globalInstance()->start([=](){ - auto requestMap = toRequest(url,params,headers,"postString"); - auto httpId = toHttpId(requestMap); - QMap data = invokeIntercept(requestMap).toMap(); - Q_EMIT callable->start(); + onStart(callable); if(_cacheMode == FluHttpType::CacheMode::IfNoneCacheRequest && cacheExists(httpId)){ - Q_EMIT callable->cache(readCache(httpId)); - Q_EMIT callable->finish(); + onCache(callable,readCache(httpId)); + onFinish(callable); return; } if(_cacheMode == FluHttpType::CacheMode::FirstCacheThenRequest && cacheExists(httpId)){ - Q_EMIT callable->cache(readCache(httpId)); + onCache(callable,readCache(httpId)); } for (int i = 0; i < retry(); ++i) { QNetworkAccessManager manager; @@ -126,9 +133,8 @@ void FluHttp::postString(QString url,HttpCallable* callable,QString params,QMap< QEventLoop loop; QNetworkReply* reply = manager.post(request,params.toUtf8()); _cacheReply.append(reply); - connect(&manager,&QNetworkAccessManager::finished,&manager,[&loop](QNetworkReply *reply){ - loop.quit(); - }); + connect(&manager,&QNetworkAccessManager::finished,&manager,[&loop](QNetworkReply *reply){loop.quit();}); + connect(qApp,&QGuiApplication::aboutToQuit,&manager, [&loop](){loop.quit();}); loop.exec(); QString result = QString::fromUtf8(reply->readAll()); int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); @@ -138,34 +144,34 @@ void FluHttp::postString(QString url,HttpCallable* callable,QString params,QMap< reply = nullptr; if (isSuccess) { handleCache(httpId,result); - Q_EMIT callable->success(result); + onSuccess(callable,result); break; }else{ if(i == retry()-1){ if(_cacheMode == FluHttpType::CacheMode::RequestFailedReadCache && cacheExists(httpId)){ - Q_EMIT callable->cache(readCache(httpId)); + onCache(callable,readCache(httpId)); } - Q_EMIT callable->error(status,errorString,result); + onError(callable,status,errorString,result); } } } - Q_EMIT callable->finish(); + onFinish(callable); }); } void FluHttp::postJson(QString url,HttpCallable* callable,QMap params,QMap headers){ + auto requestMap = toRequest(url,params,headers,"postJson"); + auto httpId = toHttpId(requestMap); + QMap data = invokeIntercept(requestMap).toMap(); QThreadPool::globalInstance()->start([=](){ - auto requestMap = toRequest(url,params,headers,"postJson"); - auto httpId = toHttpId(requestMap); - QMap data = invokeIntercept(requestMap).toMap(); - Q_EMIT callable->start(); + onStart(callable); if(_cacheMode == FluHttpType::CacheMode::IfNoneCacheRequest && cacheExists(httpId)){ - Q_EMIT callable->cache(readCache(httpId)); - Q_EMIT callable->finish(); + onCache(callable,readCache(httpId)); + onFinish(callable); return; } if(_cacheMode == FluHttpType::CacheMode::FirstCacheThenRequest && cacheExists(httpId)){ - Q_EMIT callable->cache(readCache(httpId)); + onCache(callable,readCache(httpId)); } for (int i = 0; i < retry(); ++i) { QNetworkAccessManager manager; @@ -178,9 +184,8 @@ void FluHttp::postJson(QString url,HttpCallable* callable,QMapreadAll()); int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); @@ -190,33 +195,40 @@ void FluHttp::postJson(QString url,HttpCallable* callable,QMapsuccess(result); + onSuccess(callable,result); break; }else{ if(i == retry()-1){ if(_cacheMode == FluHttpType::CacheMode::RequestFailedReadCache && cacheExists(httpId)){ - Q_EMIT callable->cache(readCache(httpId)); + onCache(callable,readCache(httpId)); } - Q_EMIT callable->error(status,errorString,result); + onError(callable,status,errorString,result); } } } - Q_EMIT callable->finish(); + onFinish(callable); }); } +void FluHttp::get2(HttpRequest* request,HttpCallable* callable){ + QString url = request->url(); + QMap params = request->params().toMap(); + QMap headers = request->headers().toMap(); + get(url,callable,params,headers); +} + void FluHttp::get(QString url,HttpCallable* callable,QMap params,QMap headers){ + auto requestMap = toRequest(url,params,headers,"get"); + auto httpId = toHttpId(requestMap); + QMap data = invokeIntercept(requestMap).toMap(); QThreadPool::globalInstance()->start([=](){ - auto requestMap = toRequest(url,params,headers,"get"); - auto httpId = toHttpId(requestMap); - QMap data = invokeIntercept(requestMap).toMap(); - Q_EMIT callable->start(); + onStart(callable); if(_cacheMode == FluHttpType::CacheMode::FirstCacheThenRequest && cacheExists(httpId)){ - Q_EMIT callable->cache(readCache(httpId)); + onCache(callable,readCache(httpId)); } if(_cacheMode == FluHttpType::CacheMode::IfNoneCacheRequest && cacheExists(httpId)){ - Q_EMIT callable->cache(readCache(httpId)); - Q_EMIT callable->finish(); + onCache(callable,readCache(httpId)); + onFinish(callable); return; } for (int i = 0; i < retry(); ++i) { @@ -229,40 +241,39 @@ void FluHttp::get(QString url,HttpCallable* callable,QMap par QEventLoop loop; QNetworkReply* reply = manager.get(request); _cacheReply.append(reply); - connect(&manager,&QNetworkAccessManager::finished,&manager,[&loop](QNetworkReply *reply){ - loop.quit(); - }); + connect(&manager,&QNetworkAccessManager::finished,&manager,[&loop](QNetworkReply *reply){loop.quit();}); + connect(qApp,&QGuiApplication::aboutToQuit,&manager, [&loop](){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; + QString result = QString::fromUtf8(reply->readAll()); if (isSuccess) { handleCache(httpId,result); - Q_EMIT callable->success(result); + onSuccess(callable,result); break; }else{ if(i == retry()-1){ if(_cacheMode == FluHttpType::CacheMode::RequestFailedReadCache && cacheExists(httpId)){ - Q_EMIT callable->cache(readCache(httpId)); + onCache(callable,readCache(httpId)); } - Q_EMIT callable->error(status,errorString,result); + onError(callable,status,errorString,result); } } + reply->deleteLater(); + reply = nullptr; } - Q_EMIT callable->finish(); + onFinish(callable); }); } void FluHttp::download(QString url,HttpCallable* callable,QString savePath,QMap params,QMap headers){ + auto requestMap = toRequest(url,params,headers,"download"); + requestMap.insert("savePath",savePath); + auto httpId = toHttpId(requestMap); + QMap data = invokeIntercept(requestMap).toMap(); QThreadPool::globalInstance()->start([=](){ - auto requestMap = toRequest(url,params,headers,"download"); - requestMap.insert("savePath",savePath); - auto httpId = toHttpId(requestMap); - QMap data = invokeIntercept(requestMap).toMap(); - Q_EMIT callable->start(); + onStart(callable); QNetworkAccessManager manager; QUrl _url(url); addQueryParam(&_url,data["params"].toMap()); @@ -274,9 +285,8 @@ void FluHttp::download(QString url,HttpCallable* callable,QString savePath,QMap< dir.mkpath(dir.path()); } QEventLoop loop; - connect(&manager,&QNetworkAccessManager::finished,&manager,[&loop](QNetworkReply *reply){ - loop.quit(); - }); + connect(&manager,&QNetworkAccessManager::finished,&manager,[&loop](QNetworkReply *reply){loop.quit();}); + connect(qApp,&QGuiApplication::aboutToQuit,&manager, [&loop](){loop.quit();}); qint64 seek = 0; auto filePath = getCacheFilePath(httpId); QSharedPointer fileCache(new QFile(filePath)); @@ -285,9 +295,9 @@ void FluHttp::download(QString url,HttpCallable* callable,QString savePath,QMap< qint64 fileSize = cacheInfo.value("fileSize").toDouble(); qint64 contentLength = cacheInfo.value("contentLength").toDouble(); if(fileSize == contentLength && file->size() == contentLength){ - Q_EMIT callable->downloadProgress(fileSize,contentLength); - Q_EMIT callable->success(savePath); - Q_EMIT callable->finish(); + onDownloadProgress(callable,fileSize,contentLength); + onSuccess(callable,savePath); + onFinish(callable); return; } if(fileSize==file->size()){ @@ -306,7 +316,7 @@ void FluHttp::download(QString url,HttpCallable* callable,QString savePath,QMap< { qDebug()<<"FileCache Error"; } - connect(reply,&QNetworkReply::readyRead,reply,[reply,file,fileCache,requestMap,callable,seek]{ + connect(reply,&QNetworkReply::readyRead,reply,[reply,file,fileCache,requestMap,callable,seek,this]{ if (!reply || !file || reply->error() != QNetworkReply::NoError) { return; @@ -322,25 +332,25 @@ void FluHttp::download(QString url,HttpCallable* callable,QString savePath,QMap< fileCache->resize(0); fileCache->write(FluTools::getInstance()->toBase64(QJsonDocument::fromVariant(QVariant(downMap)).toJson()).toUtf8()); fileCache->flush(); - Q_EMIT callable->downloadProgress(file->size(),contentLength); + onDownloadProgress(callable,file->size(),contentLength); }); loop.exec(); if (reply->error() == QNetworkReply::NoError) { - Q_EMIT callable->success(savePath); + onSuccess(callable,savePath); }else{ - Q_EMIT callable->error(reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(),reply->errorString(),""); + onError(callable,reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(),reply->errorString(),""); } reply->deleteLater(); reply = nullptr; - Q_EMIT callable->finish(); + onFinish(callable); }); } void FluHttp::upload(QString url,HttpCallable* callable,QMap params,QMap headers){ + auto requestMap = toRequest(url,params,headers,"upload"); + QMap data = invokeIntercept(requestMap).toMap(); QThreadPool::globalInstance()->start([=](){ - auto requestMap = toRequest(url,params,headers,"upload"); - QMap data = invokeIntercept(requestMap).toMap(); - Q_EMIT callable->start(); + onStart(callable); QNetworkAccessManager manager; manager.setTransferTimeout(timeout()); QUrl _url(url); @@ -364,11 +374,10 @@ void FluHttp::upload(QString url,HttpCallable* callable,QMap QEventLoop loop; QNetworkReply* reply = manager.post(request,&multiPart); _cacheReply.append(reply); - connect(&manager,&QNetworkAccessManager::finished,&manager,[&loop](QNetworkReply *reply){ - loop.quit(); - }); + connect(&manager,&QNetworkAccessManager::finished,&manager,[&loop](QNetworkReply *reply){loop.quit();}); + connect(qApp,&QGuiApplication::aboutToQuit,&manager, [&loop](){loop.quit();}); connect(reply,&QNetworkReply::uploadProgress,reply,[=](qint64 bytesSent, qint64 bytesTotal){ - Q_EMIT callable->uploadProgress(bytesSent,bytesTotal); + onUploadProgress(callable,bytesSent,bytesTotal); }); loop.exec(); QString result = QString::fromUtf8(reply->readAll()); @@ -378,11 +387,11 @@ void FluHttp::upload(QString url,HttpCallable* callable,QMap reply->deleteLater(); reply = nullptr; if (isSuccess) { - Q_EMIT callable->success(result); + onSuccess(callable,result); }else{ - Q_EMIT callable->error(status,errorString,result); + onError(callable,status,errorString,result); } - Q_EMIT callable->finish(); + onFinish(callable); }); } @@ -396,12 +405,12 @@ QMap FluHttp::toRequest(const QString& url,const QVariant& pa return request; } -QVariant FluHttp::invokeIntercept(QMap request,Qt::ConnectionType type){ +QVariant FluHttp::invokeIntercept(QMap request){ if(!FluApp::getInstance()->httpInterceptor()){ return request; } QVariant target; - QMetaObject::invokeMethod(FluApp::getInstance()->httpInterceptor(), "onIntercept",type,Q_RETURN_ARG(QVariant,target),Q_ARG(QVariant, request)); + QMetaObject::invokeMethod(FluApp::getInstance()->httpInterceptor(), "onIntercept",Q_RETURN_ARG(QVariant,target),Q_ARG(QVariant, request)); return target; } @@ -490,6 +499,53 @@ qreal FluHttp::breakPointDownloadProgress(QString url,QString savePath,QMap& map){ return FluTools::getInstance()->sha256(QJsonDocument::fromVariant(QVariant(map)).toJson()); } + +void FluHttp::onStart(QPointer callable){ + if(callable){ + Q_EMIT callable->start(); + } +} + +void FluHttp::onFinish(QPointer callable){ + if(callable){ + Q_EMIT callable->finish(); + } +} + +void FluHttp::onError(QPointer callable,int status,QString errorString,QString result){ + if(callable){ + Q_EMIT callable->error(status,errorString,result); + } +} + +void FluHttp::onSuccess(QPointer callable,QString result){ + if(callable){ + Q_EMIT callable->success(result); + } +} + +void FluHttp::onCache(QPointer callable,QString result){ + if(callable){ + Q_EMIT callable->cache(result); + } +} + +void FluHttp::onDownloadProgress(QPointer callable,qint64 recv,qint64 total){ + if(callable){ + Q_EMIT callable->downloadProgress(recv,total); + } +} + +void FluHttp::onUploadProgress(QPointer callable,qint64 sent,qint64 total){ + if(callable){ + Q_EMIT callable->uploadProgress(sent,total); + } +} diff --git a/src/FluHttp.h b/src/FluHttp.h index 74343328..2e8e1013 100644 --- a/src/FluHttp.h +++ b/src/FluHttp.h @@ -6,7 +6,20 @@ #include #include #include "stdafx.h" -#include + +class HttpRequest : public QObject{ + Q_OBJECT + Q_PROPERTY_AUTO(QString,url); + Q_PROPERTY_AUTO(QVariant,params); + Q_PROPERTY_AUTO(QVariant,headers); + Q_PROPERTY_AUTO(QString,method); + QML_NAMED_ELEMENT(HttpRequest) +public: + explicit HttpRequest(QObject *parent = nullptr); + ~HttpRequest(){ + qDebug()<<"------------析构了"< request,Qt::ConnectionType type = Qt::BlockingQueuedConnection); + QVariant invokeIntercept(QMap request); QMap toRequest(const QString& url,const QVariant& params,const QVariant& headers,const QString& method); QString toHttpId(const QMap& map); void addQueryParam(QUrl* url,const QMap& params); @@ -41,9 +54,18 @@ private: QString readCache(const QString& httpId); bool cacheExists(const QString& httpId); QString getCacheFilePath(const QString& httpId); + void onStart(QPointer callable); + void onFinish(QPointer callable); + void onError(QPointer callable,int status,QString errorString,QString result); + void onSuccess(QPointer callable,QString result); + void onCache(QPointer callable,QString result); + void onDownloadProgress(QPointer callable,qint64 recv,qint64 total); + void onUploadProgress(QPointer callable,qint64 sent,qint64 total); public: explicit FluHttp(QObject *parent = nullptr); ~FluHttp(); + Q_INVOKABLE HttpRequest* newRequest(); + Q_INVOKABLE void get2(HttpRequest* request,HttpCallable* callable); //神坑!!! 如果参数使用QVariantMap会有问题,在6.4.3版本中QML一调用就会编译失败。所以改用QMap Q_INVOKABLE void get(QString url,HttpCallable* callable,QMap params= {},QMap headers = {}); Q_INVOKABLE void post(QString url,HttpCallable* callable,QMap params= {},QMap headers = {}); diff --git a/src/Qt5/imports/FluentUI/Controls/FluScreenshot.qml b/src/Qt5/imports/FluentUI/Controls/FluScreenshot.qml index a36683f6..55f5dc53 100644 --- a/src/Qt5/imports/FluentUI/Controls/FluScreenshot.qml +++ b/src/Qt5/imports/FluentUI/Controls/FluScreenshot.qml @@ -41,7 +41,7 @@ Item{ } } Component.onCompleted: { - setGeometry(0,0,screenshot_background.width,screenshot_background.height) + setGeometry(0,0,screenshot_background.width,screenshot_background.height+1) } ScreenshotBackground{ id:screenshot_background