This commit is contained in:
zhuzichu 2023-07-21 18:58:09 +08:00
parent 2b88634c2f
commit 4d78262277
8 changed files with 68 additions and 46 deletions

View File

@ -93,6 +93,11 @@ qt_add_qml_module(example
RESOURCES ${resource_files}
)
#component,QML_NAMED_ELEMENTc++
target_include_directories(example PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/src/component
)
#
set_target_properties(example PROPERTIES
MACOSX_BUNDLE_GUI_IDENTIFIER my.example.com

View File

@ -11,6 +11,9 @@ FluScrollablePage{
title:"Http"
Component.onDestruction: {
console.debug("T_Http -> onDestruction")
}
FluHttp{
id:http_get
@ -66,6 +69,7 @@ FluScrollablePage{
onDownloadProgress:
(recv,total)=>{
var precent = (recv/total * 100).toFixed(0) + "%"
console.debug(precent)
btn_download.text = "下载中..."+precent
}
onError:

View File

@ -12,6 +12,7 @@ class CircularReveal : public QQuickPaintedItem
Q_OBJECT
Q_PROPERTY_AUTO(QQuickItem*,target)
Q_PROPERTY_AUTO(int,radius)
QML_NAMED_ELEMENT(CircularReveal)
public:
CircularReveal(QQuickItem* parent = nullptr);
void paint(QPainter* painter) override;

View File

@ -3,12 +3,14 @@
#include <QObject>
#include <QFileSystemWatcher>
#include <QtQml/qqml.h>
#include "src/stdafx.h"
class FileWatcher : public QObject
{
Q_OBJECT
Q_PROPERTY_AUTO(QString,path);
QML_NAMED_ELEMENT(FileWatcher)
public:
explicit FileWatcher(QObject *parent = nullptr);
Q_SIGNAL void fileChanged();

View File

@ -8,8 +8,6 @@
#include <QProcess>
#include <FramelessHelper/Quick/framelessquickmodule.h>
#include <FramelessHelper/Core/private/framelessconfig_p.h>
#include "src/component/CircularReveal.h"
#include "src/component/FileWatcher.h"
#include "AppInfo.h"
FRAMELESSHELPER_USE_NAMESPACE
@ -39,8 +37,6 @@ FRAMELESSHELPER_USE_NAMESPACE
#ifdef FLUENTUI_BUILD_STATIC_LIB
engine.addImportPath("qrc:/"); // 让静态资源可以被QML引擎搜索到
#endif
qmlRegisterType<CircularReveal>("example", 1, 0, "CircularReveal");
qmlRegisterType<FileWatcher>("example", 1, 0, "FileWatcher");
appInfo->init(&engine);
const QUrl url(QStringLiteral("qrc:/example/qml/App.qml"));
QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,

View File

@ -12,13 +12,32 @@ FluHttp::FluHttp(QObject *parent)
enabledBreakpointDownload(false);
timeout(15);
retry(3);
}
Q_INVOKABLE void FluHttp::postString(QString params,QVariantMap headers){
FluHttp::~FluHttp(){
cancel();
}
void FluHttp::cancel(){
foreach (auto item, cache) {
if(item){
qDebug()<<item;
item->abort();
}
}
}
void FluHttp::handleReply(QNetworkReply* reply){
cache.append(reply);
}
void FluHttp::postString(QString params,QVariantMap headers){
QVariantMap request = invokeIntercept(params,headers,"postString").toMap();
QThreadPool::globalInstance()->start([=](){
HttpClient client;
Q_EMIT start();
HttpClient client;
client.initReplyCompleted = [=](QNetworkReply* reply){ handleReply(reply); };
client.post(_url)
.retry(retry())
.timeout(timeout())
@ -26,22 +45,22 @@ Q_INVOKABLE void FluHttp::postString(QString params,QVariantMap headers){
.headers(request["headers"].toMap())
.onSuccess([=](QString result) {
Q_EMIT success(result);
Q_EMIT finish();
})
.onFailed([=](QNetworkReply* reply) {
Q_EMIT error(reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(),reply->errorString());
Q_EMIT finish();
})
.block()
.exec();
Q_EMIT finish();
});
}
void FluHttp::post(QVariantMap params,QVariantMap headers){
QVariantMap request = invokeIntercept(params,headers,"post").toMap();
QThreadPool::globalInstance()->start([=](){
HttpClient client;
Q_EMIT start();
HttpClient client;
client.initReplyCompleted = [=](QNetworkReply* reply){ handleReply(reply); };
client.post(_url)
.retry(retry())
.timeout(timeout())
@ -50,22 +69,22 @@ void FluHttp::post(QVariantMap params,QVariantMap headers){
.headers(request["headers"].toMap())
.onSuccess([=](QString result) {
Q_EMIT success(result);
Q_EMIT finish();
})
.onFailed([=](QNetworkReply* reply) {
Q_EMIT error(reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(),reply->errorString());
Q_EMIT finish();
})
.block()
.exec();
Q_EMIT finish();
});
}
void FluHttp::postJson(QVariantMap params,QVariantMap headers){
QVariantMap request = invokeIntercept(params,headers,"postJson").toMap();
QThreadPool::globalInstance()->start([=](){
HttpClient client;
Q_EMIT start();
HttpClient client;
client.initReplyCompleted = [=](QNetworkReply* reply){ handleReply(reply); };
client.post(_url)
.retry(retry())
.timeout(timeout())
@ -74,22 +93,22 @@ void FluHttp::postJson(QVariantMap params,QVariantMap headers){
.headers(request["headers"].toMap())
.onSuccess([=](QString result) {
Q_EMIT success(result);
Q_EMIT finish();
})
.onFailed([=](QNetworkReply* reply) {
Q_EMIT error(reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(),reply->errorString());
Q_EMIT finish();
})
.block()
.exec();
Q_EMIT finish();
});
}
void FluHttp::get(QVariantMap params,QVariantMap headers){
QVariantMap request = invokeIntercept(params,headers,"get").toMap();
QThreadPool::globalInstance()->start([=](){
HttpClient client;
Q_EMIT start();
HttpClient client;
client.initReplyCompleted = [=](QNetworkReply* reply){ handleReply(reply); };
client.get(_url)
.retry(retry())
.timeout(timeout())
@ -97,22 +116,23 @@ void FluHttp::get(QVariantMap params,QVariantMap headers){
.headers(request["headers"].toMap())
.onSuccess([=](QString result) {
Q_EMIT success(result);
Q_EMIT finish();
})
.onFailed([=](QNetworkReply* reply) {
Q_EMIT error(reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(),reply->errorString());
Q_EMIT finish();
})
.block()
.exec();
Q_EMIT finish();
});
}
Q_INVOKABLE void FluHttp::download(QString path,QVariantMap params,QVariantMap headers){
void FluHttp::download(QString path,QVariantMap params,QVariantMap headers){
QPointer<FluHttp> weakThis(this);
QVariantMap request = invokeIntercept(params,headers,"download").toMap();
QThreadPool::globalInstance()->start([=](){
HttpClient client;
Q_EMIT start();
HttpClient client;
client.initReplyCompleted = [=](QNetworkReply* reply){ handleReply(reply); };
client.get(_url)
.retry(retry())
.timeout(timeout())
@ -121,18 +141,20 @@ Q_INVOKABLE void FluHttp::download(QString path,QVariantMap params,QVariantMap h
.queryParams(request["params"].toMap())
.headers(request["headers"].toMap())
.onDownloadProgress([=](qint64 recv, qint64 total) {
Q_EMIT downloadProgress(recv,total);
if (!weakThis) {
return;
}
Q_EMIT weakThis->downloadProgress(recv,total);
})
.onDownloadFileSuccess([=](QString result) {
Q_EMIT success(result);
Q_EMIT finish();
})
.onDownloadFileFailed([=](QString errorString) {
Q_EMIT error(-1,errorString);
Q_EMIT finish();
})
.block()
.exec();
Q_EMIT finish();
});
}

View File

@ -16,8 +16,11 @@ class FluHttp : public QObject
QML_NAMED_ELEMENT(FluHttp)
private:
QVariant invokeIntercept(const QVariant& params,const QVariant& headers,const QString& method);
void handleReply(QNetworkReply* reply);
QList<QNetworkReply*> cache;
public:
explicit FluHttp(QObject *parent = nullptr);
~FluHttp();
Q_SIGNAL void start();
Q_SIGNAL void finish();
Q_SIGNAL void error(int status,QString errorString);
@ -27,8 +30,8 @@ public:
Q_INVOKABLE void post(QVariantMap params = {},QVariantMap headers = {});
Q_INVOKABLE void postJson(QVariantMap params = {},QVariantMap headers = {});
Q_INVOKABLE void postString(QString params = "",QVariantMap headers = {});
Q_INVOKABLE void download(QString path,QVariantMap params = {},QVariantMap headers = {});
Q_INVOKABLE void cancel();
};
#endif // FLUHTTP_H

View File

@ -51,6 +51,9 @@ class HttpClient : public QNetworkAccessManager
Q_OBJECT
public:
inline static HttpClient* instance();
~HttpClient(){
qDebug()<<"HttpClient析构了";
}
inline HttpClient(QObject* parent = nullptr);
inline QString getVersion() const;
@ -61,20 +64,7 @@ public:
inline HttpRequest send(const QString& url, Operation op = GetOperation);
HttpClient* timeout(const int& second = 60)
{
timeoutSecond = second;
return this;
}
int timeoutSecond = 60;
HttpClient* header(const QString& key, const QVariant& value)
{
globalHeader.insert(key, value);
return this;
}
QMap<QString, QVariant> globalHeader;
std::function<void(QNetworkReply*)> initReplyCompleted;
private:
#if (QT_VERSION < QT_VERSION_CHECK(5, 8, 0))
inline QNetworkReply* sendCustomRequest(const QNetworkRequest& request, const QByteArray& verb,
@ -1347,9 +1337,10 @@ HttpResponse* HttpRequest::exec(const HttpRequest& _httpRequest, HttpResponse* h
{
httpRequest.m_reply->setReadBufferSize(httpRequest.m_readBufferSize);
}
printDebug(httpRequest.m_logLevel, toString().toStdString().c_str());
if(httpClient->initReplyCompleted){
httpClient->initReplyCompleted(httpRequest.m_reply);
}
if (httpResponse)
{
httpResponse->setParent(httpRequest.m_reply);
@ -1564,27 +1555,27 @@ QString HttpClient::getVersion() const
HttpRequest HttpClient::head(const QString& url)
{
return HttpRequest(QNetworkAccessManager::HeadOperation, this).headers(globalHeader).url(url).timeout(timeoutSecond);
return HttpRequest(QNetworkAccessManager::HeadOperation, this).url(url);
}
HttpRequest HttpClient::get(const QString& url)
{
return HttpRequest(QNetworkAccessManager::GetOperation, this).headers(globalHeader).url(url).timeout(timeoutSecond);
return HttpRequest(QNetworkAccessManager::GetOperation, this).url(url);
}
HttpRequest HttpClient::post(const QString& url)
{
return HttpRequest(QNetworkAccessManager::PostOperation, this).headers(globalHeader).url(url).timeout(timeoutSecond);
return HttpRequest(QNetworkAccessManager::PostOperation, this).url(url);
}
HttpRequest HttpClient::put(const QString& url)
{
return HttpRequest(QNetworkAccessManager::PutOperation, this).headers(globalHeader).url(url).timeout(timeoutSecond);
return HttpRequest(QNetworkAccessManager::PutOperation, this).url(url);
}
HttpRequest HttpClient::send(const QString& url, QNetworkAccessManager::Operation op)
{
return HttpRequest(op, this).headers(globalHeader).url(url).timeout(timeoutSecond);
return HttpRequest(op, this).url(url);
}
#if (QT_VERSION < QT_VERSION_CHECK(5, 8, 0))
@ -1791,12 +1782,10 @@ void HttpResponse::setHttpRequest(const HttpRequest& httpRequest)
}
}
}
if (reply && httpRequest.m_isBlock)
{
new HttpBlocker(reply, httpRequest.m_isBlock);
}
HttpRequest oldRequest = m_httpRequest;
m_httpRequest = httpRequest;