This commit is contained in:
zhuzichu 2023-09-07 18:07:23 +08:00
parent 531bffdf1a
commit c92d807ec1
6 changed files with 191 additions and 95 deletions

View File

@ -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"

View File

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

View File

@ -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"

View File

@ -5,6 +5,7 @@
#include <QNetworkReply>
#include <QUrlQuery>
#include <QHttpMultiPart>
#include <QGuiApplication>
#include <QJsonDocument>
#include <QStandardPaths>
#include <QTextStream>
@ -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<QString, QVariant> params,QMap<QString, QVariant> headers){
QThreadPool::globalInstance()->start([=](){
auto requestMap = toRequest(url,params,headers,"post");
auto httpId = toHttpId(requestMap);
QMap<QString, QVariant> data = invokeIntercept(requestMap).toMap();
Q_EMIT callable->start();
QThreadPool::globalInstance()->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<QString, QVariant> 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<QString, QVariant> 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<QString, QVariant> headers){
QThreadPool::globalInstance()->start([=](){
auto requestMap = toRequest(url,params,headers,"postString");
auto httpId = toHttpId(requestMap);
QMap<QString, QVariant> data = invokeIntercept(requestMap).toMap();
Q_EMIT callable->start();
QThreadPool::globalInstance()->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<QString, QVariant> params,QMap<QString, QVariant> headers){
QThreadPool::globalInstance()->start([=](){
auto requestMap = toRequest(url,params,headers,"postJson");
auto httpId = toHttpId(requestMap);
QMap<QString, QVariant> data = invokeIntercept(requestMap).toMap();
Q_EMIT callable->start();
QThreadPool::globalInstance()->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,QMap<QString, QVariant
QEventLoop loop;
QNetworkReply* reply = manager.post(request,QJsonDocument::fromVariant(data["params"]).toJson());
_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();
@ -190,33 +195,40 @@ void FluHttp::postJson(QString url,HttpCallable* callable,QMap<QString, QVariant
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::get2(HttpRequest* request,HttpCallable* callable){
QString url = request->url();
QMap<QString, QVariant> params = request->params().toMap();
QMap<QString, QVariant> headers = request->headers().toMap();
get(url,callable,params,headers);
}
void FluHttp::get(QString url,HttpCallable* callable,QMap<QString, QVariant> params,QMap<QString, QVariant> headers){
QThreadPool::globalInstance()->start([=](){
auto requestMap = toRequest(url,params,headers,"get");
auto httpId = toHttpId(requestMap);
QMap<QString, QVariant> data = invokeIntercept(requestMap).toMap();
Q_EMIT callable->start();
QThreadPool::globalInstance()->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<QString, QVariant> 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<QString, QVariant> params,QMap<QString, QVariant> headers){
QThreadPool::globalInstance()->start([=](){
auto requestMap = toRequest(url,params,headers,"download");
requestMap.insert("savePath",savePath);
auto httpId = toHttpId(requestMap);
QMap<QString, QVariant> data = invokeIntercept(requestMap).toMap();
Q_EMIT callable->start();
QThreadPool::globalInstance()->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<QFile> 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<QString, QVariant> params,QMap<QString, QVariant> headers){
QThreadPool::globalInstance()->start([=](){
auto requestMap = toRequest(url,params,headers,"upload");
QMap<QString, QVariant> data = invokeIntercept(requestMap).toMap();
Q_EMIT callable->start();
QThreadPool::globalInstance()->start([=](){
onStart(callable);
QNetworkAccessManager manager;
manager.setTransferTimeout(timeout());
QUrl _url(url);
@ -364,11 +374,10 @@ void FluHttp::upload(QString url,HttpCallable* callable,QMap<QString, QVariant>
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<QString, QVariant>
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<QString, QVariant> FluHttp::toRequest(const QString& url,const QVariant& pa
return request;
}
QVariant FluHttp::invokeIntercept(QMap<QString, QVariant> request,Qt::ConnectionType type){
QVariant FluHttp::invokeIntercept(QMap<QString, QVariant> 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<QStr
}
}
HttpRequest* FluHttp::newRequest(){
HttpRequest* request = new HttpRequest();
return request;
}
QString FluHttp::toHttpId(const QMap<QString, QVariant>& map){
return FluTools::getInstance()->sha256(QJsonDocument::fromVariant(QVariant(map)).toJson());
}
void FluHttp::onStart(QPointer<HttpCallable> callable){
if(callable){
Q_EMIT callable->start();
}
}
void FluHttp::onFinish(QPointer<HttpCallable> callable){
if(callable){
Q_EMIT callable->finish();
}
}
void FluHttp::onError(QPointer<HttpCallable> callable,int status,QString errorString,QString result){
if(callable){
Q_EMIT callable->error(status,errorString,result);
}
}
void FluHttp::onSuccess(QPointer<HttpCallable> callable,QString result){
if(callable){
Q_EMIT callable->success(result);
}
}
void FluHttp::onCache(QPointer<HttpCallable> callable,QString result){
if(callable){
Q_EMIT callable->cache(result);
}
}
void FluHttp::onDownloadProgress(QPointer<HttpCallable> callable,qint64 recv,qint64 total){
if(callable){
Q_EMIT callable->downloadProgress(recv,total);
}
}
void FluHttp::onUploadProgress(QPointer<HttpCallable> callable,qint64 sent,qint64 total){
if(callable){
Q_EMIT callable->uploadProgress(sent,total);
}
}

View File

@ -6,7 +6,20 @@
#include <QFile>
#include <QNetworkAccessManager>
#include "stdafx.h"
#include <QMutex>
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()<<"------------析构了"<<url();
}
};
class HttpCallable : public QObject{
Q_OBJECT
@ -19,7 +32,7 @@ public:
Q_SIGNAL void success(QString result);
Q_SIGNAL void cache(QString result);
Q_SIGNAL void downloadProgress(qint64 recv, qint64 total);
Q_SIGNAL void uploadProgress(qint64 recv, qint64 total);
Q_SIGNAL void uploadProgress(qint64 sent, qint64 total);
};
class FluHttp : public QObject
@ -32,7 +45,7 @@ class FluHttp : public QObject
Q_PROPERTY_AUTO(bool,breakPointDownload);
QML_NAMED_ELEMENT(FluHttp)
private:
QVariant invokeIntercept(QMap<QString, QVariant> request,Qt::ConnectionType type = Qt::BlockingQueuedConnection);
QVariant invokeIntercept(QMap<QString, QVariant> request);
QMap<QString, QVariant> toRequest(const QString& url,const QVariant& params,const QVariant& headers,const QString& method);
QString toHttpId(const QMap<QString, QVariant>& map);
void addQueryParam(QUrl* url,const QMap<QString, QVariant>& params);
@ -41,9 +54,18 @@ private:
QString readCache(const QString& httpId);
bool cacheExists(const QString& httpId);
QString getCacheFilePath(const QString& httpId);
void onStart(QPointer<HttpCallable> callable);
void onFinish(QPointer<HttpCallable> callable);
void onError(QPointer<HttpCallable> callable,int status,QString errorString,QString result);
void onSuccess(QPointer<HttpCallable> callable,QString result);
void onCache(QPointer<HttpCallable> callable,QString result);
void onDownloadProgress(QPointer<HttpCallable> callable,qint64 recv,qint64 total);
void onUploadProgress(QPointer<HttpCallable> 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<QString, QVariant>
Q_INVOKABLE void get(QString url,HttpCallable* callable,QMap<QString, QVariant> params= {},QMap<QString, QVariant> headers = {});
Q_INVOKABLE void post(QString url,HttpCallable* callable,QMap<QString, QVariant> params= {},QMap<QString, QVariant> headers = {});

View File

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