FluentUI/src/FluHttp.h

61 lines
2.9 KiB
C
Raw Normal View History

2023-07-20 18:26:47 +08:00
#ifndef FLUHTTP_H
#define FLUHTTP_H
#include <QObject>
#include <QtQml/qqml.h>
2023-07-23 20:44:43 +08:00
#include <QFile>
2023-07-20 18:26:47 +08:00
#include <QNetworkAccessManager>
#include "stdafx.h"
2023-09-04 18:37:55 +08:00
#include <QMutex>
class HttpCallable : public QObject{
Q_OBJECT
QML_NAMED_ELEMENT(HttpCallable)
public:
explicit HttpCallable(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);
Q_SIGNAL void cache(QString result);
Q_SIGNAL void downloadProgress(qint64 recv, qint64 total);
Q_SIGNAL void uploadProgress(qint64 recv, qint64 total);
};
2023-07-20 18:26:47 +08:00
class FluHttp : public QObject
{
Q_OBJECT
2023-07-24 16:48:45 +08:00
Q_PROPERTY_AUTO(int,retry);
Q_PROPERTY_AUTO(int,timeout)
2023-09-01 18:38:21 +08:00
Q_PROPERTY_AUTO(int,cacheMode);
Q_PROPERTY_AUTO(QString,cacheDir);
2023-09-06 00:22:37 +08:00
Q_PROPERTY_AUTO(bool,breakPointDownload);
2023-07-20 18:26:47 +08:00
QML_NAMED_ELEMENT(FluHttp)
private:
2023-09-06 14:05:29 +08:00
QVariant invokeIntercept(QMap<QString, QVariant> request,Qt::ConnectionType type = Qt::BlockingQueuedConnection);
2023-09-01 18:38:21 +08:00
QMap<QString, QVariant> toRequest(const QString& url,const QVariant& params,const QVariant& headers,const QString& method);
2023-09-06 18:07:51 +08:00
QString toHttpId(const QMap<QString, QVariant>& map);
2023-07-23 20:44:43 +08:00
void addQueryParam(QUrl* url,const QMap<QString, QVariant>& params);
void addHeaders(QNetworkRequest* request,const QMap<QString, QVariant>& params);
2023-09-06 18:07:51 +08:00
void handleCache(const QString& httpId, const QString& result);
QString readCache(const QString& httpId);
bool cacheExists(const QString& httpId);
QString getCacheFilePath(const QString& httpId);
2023-07-20 18:26:47 +08:00
public:
explicit FluHttp(QObject *parent = nullptr);
2023-07-21 18:58:09 +08:00
~FluHttp();
2023-08-08 21:59:18 +08:00
//神坑!!! 如果参数使用QVariantMap会有问题在6.4.3版本中QML一调用就会编译失败。所以改用QMap<QString, QVariant>
2023-09-04 18:37:55 +08:00
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 = {});
Q_INVOKABLE void postString(QString url,HttpCallable* callable,QString params = "",QMap<QString, QVariant> headers = {});
Q_INVOKABLE void postJson(QString url,HttpCallable* callable,QMap<QString, QVariant> params = {},QMap<QString, QVariant> headers = {});
2023-09-05 18:38:16 +08:00
Q_INVOKABLE void download(QString url,HttpCallable* callable,QString savePath,QMap<QString, QVariant> params = {},QMap<QString, QVariant> headers = {});
2023-09-04 18:37:55 +08:00
Q_INVOKABLE void upload(QString url,HttpCallable* callable,QMap<QString, QVariant> params = {},QMap<QString, QVariant> headers = {});
2023-09-06 14:05:29 +08:00
Q_INVOKABLE qreal breakPointDownloadProgress(QString url,QString savePath,QMap<QString, QVariant> params = {},QMap<QString, QVariant> headers = {});
2023-07-21 18:58:09 +08:00
Q_INVOKABLE void cancel();
2023-07-23 20:44:43 +08:00
private:
2023-09-01 18:38:21 +08:00
QList<QPointer<QNetworkReply>> _cacheReply;
2023-07-20 18:26:47 +08:00
};
#endif // FLUHTTP_H