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"
|
|
|
|
|
|
|
|
|
|
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-07-20 18:26:47 +08:00
|
|
|
|
QML_NAMED_ELEMENT(FluHttp)
|
|
|
|
|
private:
|
|
|
|
|
QVariant invokeIntercept(const QVariant& params,const QVariant& headers,const QString& method);
|
2023-07-21 18:58:09 +08:00
|
|
|
|
void handleReply(QNetworkReply* reply);
|
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-07-26 17:48:35 +08:00
|
|
|
|
void onStart(const QJSValue& callable);
|
|
|
|
|
void onFinish(const QJSValue& callable);
|
2023-08-16 13:14:00 +08:00
|
|
|
|
void onError(const QJSValue& callable,int status,QString errorString,QString result);
|
2023-07-26 17:48:35 +08:00
|
|
|
|
void onSuccess(const QJSValue& callable,QString result);
|
|
|
|
|
void onDownloadProgress(const QJSValue& callable,qint64 recv, qint64 total);
|
2023-08-16 13:14:00 +08:00
|
|
|
|
void onUploadProgress(const QJSValue& callable,qint64 recv, qint64 total);
|
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-08-16 13:14:00 +08:00
|
|
|
|
Q_INVOKABLE void get(QString url,QJSValue callable,QMap<QString, QVariant> params= {},QMap<QString, QVariant> headers = {});
|
|
|
|
|
Q_INVOKABLE void post(QString url,QJSValue callable,QMap<QString, QVariant> params= {},QMap<QString, QVariant> headers = {});
|
2023-08-08 21:57:08 +08:00
|
|
|
|
Q_INVOKABLE void postString(QString url,QJSValue callable,QString params = "",QMap<QString, QVariant> headers = {});
|
|
|
|
|
Q_INVOKABLE void postJson(QString url,QJSValue callable,QMap<QString, QVariant> params = {},QMap<QString, QVariant> headers = {});
|
|
|
|
|
Q_INVOKABLE void download(QString url,QJSValue callable,QString filePath,QMap<QString, QVariant> params = {},QMap<QString, QVariant> headers = {});
|
2023-08-16 13:14:00 +08:00
|
|
|
|
Q_INVOKABLE void upload(QString url,QJSValue callable,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:
|
|
|
|
|
QList<QPointer<QNetworkReply>> _cache;
|
2023-07-20 18:26:47 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
#endif // FLUHTTP_H
|