This commit is contained in:
zhuzichu 2023-11-29 10:41:48 +08:00
parent ce3da2e92f
commit 1047fac4d6
3 changed files with 36 additions and 18 deletions

View File

@ -41,6 +41,9 @@ Item {
} }
Component.onCompleted: { Component.onCompleted: {
FluNetwork.setInterceptor(function(param){
param.addHeader("Token","000000000000000000000")
})
FluApp.init(app) FluApp.init(app)
FluApp.useSystemAppBar = SettingsHelper.getUseSystemAppBar() FluApp.useSystemAppBar = SettingsHelper.getUseSystemAppBar()
FluApp.vsync = SettingsHelper.getVsync() FluApp.vsync = SettingsHelper.getVsync()

View File

@ -7,6 +7,9 @@
#include <QNetworkReply> #include <QNetworkReply>
#include <QJsonObject> #include <QJsonObject>
#include <QNetworkDiskCache> #include <QNetworkDiskCache>
#include <QQmlEngine>
#include <QQmlContext>
#include <QJSEngine>
#include <QJsonArray> #include <QJsonArray>
#include <QStandardPaths> #include <QStandardPaths>
#include <QDir> #include <QDir>
@ -132,11 +135,15 @@ QString NetworkParams::buildCacheKey(){
} }
void NetworkParams::go(NetworkCallable* callable){ void NetworkParams::go(NetworkCallable* callable){
QJSValueList data;
data<<qjsEngine(FluNetwork::getInstance())->newQObject(this);
FluNetwork::getInstance()->_interceptor.call(data);
if(_downloadParam){ if(_downloadParam){
FluNetwork::getInstance()->handleDownload(this,callable); FluNetwork::getInstance()->handleDownload(this,callable);
}else{ }else{
FluNetwork::getInstance()->handle(this,callable); FluNetwork::getInstance()->handle(this,callable);
} }
} }
void FluNetwork::handle(NetworkParams* params,NetworkCallable* c){ void FluNetwork::handle(NetworkParams* params,NetworkCallable* c){
@ -346,73 +353,77 @@ FluNetwork::FluNetwork(QObject *parent): QObject{parent}
} }
NetworkParams* FluNetwork::get(const QString& url){ 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){ 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){ 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){ 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){ 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){ 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){ 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){ 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){ 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){ 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){ 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){ 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){ 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){ 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){ 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){ 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){ 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){ 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;
} }

View File

@ -5,6 +5,7 @@
#include <QtQml/qqml.h> #include <QtQml/qqml.h>
#include <QFile> #include <QFile>
#include <QJsonValue> #include <QJsonValue>
#include <QJSValue>
#include <QNetworkAccessManager> #include <QNetworkAccessManager>
#include "Def.h" #include "Def.h"
#include "stdafx.h" #include "stdafx.h"
@ -116,6 +117,7 @@ public:
Q_INVOKABLE NetworkParams* putJsonArray(const QString& url); Q_INVOKABLE NetworkParams* putJsonArray(const QString& url);
Q_INVOKABLE NetworkParams* patchJsonArray(const QString& url); Q_INVOKABLE NetworkParams* patchJsonArray(const QString& url);
Q_INVOKABLE NetworkParams* deleteJsonArray(const QString& url); Q_INVOKABLE NetworkParams* deleteJsonArray(const QString& url);
Q_INVOKABLE void setInterceptor(QJSValue interceptor);
void handle(NetworkParams* params,NetworkCallable* result); void handle(NetworkParams* params,NetworkCallable* result);
void handleDownload(NetworkParams* params,NetworkCallable* result); void handleDownload(NetworkParams* params,NetworkCallable* result);
private: private:
@ -126,6 +128,8 @@ private:
QString readCache(const QString& key); QString readCache(const QString& key);
bool cacheExists(const QString& key); bool cacheExists(const QString& key);
QString getCacheFilePath(const QString& key); QString getCacheFilePath(const QString& key);
public:
QJSValue _interceptor;
}; };
#endif // FLUNETWORK_H #endif // FLUNETWORK_H