#ifndef FLUHTTP_H #define FLUHTTP_H #include #include #include #include #include "stdafx.h" #include 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); }; class FluHttp : public QObject { Q_OBJECT Q_PROPERTY_AUTO(int,retry); Q_PROPERTY_AUTO(int,timeout) Q_PROPERTY_AUTO(int,cacheMode); Q_PROPERTY_AUTO(QString,cacheDir); Q_PROPERTY_AUTO(bool,breakPointDownload); QML_NAMED_ELEMENT(FluHttp) private: QVariant invokeIntercept(QMap request); QMap toRequest(const QString& url,const QVariant& params,const QVariant& headers,const QString& method); void handleReply(QNetworkReply* reply); void addQueryParam(QUrl* url,const QMap& params); void addHeaders(QNetworkRequest* request,const QMap& params); void handleCache(QMap request, const QString& result); QString readCache(const QMap& request); bool cacheExists(const QMap& request); QString getCacheFilePath(const QMap& request); public: explicit FluHttp(QObject *parent = nullptr); ~FluHttp(); //神坑!!! 如果参数使用QVariantMap会有问题,在6.4.3版本中QML一调用就会编译失败。所以改用QMap Q_INVOKABLE void get(QString url,HttpCallable* callable,QMap params= {},QMap headers = {}); Q_INVOKABLE void post(QString url,HttpCallable* callable,QMap params= {},QMap headers = {}); Q_INVOKABLE void postString(QString url,HttpCallable* callable,QString params = "",QMap headers = {}); Q_INVOKABLE void postJson(QString url,HttpCallable* callable,QMap params = {},QMap headers = {}); Q_INVOKABLE void download(QString url,HttpCallable* callable,QString savePath,QMap params = {},QMap headers = {}); Q_INVOKABLE void upload(QString url,HttpCallable* callable,QMap params = {},QMap headers = {}); Q_INVOKABLE void cancel(); private: QList> _cacheReply; }; #endif // FLUHTTP_H