From 1047fac4d6a841c84ceded6c47ebb5bf1112bdc9 Mon Sep 17 00:00:00 2001 From: zhuzichu Date: Wed, 29 Nov 2023 10:41:48 +0800 Subject: [PATCH] update --- example/qml/App.qml | 3 +++ src/FluNetwork.cpp | 47 ++++++++++++++++++++++++++++----------------- src/FluNetwork.h | 4 ++++ 3 files changed, 36 insertions(+), 18 deletions(-) diff --git a/example/qml/App.qml b/example/qml/App.qml index f8a0358a..e98d8c9c 100644 --- a/example/qml/App.qml +++ b/example/qml/App.qml @@ -41,6 +41,9 @@ Item { } Component.onCompleted: { + FluNetwork.setInterceptor(function(param){ + param.addHeader("Token","000000000000000000000") + }) FluApp.init(app) FluApp.useSystemAppBar = SettingsHelper.getUseSystemAppBar() FluApp.vsync = SettingsHelper.getVsync() diff --git a/src/FluNetwork.cpp b/src/FluNetwork.cpp index 61b6dc73..97c92811 100644 --- a/src/FluNetwork.cpp +++ b/src/FluNetwork.cpp @@ -7,6 +7,9 @@ #include #include #include +#include +#include +#include #include #include #include @@ -132,11 +135,15 @@ QString NetworkParams::buildCacheKey(){ } void NetworkParams::go(NetworkCallable* callable){ + QJSValueList data; + data<newQObject(this); + FluNetwork::getInstance()->_interceptor.call(data); if(_downloadParam){ FluNetwork::getInstance()->handleDownload(this,callable); }else{ FluNetwork::getInstance()->handle(this,callable); } + } void FluNetwork::handle(NetworkParams* params,NetworkCallable* c){ @@ -346,73 +353,77 @@ FluNetwork::FluNetwork(QObject *parent): QObject{parent} } NetworkParams* FluNetwork::get(const QString& url){ - return new NetworkParams(url,NetworkParams::TYPE_NONE,NetworkParams::METHOD_GET); + return new NetworkParams(url,NetworkParams::TYPE_NONE,NetworkParams::METHOD_GET,this); } NetworkParams* FluNetwork::head(const QString& url){ - return new NetworkParams(url,NetworkParams::TYPE_NONE,NetworkParams::METHOD_HEAD); + return new NetworkParams(url,NetworkParams::TYPE_NONE,NetworkParams::METHOD_HEAD,this); } NetworkParams* FluNetwork::postBody(const QString& url){ - return new NetworkParams(url,NetworkParams::TYPE_BODY,NetworkParams::METHOD_POST); + return new NetworkParams(url,NetworkParams::TYPE_BODY,NetworkParams::METHOD_POST,this); } NetworkParams* FluNetwork::putBody(const QString& url){ - return new NetworkParams(url,NetworkParams::TYPE_BODY,NetworkParams::METHOD_PUT); + return new NetworkParams(url,NetworkParams::TYPE_BODY,NetworkParams::METHOD_PUT,this); } NetworkParams* FluNetwork::patchBody(const QString& url){ - return new NetworkParams(url,NetworkParams::TYPE_BODY,NetworkParams::METHOD_PATCH); + return new NetworkParams(url,NetworkParams::TYPE_BODY,NetworkParams::METHOD_PATCH,this); } NetworkParams* FluNetwork::deleteBody(const QString& url){ - return new NetworkParams(url,NetworkParams::TYPE_BODY,NetworkParams::METHOD_DELETE); + return new NetworkParams(url,NetworkParams::TYPE_BODY,NetworkParams::METHOD_DELETE,this); } NetworkParams* FluNetwork::postForm(const QString& url){ - return new NetworkParams(url,NetworkParams::TYPE_FORM,NetworkParams::METHOD_POST); + return new NetworkParams(url,NetworkParams::TYPE_FORM,NetworkParams::METHOD_POST,this); } NetworkParams* FluNetwork::putForm(const QString& url){ - return new NetworkParams(url,NetworkParams::TYPE_FORM,NetworkParams::METHOD_PUT); + return new NetworkParams(url,NetworkParams::TYPE_FORM,NetworkParams::METHOD_PUT,this); } NetworkParams* FluNetwork::patchForm(const QString& url){ - return new NetworkParams(url,NetworkParams::TYPE_FORM,NetworkParams::METHOD_PATCH); + return new NetworkParams(url,NetworkParams::TYPE_FORM,NetworkParams::METHOD_PATCH,this); } NetworkParams* FluNetwork::deleteForm(const QString& url){ - return new NetworkParams(url,NetworkParams::TYPE_FORM,NetworkParams::METHOD_DELETE); + return new NetworkParams(url,NetworkParams::TYPE_FORM,NetworkParams::METHOD_DELETE,this); } NetworkParams* FluNetwork::postJson(const QString& url){ - return new NetworkParams(url,NetworkParams::TYPE_JSON,NetworkParams::METHOD_POST); + return new NetworkParams(url,NetworkParams::TYPE_JSON,NetworkParams::METHOD_POST,this); } NetworkParams* FluNetwork::putJson(const QString& url){ - return new NetworkParams(url,NetworkParams::TYPE_JSON,NetworkParams::METHOD_PUT); + return new NetworkParams(url,NetworkParams::TYPE_JSON,NetworkParams::METHOD_PUT,this); } NetworkParams* FluNetwork::patchJson(const QString& url){ - return new NetworkParams(url,NetworkParams::TYPE_JSON,NetworkParams::METHOD_PATCH); + return new NetworkParams(url,NetworkParams::TYPE_JSON,NetworkParams::METHOD_PATCH,this); } NetworkParams* FluNetwork::deleteJson(const QString& url){ - return new NetworkParams(url,NetworkParams::TYPE_JSON,NetworkParams::METHOD_DELETE); + return new NetworkParams(url,NetworkParams::TYPE_JSON,NetworkParams::METHOD_DELETE,this); } NetworkParams* FluNetwork::postJsonArray(const QString& url){ - return new NetworkParams(url,NetworkParams::TYPE_JSONARRAY,NetworkParams::METHOD_POST); + return new NetworkParams(url,NetworkParams::TYPE_JSONARRAY,NetworkParams::METHOD_POST,this); } NetworkParams* FluNetwork::putJsonArray(const QString& url){ - return new NetworkParams(url,NetworkParams::TYPE_JSONARRAY,NetworkParams::METHOD_PUT); + return new NetworkParams(url,NetworkParams::TYPE_JSONARRAY,NetworkParams::METHOD_PUT,this); } NetworkParams* FluNetwork::patchJsonArray(const QString& url){ - return new NetworkParams(url,NetworkParams::TYPE_JSONARRAY,NetworkParams::METHOD_PATCH); + return new NetworkParams(url,NetworkParams::TYPE_JSONARRAY,NetworkParams::METHOD_PATCH,this); } NetworkParams* FluNetwork::deleteJsonArray(const QString& url){ - return new NetworkParams(url,NetworkParams::TYPE_JSONARRAY,NetworkParams::METHOD_DELETE); + return new NetworkParams(url,NetworkParams::TYPE_JSONARRAY,NetworkParams::METHOD_DELETE,this); +} + +void FluNetwork::setInterceptor(QJSValue interceptor){ + this->_interceptor = interceptor; } diff --git a/src/FluNetwork.h b/src/FluNetwork.h index 6d680320..882f0f11 100644 --- a/src/FluNetwork.h +++ b/src/FluNetwork.h @@ -5,6 +5,7 @@ #include #include #include +#include #include #include "Def.h" #include "stdafx.h" @@ -116,6 +117,7 @@ public: Q_INVOKABLE NetworkParams* putJsonArray(const QString& url); Q_INVOKABLE NetworkParams* patchJsonArray(const QString& url); Q_INVOKABLE NetworkParams* deleteJsonArray(const QString& url); + Q_INVOKABLE void setInterceptor(QJSValue interceptor); void handle(NetworkParams* params,NetworkCallable* result); void handleDownload(NetworkParams* params,NetworkCallable* result); private: @@ -126,6 +128,8 @@ private: QString readCache(const QString& key); bool cacheExists(const QString& key); QString getCacheFilePath(const QString& key); +public: + QJSValue _interceptor; }; #endif // FLUNETWORK_H