2023-11-23 18:18:28 +08:00
|
|
|
#ifndef FLUNETWORK_H
|
|
|
|
#define FLUNETWORK_H
|
|
|
|
|
|
|
|
#include <QObject>
|
|
|
|
#include <QtQml/qqml.h>
|
|
|
|
#include <QFile>
|
|
|
|
#include <QJsonValue>
|
2023-11-29 10:41:48 +08:00
|
|
|
#include <QJSValue>
|
2023-11-23 18:18:28 +08:00
|
|
|
#include <QNetworkAccessManager>
|
2023-12-08 23:30:41 +08:00
|
|
|
#include <QNetworkReply>
|
2023-11-24 15:35:40 +08:00
|
|
|
#include "Def.h"
|
2023-11-23 18:18:28 +08:00
|
|
|
#include "stdafx.h"
|
|
|
|
#include "singleton.h"
|
|
|
|
|
|
|
|
class NetworkCallable : public QObject{
|
|
|
|
Q_OBJECT
|
|
|
|
QML_NAMED_ELEMENT(FluNetworkCallable)
|
|
|
|
public:
|
|
|
|
explicit NetworkCallable(QObject *parent = nullptr);
|
|
|
|
Q_SIGNAL void start();
|
|
|
|
Q_SIGNAL void finish();
|
|
|
|
Q_SIGNAL void error(int status,QString errorString,QString result);
|
|
|
|
Q_SIGNAL void success(QString result);
|
2023-11-24 15:35:40 +08:00
|
|
|
Q_SIGNAL void cache(QString result);
|
2023-11-24 18:04:26 +08:00
|
|
|
Q_SIGNAL void uploadProgress(qint64 sent, qint64 total);
|
|
|
|
Q_SIGNAL void downloadProgress(qint64 recv, qint64 total);
|
|
|
|
};
|
|
|
|
|
|
|
|
class DownloadParam : public QObject{
|
|
|
|
Q_OBJECT
|
|
|
|
public:
|
|
|
|
explicit DownloadParam(QObject *parent = nullptr);
|
|
|
|
DownloadParam(QString destPath,bool append,QObject *parent = nullptr);
|
|
|
|
public:
|
|
|
|
QString _destPath;
|
|
|
|
bool _append;
|
2023-11-23 18:18:28 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
class NetworkParams : public QObject
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
QML_NAMED_ELEMENT(FluNetworkParams)
|
|
|
|
public:
|
|
|
|
enum Method{
|
|
|
|
METHOD_GET,
|
|
|
|
METHOD_HEAD,
|
|
|
|
METHOD_POST,
|
|
|
|
METHOD_PUT,
|
|
|
|
METHOD_PATCH,
|
|
|
|
METHOD_DELETE
|
|
|
|
};
|
|
|
|
enum Type{
|
|
|
|
TYPE_NONE,
|
|
|
|
TYPE_FORM,
|
|
|
|
TYPE_JSON,
|
|
|
|
TYPE_JSONARRAY,
|
|
|
|
TYPE_BODY
|
|
|
|
};
|
|
|
|
explicit NetworkParams(QObject *parent = nullptr);
|
|
|
|
NetworkParams(QString url,Type type,Method method,QObject *parent = nullptr);
|
|
|
|
Q_INVOKABLE NetworkParams* addQuery(QString key,QVariant val);
|
|
|
|
Q_INVOKABLE NetworkParams* addHeader(QString key,QVariant val);
|
|
|
|
Q_INVOKABLE NetworkParams* add(QString key,QVariant val);
|
2023-11-24 15:35:40 +08:00
|
|
|
Q_INVOKABLE NetworkParams* addFile(QString key,QVariant val);
|
2023-11-23 18:18:28 +08:00
|
|
|
Q_INVOKABLE NetworkParams* setBody(QString val);
|
2023-11-24 15:35:40 +08:00
|
|
|
Q_INVOKABLE NetworkParams* setTimeout(int val);
|
2023-11-23 18:18:28 +08:00
|
|
|
Q_INVOKABLE NetworkParams* setRetry(int val);
|
2023-11-24 15:35:40 +08:00
|
|
|
Q_INVOKABLE NetworkParams* setCacheMode(int val);
|
2023-11-29 18:10:56 +08:00
|
|
|
Q_INVOKABLE NetworkParams* toDownload(QString destPath,bool append = false);
|
2023-11-29 21:35:06 +08:00
|
|
|
Q_INVOKABLE NetworkParams* bind(QObject* target);
|
2023-12-07 23:01:09 +08:00
|
|
|
Q_INVOKABLE NetworkParams* openLog(QVariant val);
|
2023-11-23 18:18:28 +08:00
|
|
|
Q_INVOKABLE void go(NetworkCallable* result);
|
2023-11-24 15:35:40 +08:00
|
|
|
QString buildCacheKey();
|
2023-11-23 18:18:28 +08:00
|
|
|
QString method2String();
|
|
|
|
int getTimeout();
|
|
|
|
int getRetry();
|
2023-12-07 23:01:09 +08:00
|
|
|
bool getOpenLog();
|
2023-11-23 18:18:28 +08:00
|
|
|
public:
|
2023-11-24 18:04:26 +08:00
|
|
|
DownloadParam* _downloadParam = nullptr;
|
2023-11-29 21:35:06 +08:00
|
|
|
QObject* _target = nullptr;
|
2023-11-23 18:18:28 +08:00
|
|
|
Method _method;
|
|
|
|
Type _type;
|
|
|
|
QString _url;
|
|
|
|
QString _body;
|
|
|
|
QMap<QString, QVariant> _queryMap;
|
|
|
|
QMap<QString, QVariant> _headerMap;
|
|
|
|
QMap<QString, QVariant> _paramMap;
|
2023-11-24 15:35:40 +08:00
|
|
|
QMap<QString, QVariant> _fileMap;
|
2023-11-23 18:18:28 +08:00
|
|
|
int _timeout = -1;
|
|
|
|
int _retry = -1;
|
2023-12-07 23:01:09 +08:00
|
|
|
QVariant _openLog;
|
2023-11-24 15:35:40 +08:00
|
|
|
int _cacheMode = FluNetworkType::CacheMode::NoCache;
|
2023-11-23 18:18:28 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
class FluNetwork : public QObject
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
Q_PROPERTY_AUTO(int,timeout)
|
|
|
|
Q_PROPERTY_AUTO(int,retry)
|
2023-11-24 15:35:40 +08:00
|
|
|
Q_PROPERTY_AUTO(QString,cacheDir)
|
2023-12-07 18:13:58 +08:00
|
|
|
Q_PROPERTY_AUTO(bool,openLog)
|
2023-11-23 18:18:28 +08:00
|
|
|
QML_NAMED_ELEMENT(FluNetwork)
|
|
|
|
QML_SINGLETON
|
|
|
|
private:
|
|
|
|
explicit FluNetwork(QObject *parent = nullptr);
|
|
|
|
public:
|
2024-01-04 00:08:34 +08:00
|
|
|
SINGLETON(FluNetwork)
|
2023-11-23 18:18:28 +08:00
|
|
|
static FluNetwork *create(QQmlEngine *qmlEngine, QJSEngine *jsEngine){return getInstance();}
|
|
|
|
Q_INVOKABLE NetworkParams* get(const QString& url);
|
|
|
|
Q_INVOKABLE NetworkParams* head(const QString& url);
|
|
|
|
Q_INVOKABLE NetworkParams* postBody(const QString& url);
|
|
|
|
Q_INVOKABLE NetworkParams* putBody(const QString& url);
|
|
|
|
Q_INVOKABLE NetworkParams* patchBody(const QString& url);
|
|
|
|
Q_INVOKABLE NetworkParams* deleteBody(const QString& url);
|
|
|
|
Q_INVOKABLE NetworkParams* postForm(const QString& url);
|
|
|
|
Q_INVOKABLE NetworkParams* putForm(const QString& url);
|
|
|
|
Q_INVOKABLE NetworkParams* patchForm(const QString& url);
|
|
|
|
Q_INVOKABLE NetworkParams* deleteForm(const QString& url);
|
|
|
|
Q_INVOKABLE NetworkParams* postJson(const QString& url);
|
|
|
|
Q_INVOKABLE NetworkParams* putJson(const QString& url);
|
|
|
|
Q_INVOKABLE NetworkParams* patchJson(const QString& url);
|
|
|
|
Q_INVOKABLE NetworkParams* deleteJson(const QString& url);
|
|
|
|
Q_INVOKABLE NetworkParams* postJsonArray(const QString& url);
|
|
|
|
Q_INVOKABLE NetworkParams* putJsonArray(const QString& url);
|
|
|
|
Q_INVOKABLE NetworkParams* patchJsonArray(const QString& url);
|
|
|
|
Q_INVOKABLE NetworkParams* deleteJsonArray(const QString& url);
|
2023-11-29 10:41:48 +08:00
|
|
|
Q_INVOKABLE void setInterceptor(QJSValue interceptor);
|
2023-11-23 18:18:28 +08:00
|
|
|
void handle(NetworkParams* params,NetworkCallable* result);
|
2023-11-24 18:04:26 +08:00
|
|
|
void handleDownload(NetworkParams* params,NetworkCallable* result);
|
2023-11-23 18:18:28 +08:00
|
|
|
private:
|
2023-12-07 18:13:58 +08:00
|
|
|
void sendRequest(QNetworkAccessManager* manager,QNetworkRequest request,NetworkParams* params,QNetworkReply*& reply,bool isFirst,QPointer<NetworkCallable> callable);
|
2023-11-23 18:18:28 +08:00
|
|
|
void addQueryParam(QUrl* url,const QMap<QString, QVariant>& params);
|
|
|
|
void addHeaders(QNetworkRequest* request,const QMap<QString, QVariant>& headers);
|
2023-11-24 15:35:40 +08:00
|
|
|
void saveResponse(QString key,QString response);
|
|
|
|
QString readCache(const QString& key);
|
|
|
|
bool cacheExists(const QString& key);
|
|
|
|
QString getCacheFilePath(const QString& key);
|
2023-12-07 18:13:58 +08:00
|
|
|
QString map2String(const QMap<QString, QVariant>& map);
|
2023-12-08 23:30:41 +08:00
|
|
|
QString headerList2String(const QList<QNetworkReply::RawHeaderPair>& data);
|
2023-12-07 18:13:58 +08:00
|
|
|
void printRequestStartLog(QNetworkRequest request,NetworkParams* params);
|
|
|
|
void printRequestEndLog(QNetworkRequest request,NetworkParams* params,QNetworkReply*& reply,const QString& response);
|
2023-11-29 10:41:48 +08:00
|
|
|
public:
|
|
|
|
QJSValue _interceptor;
|
2023-11-23 18:18:28 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif // FLUNETWORK_H
|