mirror of
https://github.com/zhuzichu520/FluentUI.git
synced 2024-11-26 21:37:04 +08:00
update
This commit is contained in:
parent
531bffdf1a
commit
c92d807ec1
@ -88,7 +88,10 @@ FluObject{
|
|||||||
}
|
}
|
||||||
url:"qrc:/example/qml/page/T_Text.qml"
|
url:"qrc:/example/qml/page/T_Text.qml"
|
||||||
onDropped:{ FluApp.navigate("/pageWindow",{title:title,url:url}) }
|
onDropped:{ FluApp.navigate("/pageWindow",{title:title,url:url}) }
|
||||||
onTap:{ navigationView.push(url) }
|
onTap:{
|
||||||
|
item_text.count = 0
|
||||||
|
navigationView.push(url)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
FluPaneItem{
|
FluPaneItem{
|
||||||
title:"Image"
|
title:"Image"
|
||||||
|
@ -309,7 +309,6 @@ CustomWindow {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
HttpCallable{
|
HttpCallable{
|
||||||
id:callable
|
id:callable
|
||||||
onStart: {
|
onStart: {
|
||||||
@ -335,8 +334,21 @@ CustomWindow {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// HttpRequest{
|
||||||
|
// id:reuqest
|
||||||
|
// url: "https://api.github.com/repos/zhuzichu520/FluentUI/releases/latest"
|
||||||
|
// }
|
||||||
|
|
||||||
|
|
||||||
function checkUpdate(){
|
function checkUpdate(){
|
||||||
http.get("https://api.github.com/repos/zhuzichu520/FluentUI/releases/latest",callable)
|
var request = http.newRequest()
|
||||||
|
console.debug("-------------------->"+request)
|
||||||
|
request.url = "https://api.github.com/repos/zhuzichu520/FluentUI/releases/latest"
|
||||||
|
|
||||||
|
console.debug("-------------------->"+request.url)
|
||||||
|
http.get2(request,callable);
|
||||||
|
// http.get("https://api.github.com/repos/zhuzichu520/FluentUI/releases/latest",callable)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -88,7 +88,10 @@ FluObject{
|
|||||||
}
|
}
|
||||||
url:"qrc:/example/qml/page/T_Text.qml"
|
url:"qrc:/example/qml/page/T_Text.qml"
|
||||||
onDropped:{ FluApp.navigate("/pageWindow",{title:title,url:url}) }
|
onDropped:{ FluApp.navigate("/pageWindow",{title:title,url:url}) }
|
||||||
onTap:{ navigationView.push(url) }
|
onTap:{
|
||||||
|
item_text.count = 0
|
||||||
|
navigationView.push(url)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
FluPaneItem{
|
FluPaneItem{
|
||||||
title:"Image"
|
title:"Image"
|
||||||
|
230
src/FluHttp.cpp
230
src/FluHttp.cpp
@ -5,6 +5,7 @@
|
|||||||
#include <QNetworkReply>
|
#include <QNetworkReply>
|
||||||
#include <QUrlQuery>
|
#include <QUrlQuery>
|
||||||
#include <QHttpMultiPart>
|
#include <QHttpMultiPart>
|
||||||
|
#include <QGuiApplication>
|
||||||
#include <QJsonDocument>
|
#include <QJsonDocument>
|
||||||
#include <QStandardPaths>
|
#include <QStandardPaths>
|
||||||
#include <QTextStream>
|
#include <QTextStream>
|
||||||
@ -13,6 +14,13 @@
|
|||||||
#include "FluApp.h"
|
#include "FluApp.h"
|
||||||
#include "FluTools.h"
|
#include "FluTools.h"
|
||||||
|
|
||||||
|
HttpRequest::HttpRequest(QObject *parent)
|
||||||
|
: QObject{parent}
|
||||||
|
{
|
||||||
|
params({});
|
||||||
|
headers({});
|
||||||
|
}
|
||||||
|
|
||||||
HttpCallable::HttpCallable(QObject *parent)
|
HttpCallable::HttpCallable(QObject *parent)
|
||||||
: QObject{parent}
|
: QObject{parent}
|
||||||
{
|
{
|
||||||
@ -41,18 +49,18 @@ void FluHttp::cancel(){
|
|||||||
}
|
}
|
||||||
|
|
||||||
void FluHttp::post(QString url,HttpCallable* callable,QMap<QString, QVariant> params,QMap<QString, QVariant> headers){
|
void FluHttp::post(QString url,HttpCallable* callable,QMap<QString, QVariant> params,QMap<QString, QVariant> headers){
|
||||||
|
auto requestMap = toRequest(url,params,headers,"post");
|
||||||
|
auto httpId = toHttpId(requestMap);
|
||||||
|
QMap<QString, QVariant> data = invokeIntercept(requestMap).toMap();
|
||||||
QThreadPool::globalInstance()->start([=](){
|
QThreadPool::globalInstance()->start([=](){
|
||||||
auto requestMap = toRequest(url,params,headers,"post");
|
onStart(callable);
|
||||||
auto httpId = toHttpId(requestMap);
|
|
||||||
QMap<QString, QVariant> data = invokeIntercept(requestMap).toMap();
|
|
||||||
Q_EMIT callable->start();
|
|
||||||
if(_cacheMode == FluHttpType::CacheMode::IfNoneCacheRequest && cacheExists(httpId)){
|
if(_cacheMode == FluHttpType::CacheMode::IfNoneCacheRequest && cacheExists(httpId)){
|
||||||
Q_EMIT callable->cache(readCache(httpId));
|
onCache(callable,readCache(httpId));
|
||||||
Q_EMIT callable->finish();
|
onFinish(callable);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if(_cacheMode == FluHttpType::CacheMode::FirstCacheThenRequest && cacheExists(httpId)){
|
if(_cacheMode == FluHttpType::CacheMode::FirstCacheThenRequest && cacheExists(httpId)){
|
||||||
Q_EMIT callable->cache(readCache(httpId));
|
onCache(callable,readCache(httpId));
|
||||||
}
|
}
|
||||||
for (int i = 0; i < retry(); ++i) {
|
for (int i = 0; i < retry(); ++i) {
|
||||||
QNetworkAccessManager manager;
|
QNetworkAccessManager manager;
|
||||||
@ -74,9 +82,8 @@ void FluHttp::post(QString url,HttpCallable* callable,QMap<QString, QVariant> pa
|
|||||||
QEventLoop loop;
|
QEventLoop loop;
|
||||||
QNetworkReply* reply = manager.post(request,&multiPart);
|
QNetworkReply* reply = manager.post(request,&multiPart);
|
||||||
_cacheReply.append(reply);
|
_cacheReply.append(reply);
|
||||||
connect(&manager,&QNetworkAccessManager::finished,&manager,[&loop](QNetworkReply *reply){
|
connect(&manager,&QNetworkAccessManager::finished,&manager,[&loop](QNetworkReply *reply){loop.quit();});
|
||||||
loop.quit();
|
connect(qApp,&QGuiApplication::aboutToQuit,&manager, [&loop](){loop.quit();});
|
||||||
});
|
|
||||||
loop.exec();
|
loop.exec();
|
||||||
QString result = QString::fromUtf8(reply->readAll());
|
QString result = QString::fromUtf8(reply->readAll());
|
||||||
int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
|
int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
|
||||||
@ -86,34 +93,34 @@ void FluHttp::post(QString url,HttpCallable* callable,QMap<QString, QVariant> pa
|
|||||||
reply = nullptr;
|
reply = nullptr;
|
||||||
if (isSuccess) {
|
if (isSuccess) {
|
||||||
handleCache(httpId,result);
|
handleCache(httpId,result);
|
||||||
Q_EMIT callable->success(result);
|
onSuccess(callable,result);
|
||||||
break;
|
break;
|
||||||
}else{
|
}else{
|
||||||
if(i == retry()-1){
|
if(i == retry()-1){
|
||||||
if(_cacheMode == FluHttpType::CacheMode::RequestFailedReadCache && cacheExists(httpId)){
|
if(_cacheMode == FluHttpType::CacheMode::RequestFailedReadCache && cacheExists(httpId)){
|
||||||
Q_EMIT callable->cache(readCache(httpId));
|
onCache(callable,readCache(httpId));
|
||||||
}
|
}
|
||||||
Q_EMIT callable->error(status,errorString,result);
|
onError(callable,status,errorString,result);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Q_EMIT callable->finish();
|
onFinish(callable);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
void FluHttp::postString(QString url,HttpCallable* callable,QString params,QMap<QString, QVariant> headers){
|
void FluHttp::postString(QString url,HttpCallable* callable,QString params,QMap<QString, QVariant> headers){
|
||||||
|
auto requestMap = toRequest(url,params,headers,"postString");
|
||||||
|
auto httpId = toHttpId(requestMap);
|
||||||
|
QMap<QString, QVariant> data = invokeIntercept(requestMap).toMap();
|
||||||
QThreadPool::globalInstance()->start([=](){
|
QThreadPool::globalInstance()->start([=](){
|
||||||
auto requestMap = toRequest(url,params,headers,"postString");
|
onStart(callable);
|
||||||
auto httpId = toHttpId(requestMap);
|
|
||||||
QMap<QString, QVariant> data = invokeIntercept(requestMap).toMap();
|
|
||||||
Q_EMIT callable->start();
|
|
||||||
if(_cacheMode == FluHttpType::CacheMode::IfNoneCacheRequest && cacheExists(httpId)){
|
if(_cacheMode == FluHttpType::CacheMode::IfNoneCacheRequest && cacheExists(httpId)){
|
||||||
Q_EMIT callable->cache(readCache(httpId));
|
onCache(callable,readCache(httpId));
|
||||||
Q_EMIT callable->finish();
|
onFinish(callable);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if(_cacheMode == FluHttpType::CacheMode::FirstCacheThenRequest && cacheExists(httpId)){
|
if(_cacheMode == FluHttpType::CacheMode::FirstCacheThenRequest && cacheExists(httpId)){
|
||||||
Q_EMIT callable->cache(readCache(httpId));
|
onCache(callable,readCache(httpId));
|
||||||
}
|
}
|
||||||
for (int i = 0; i < retry(); ++i) {
|
for (int i = 0; i < retry(); ++i) {
|
||||||
QNetworkAccessManager manager;
|
QNetworkAccessManager manager;
|
||||||
@ -126,9 +133,8 @@ void FluHttp::postString(QString url,HttpCallable* callable,QString params,QMap<
|
|||||||
QEventLoop loop;
|
QEventLoop loop;
|
||||||
QNetworkReply* reply = manager.post(request,params.toUtf8());
|
QNetworkReply* reply = manager.post(request,params.toUtf8());
|
||||||
_cacheReply.append(reply);
|
_cacheReply.append(reply);
|
||||||
connect(&manager,&QNetworkAccessManager::finished,&manager,[&loop](QNetworkReply *reply){
|
connect(&manager,&QNetworkAccessManager::finished,&manager,[&loop](QNetworkReply *reply){loop.quit();});
|
||||||
loop.quit();
|
connect(qApp,&QGuiApplication::aboutToQuit,&manager, [&loop](){loop.quit();});
|
||||||
});
|
|
||||||
loop.exec();
|
loop.exec();
|
||||||
QString result = QString::fromUtf8(reply->readAll());
|
QString result = QString::fromUtf8(reply->readAll());
|
||||||
int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
|
int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
|
||||||
@ -138,34 +144,34 @@ void FluHttp::postString(QString url,HttpCallable* callable,QString params,QMap<
|
|||||||
reply = nullptr;
|
reply = nullptr;
|
||||||
if (isSuccess) {
|
if (isSuccess) {
|
||||||
handleCache(httpId,result);
|
handleCache(httpId,result);
|
||||||
Q_EMIT callable->success(result);
|
onSuccess(callable,result);
|
||||||
break;
|
break;
|
||||||
}else{
|
}else{
|
||||||
if(i == retry()-1){
|
if(i == retry()-1){
|
||||||
if(_cacheMode == FluHttpType::CacheMode::RequestFailedReadCache && cacheExists(httpId)){
|
if(_cacheMode == FluHttpType::CacheMode::RequestFailedReadCache && cacheExists(httpId)){
|
||||||
Q_EMIT callable->cache(readCache(httpId));
|
onCache(callable,readCache(httpId));
|
||||||
}
|
}
|
||||||
Q_EMIT callable->error(status,errorString,result);
|
onError(callable,status,errorString,result);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Q_EMIT callable->finish();
|
onFinish(callable);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
void FluHttp::postJson(QString url,HttpCallable* callable,QMap<QString, QVariant> params,QMap<QString, QVariant> headers){
|
void FluHttp::postJson(QString url,HttpCallable* callable,QMap<QString, QVariant> params,QMap<QString, QVariant> headers){
|
||||||
|
auto requestMap = toRequest(url,params,headers,"postJson");
|
||||||
|
auto httpId = toHttpId(requestMap);
|
||||||
|
QMap<QString, QVariant> data = invokeIntercept(requestMap).toMap();
|
||||||
QThreadPool::globalInstance()->start([=](){
|
QThreadPool::globalInstance()->start([=](){
|
||||||
auto requestMap = toRequest(url,params,headers,"postJson");
|
onStart(callable);
|
||||||
auto httpId = toHttpId(requestMap);
|
|
||||||
QMap<QString, QVariant> data = invokeIntercept(requestMap).toMap();
|
|
||||||
Q_EMIT callable->start();
|
|
||||||
if(_cacheMode == FluHttpType::CacheMode::IfNoneCacheRequest && cacheExists(httpId)){
|
if(_cacheMode == FluHttpType::CacheMode::IfNoneCacheRequest && cacheExists(httpId)){
|
||||||
Q_EMIT callable->cache(readCache(httpId));
|
onCache(callable,readCache(httpId));
|
||||||
Q_EMIT callable->finish();
|
onFinish(callable);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if(_cacheMode == FluHttpType::CacheMode::FirstCacheThenRequest && cacheExists(httpId)){
|
if(_cacheMode == FluHttpType::CacheMode::FirstCacheThenRequest && cacheExists(httpId)){
|
||||||
Q_EMIT callable->cache(readCache(httpId));
|
onCache(callable,readCache(httpId));
|
||||||
}
|
}
|
||||||
for (int i = 0; i < retry(); ++i) {
|
for (int i = 0; i < retry(); ++i) {
|
||||||
QNetworkAccessManager manager;
|
QNetworkAccessManager manager;
|
||||||
@ -178,9 +184,8 @@ void FluHttp::postJson(QString url,HttpCallable* callable,QMap<QString, QVariant
|
|||||||
QEventLoop loop;
|
QEventLoop loop;
|
||||||
QNetworkReply* reply = manager.post(request,QJsonDocument::fromVariant(data["params"]).toJson());
|
QNetworkReply* reply = manager.post(request,QJsonDocument::fromVariant(data["params"]).toJson());
|
||||||
_cacheReply.append(reply);
|
_cacheReply.append(reply);
|
||||||
connect(&manager,&QNetworkAccessManager::finished,&manager,[&loop](QNetworkReply *reply){
|
connect(&manager,&QNetworkAccessManager::finished,&manager,[&loop](QNetworkReply *reply){loop.quit();});
|
||||||
loop.quit();
|
connect(qApp,&QGuiApplication::aboutToQuit,&manager, [&loop](){loop.quit();});
|
||||||
});
|
|
||||||
loop.exec();
|
loop.exec();
|
||||||
QString result = QString::fromUtf8(reply->readAll());
|
QString result = QString::fromUtf8(reply->readAll());
|
||||||
int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
|
int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
|
||||||
@ -190,33 +195,40 @@ void FluHttp::postJson(QString url,HttpCallable* callable,QMap<QString, QVariant
|
|||||||
reply = nullptr;
|
reply = nullptr;
|
||||||
if (isSuccess) {
|
if (isSuccess) {
|
||||||
handleCache(httpId,result);
|
handleCache(httpId,result);
|
||||||
Q_EMIT callable->success(result);
|
onSuccess(callable,result);
|
||||||
break;
|
break;
|
||||||
}else{
|
}else{
|
||||||
if(i == retry()-1){
|
if(i == retry()-1){
|
||||||
if(_cacheMode == FluHttpType::CacheMode::RequestFailedReadCache && cacheExists(httpId)){
|
if(_cacheMode == FluHttpType::CacheMode::RequestFailedReadCache && cacheExists(httpId)){
|
||||||
Q_EMIT callable->cache(readCache(httpId));
|
onCache(callable,readCache(httpId));
|
||||||
}
|
}
|
||||||
Q_EMIT callable->error(status,errorString,result);
|
onError(callable,status,errorString,result);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Q_EMIT callable->finish();
|
onFinish(callable);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void FluHttp::get2(HttpRequest* request,HttpCallable* callable){
|
||||||
|
QString url = request->url();
|
||||||
|
QMap<QString, QVariant> params = request->params().toMap();
|
||||||
|
QMap<QString, QVariant> headers = request->headers().toMap();
|
||||||
|
get(url,callable,params,headers);
|
||||||
|
}
|
||||||
|
|
||||||
void FluHttp::get(QString url,HttpCallable* callable,QMap<QString, QVariant> params,QMap<QString, QVariant> headers){
|
void FluHttp::get(QString url,HttpCallable* callable,QMap<QString, QVariant> params,QMap<QString, QVariant> headers){
|
||||||
|
auto requestMap = toRequest(url,params,headers,"get");
|
||||||
|
auto httpId = toHttpId(requestMap);
|
||||||
|
QMap<QString, QVariant> data = invokeIntercept(requestMap).toMap();
|
||||||
QThreadPool::globalInstance()->start([=](){
|
QThreadPool::globalInstance()->start([=](){
|
||||||
auto requestMap = toRequest(url,params,headers,"get");
|
onStart(callable);
|
||||||
auto httpId = toHttpId(requestMap);
|
|
||||||
QMap<QString, QVariant> data = invokeIntercept(requestMap).toMap();
|
|
||||||
Q_EMIT callable->start();
|
|
||||||
if(_cacheMode == FluHttpType::CacheMode::FirstCacheThenRequest && cacheExists(httpId)){
|
if(_cacheMode == FluHttpType::CacheMode::FirstCacheThenRequest && cacheExists(httpId)){
|
||||||
Q_EMIT callable->cache(readCache(httpId));
|
onCache(callable,readCache(httpId));
|
||||||
}
|
}
|
||||||
if(_cacheMode == FluHttpType::CacheMode::IfNoneCacheRequest && cacheExists(httpId)){
|
if(_cacheMode == FluHttpType::CacheMode::IfNoneCacheRequest && cacheExists(httpId)){
|
||||||
Q_EMIT callable->cache(readCache(httpId));
|
onCache(callable,readCache(httpId));
|
||||||
Q_EMIT callable->finish();
|
onFinish(callable);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
for (int i = 0; i < retry(); ++i) {
|
for (int i = 0; i < retry(); ++i) {
|
||||||
@ -229,40 +241,39 @@ void FluHttp::get(QString url,HttpCallable* callable,QMap<QString, QVariant> par
|
|||||||
QEventLoop loop;
|
QEventLoop loop;
|
||||||
QNetworkReply* reply = manager.get(request);
|
QNetworkReply* reply = manager.get(request);
|
||||||
_cacheReply.append(reply);
|
_cacheReply.append(reply);
|
||||||
connect(&manager,&QNetworkAccessManager::finished,&manager,[&loop](QNetworkReply *reply){
|
connect(&manager,&QNetworkAccessManager::finished,&manager,[&loop](QNetworkReply *reply){loop.quit();});
|
||||||
loop.quit();
|
connect(qApp,&QGuiApplication::aboutToQuit,&manager, [&loop](){loop.quit();});
|
||||||
});
|
|
||||||
loop.exec();
|
loop.exec();
|
||||||
QString result = QString::fromUtf8(reply->readAll());
|
|
||||||
int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
|
int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
|
||||||
QString errorString = reply->errorString();
|
QString errorString = reply->errorString();
|
||||||
bool isSuccess = reply->error() == QNetworkReply::NoError;
|
bool isSuccess = reply->error() == QNetworkReply::NoError;
|
||||||
reply->deleteLater();
|
QString result = QString::fromUtf8(reply->readAll());
|
||||||
reply = nullptr;
|
|
||||||
if (isSuccess) {
|
if (isSuccess) {
|
||||||
handleCache(httpId,result);
|
handleCache(httpId,result);
|
||||||
Q_EMIT callable->success(result);
|
onSuccess(callable,result);
|
||||||
break;
|
break;
|
||||||
}else{
|
}else{
|
||||||
if(i == retry()-1){
|
if(i == retry()-1){
|
||||||
if(_cacheMode == FluHttpType::CacheMode::RequestFailedReadCache && cacheExists(httpId)){
|
if(_cacheMode == FluHttpType::CacheMode::RequestFailedReadCache && cacheExists(httpId)){
|
||||||
Q_EMIT callable->cache(readCache(httpId));
|
onCache(callable,readCache(httpId));
|
||||||
}
|
}
|
||||||
Q_EMIT callable->error(status,errorString,result);
|
onError(callable,status,errorString,result);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
reply->deleteLater();
|
||||||
|
reply = nullptr;
|
||||||
}
|
}
|
||||||
Q_EMIT callable->finish();
|
onFinish(callable);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
void FluHttp::download(QString url,HttpCallable* callable,QString savePath,QMap<QString, QVariant> params,QMap<QString, QVariant> headers){
|
void FluHttp::download(QString url,HttpCallable* callable,QString savePath,QMap<QString, QVariant> params,QMap<QString, QVariant> headers){
|
||||||
|
auto requestMap = toRequest(url,params,headers,"download");
|
||||||
|
requestMap.insert("savePath",savePath);
|
||||||
|
auto httpId = toHttpId(requestMap);
|
||||||
|
QMap<QString, QVariant> data = invokeIntercept(requestMap).toMap();
|
||||||
QThreadPool::globalInstance()->start([=](){
|
QThreadPool::globalInstance()->start([=](){
|
||||||
auto requestMap = toRequest(url,params,headers,"download");
|
onStart(callable);
|
||||||
requestMap.insert("savePath",savePath);
|
|
||||||
auto httpId = toHttpId(requestMap);
|
|
||||||
QMap<QString, QVariant> data = invokeIntercept(requestMap).toMap();
|
|
||||||
Q_EMIT callable->start();
|
|
||||||
QNetworkAccessManager manager;
|
QNetworkAccessManager manager;
|
||||||
QUrl _url(url);
|
QUrl _url(url);
|
||||||
addQueryParam(&_url,data["params"].toMap());
|
addQueryParam(&_url,data["params"].toMap());
|
||||||
@ -274,9 +285,8 @@ void FluHttp::download(QString url,HttpCallable* callable,QString savePath,QMap<
|
|||||||
dir.mkpath(dir.path());
|
dir.mkpath(dir.path());
|
||||||
}
|
}
|
||||||
QEventLoop loop;
|
QEventLoop loop;
|
||||||
connect(&manager,&QNetworkAccessManager::finished,&manager,[&loop](QNetworkReply *reply){
|
connect(&manager,&QNetworkAccessManager::finished,&manager,[&loop](QNetworkReply *reply){loop.quit();});
|
||||||
loop.quit();
|
connect(qApp,&QGuiApplication::aboutToQuit,&manager, [&loop](){loop.quit();});
|
||||||
});
|
|
||||||
qint64 seek = 0;
|
qint64 seek = 0;
|
||||||
auto filePath = getCacheFilePath(httpId);
|
auto filePath = getCacheFilePath(httpId);
|
||||||
QSharedPointer<QFile> fileCache(new QFile(filePath));
|
QSharedPointer<QFile> fileCache(new QFile(filePath));
|
||||||
@ -285,9 +295,9 @@ void FluHttp::download(QString url,HttpCallable* callable,QString savePath,QMap<
|
|||||||
qint64 fileSize = cacheInfo.value("fileSize").toDouble();
|
qint64 fileSize = cacheInfo.value("fileSize").toDouble();
|
||||||
qint64 contentLength = cacheInfo.value("contentLength").toDouble();
|
qint64 contentLength = cacheInfo.value("contentLength").toDouble();
|
||||||
if(fileSize == contentLength && file->size() == contentLength){
|
if(fileSize == contentLength && file->size() == contentLength){
|
||||||
Q_EMIT callable->downloadProgress(fileSize,contentLength);
|
onDownloadProgress(callable,fileSize,contentLength);
|
||||||
Q_EMIT callable->success(savePath);
|
onSuccess(callable,savePath);
|
||||||
Q_EMIT callable->finish();
|
onFinish(callable);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if(fileSize==file->size()){
|
if(fileSize==file->size()){
|
||||||
@ -306,7 +316,7 @@ void FluHttp::download(QString url,HttpCallable* callable,QString savePath,QMap<
|
|||||||
{
|
{
|
||||||
qDebug()<<"FileCache Error";
|
qDebug()<<"FileCache Error";
|
||||||
}
|
}
|
||||||
connect(reply,&QNetworkReply::readyRead,reply,[reply,file,fileCache,requestMap,callable,seek]{
|
connect(reply,&QNetworkReply::readyRead,reply,[reply,file,fileCache,requestMap,callable,seek,this]{
|
||||||
if (!reply || !file || reply->error() != QNetworkReply::NoError)
|
if (!reply || !file || reply->error() != QNetworkReply::NoError)
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
@ -322,25 +332,25 @@ void FluHttp::download(QString url,HttpCallable* callable,QString savePath,QMap<
|
|||||||
fileCache->resize(0);
|
fileCache->resize(0);
|
||||||
fileCache->write(FluTools::getInstance()->toBase64(QJsonDocument::fromVariant(QVariant(downMap)).toJson()).toUtf8());
|
fileCache->write(FluTools::getInstance()->toBase64(QJsonDocument::fromVariant(QVariant(downMap)).toJson()).toUtf8());
|
||||||
fileCache->flush();
|
fileCache->flush();
|
||||||
Q_EMIT callable->downloadProgress(file->size(),contentLength);
|
onDownloadProgress(callable,file->size(),contentLength);
|
||||||
});
|
});
|
||||||
loop.exec();
|
loop.exec();
|
||||||
if (reply->error() == QNetworkReply::NoError) {
|
if (reply->error() == QNetworkReply::NoError) {
|
||||||
Q_EMIT callable->success(savePath);
|
onSuccess(callable,savePath);
|
||||||
}else{
|
}else{
|
||||||
Q_EMIT callable->error(reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(),reply->errorString(),"");
|
onError(callable,reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(),reply->errorString(),"");
|
||||||
}
|
}
|
||||||
reply->deleteLater();
|
reply->deleteLater();
|
||||||
reply = nullptr;
|
reply = nullptr;
|
||||||
Q_EMIT callable->finish();
|
onFinish(callable);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
void FluHttp::upload(QString url,HttpCallable* callable,QMap<QString, QVariant> params,QMap<QString, QVariant> headers){
|
void FluHttp::upload(QString url,HttpCallable* callable,QMap<QString, QVariant> params,QMap<QString, QVariant> headers){
|
||||||
|
auto requestMap = toRequest(url,params,headers,"upload");
|
||||||
|
QMap<QString, QVariant> data = invokeIntercept(requestMap).toMap();
|
||||||
QThreadPool::globalInstance()->start([=](){
|
QThreadPool::globalInstance()->start([=](){
|
||||||
auto requestMap = toRequest(url,params,headers,"upload");
|
onStart(callable);
|
||||||
QMap<QString, QVariant> data = invokeIntercept(requestMap).toMap();
|
|
||||||
Q_EMIT callable->start();
|
|
||||||
QNetworkAccessManager manager;
|
QNetworkAccessManager manager;
|
||||||
manager.setTransferTimeout(timeout());
|
manager.setTransferTimeout(timeout());
|
||||||
QUrl _url(url);
|
QUrl _url(url);
|
||||||
@ -364,11 +374,10 @@ void FluHttp::upload(QString url,HttpCallable* callable,QMap<QString, QVariant>
|
|||||||
QEventLoop loop;
|
QEventLoop loop;
|
||||||
QNetworkReply* reply = manager.post(request,&multiPart);
|
QNetworkReply* reply = manager.post(request,&multiPart);
|
||||||
_cacheReply.append(reply);
|
_cacheReply.append(reply);
|
||||||
connect(&manager,&QNetworkAccessManager::finished,&manager,[&loop](QNetworkReply *reply){
|
connect(&manager,&QNetworkAccessManager::finished,&manager,[&loop](QNetworkReply *reply){loop.quit();});
|
||||||
loop.quit();
|
connect(qApp,&QGuiApplication::aboutToQuit,&manager, [&loop](){loop.quit();});
|
||||||
});
|
|
||||||
connect(reply,&QNetworkReply::uploadProgress,reply,[=](qint64 bytesSent, qint64 bytesTotal){
|
connect(reply,&QNetworkReply::uploadProgress,reply,[=](qint64 bytesSent, qint64 bytesTotal){
|
||||||
Q_EMIT callable->uploadProgress(bytesSent,bytesTotal);
|
onUploadProgress(callable,bytesSent,bytesTotal);
|
||||||
});
|
});
|
||||||
loop.exec();
|
loop.exec();
|
||||||
QString result = QString::fromUtf8(reply->readAll());
|
QString result = QString::fromUtf8(reply->readAll());
|
||||||
@ -378,11 +387,11 @@ void FluHttp::upload(QString url,HttpCallable* callable,QMap<QString, QVariant>
|
|||||||
reply->deleteLater();
|
reply->deleteLater();
|
||||||
reply = nullptr;
|
reply = nullptr;
|
||||||
if (isSuccess) {
|
if (isSuccess) {
|
||||||
Q_EMIT callable->success(result);
|
onSuccess(callable,result);
|
||||||
}else{
|
}else{
|
||||||
Q_EMIT callable->error(status,errorString,result);
|
onError(callable,status,errorString,result);
|
||||||
}
|
}
|
||||||
Q_EMIT callable->finish();
|
onFinish(callable);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -396,12 +405,12 @@ QMap<QString, QVariant> FluHttp::toRequest(const QString& url,const QVariant& pa
|
|||||||
return request;
|
return request;
|
||||||
}
|
}
|
||||||
|
|
||||||
QVariant FluHttp::invokeIntercept(QMap<QString, QVariant> request,Qt::ConnectionType type){
|
QVariant FluHttp::invokeIntercept(QMap<QString, QVariant> request){
|
||||||
if(!FluApp::getInstance()->httpInterceptor()){
|
if(!FluApp::getInstance()->httpInterceptor()){
|
||||||
return request;
|
return request;
|
||||||
}
|
}
|
||||||
QVariant target;
|
QVariant target;
|
||||||
QMetaObject::invokeMethod(FluApp::getInstance()->httpInterceptor(), "onIntercept",type,Q_RETURN_ARG(QVariant,target),Q_ARG(QVariant, request));
|
QMetaObject::invokeMethod(FluApp::getInstance()->httpInterceptor(), "onIntercept",Q_RETURN_ARG(QVariant,target),Q_ARG(QVariant, request));
|
||||||
return target;
|
return target;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -490,6 +499,53 @@ qreal FluHttp::breakPointDownloadProgress(QString url,QString savePath,QMap<QStr
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
HttpRequest* FluHttp::newRequest(){
|
||||||
|
HttpRequest* request = new HttpRequest();
|
||||||
|
return request;
|
||||||
|
}
|
||||||
|
|
||||||
QString FluHttp::toHttpId(const QMap<QString, QVariant>& map){
|
QString FluHttp::toHttpId(const QMap<QString, QVariant>& map){
|
||||||
return FluTools::getInstance()->sha256(QJsonDocument::fromVariant(QVariant(map)).toJson());
|
return FluTools::getInstance()->sha256(QJsonDocument::fromVariant(QVariant(map)).toJson());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void FluHttp::onStart(QPointer<HttpCallable> callable){
|
||||||
|
if(callable){
|
||||||
|
Q_EMIT callable->start();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void FluHttp::onFinish(QPointer<HttpCallable> callable){
|
||||||
|
if(callable){
|
||||||
|
Q_EMIT callable->finish();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void FluHttp::onError(QPointer<HttpCallable> callable,int status,QString errorString,QString result){
|
||||||
|
if(callable){
|
||||||
|
Q_EMIT callable->error(status,errorString,result);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void FluHttp::onSuccess(QPointer<HttpCallable> callable,QString result){
|
||||||
|
if(callable){
|
||||||
|
Q_EMIT callable->success(result);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void FluHttp::onCache(QPointer<HttpCallable> callable,QString result){
|
||||||
|
if(callable){
|
||||||
|
Q_EMIT callable->cache(result);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void FluHttp::onDownloadProgress(QPointer<HttpCallable> callable,qint64 recv,qint64 total){
|
||||||
|
if(callable){
|
||||||
|
Q_EMIT callable->downloadProgress(recv,total);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void FluHttp::onUploadProgress(QPointer<HttpCallable> callable,qint64 sent,qint64 total){
|
||||||
|
if(callable){
|
||||||
|
Q_EMIT callable->uploadProgress(sent,total);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -6,7 +6,20 @@
|
|||||||
#include <QFile>
|
#include <QFile>
|
||||||
#include <QNetworkAccessManager>
|
#include <QNetworkAccessManager>
|
||||||
#include "stdafx.h"
|
#include "stdafx.h"
|
||||||
#include <QMutex>
|
|
||||||
|
class HttpRequest : public QObject{
|
||||||
|
Q_OBJECT
|
||||||
|
Q_PROPERTY_AUTO(QString,url);
|
||||||
|
Q_PROPERTY_AUTO(QVariant,params);
|
||||||
|
Q_PROPERTY_AUTO(QVariant,headers);
|
||||||
|
Q_PROPERTY_AUTO(QString,method);
|
||||||
|
QML_NAMED_ELEMENT(HttpRequest)
|
||||||
|
public:
|
||||||
|
explicit HttpRequest(QObject *parent = nullptr);
|
||||||
|
~HttpRequest(){
|
||||||
|
qDebug()<<"------------析构了"<<url();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
class HttpCallable : public QObject{
|
class HttpCallable : public QObject{
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
@ -19,7 +32,7 @@ public:
|
|||||||
Q_SIGNAL void success(QString result);
|
Q_SIGNAL void success(QString result);
|
||||||
Q_SIGNAL void cache(QString result);
|
Q_SIGNAL void cache(QString result);
|
||||||
Q_SIGNAL void downloadProgress(qint64 recv, qint64 total);
|
Q_SIGNAL void downloadProgress(qint64 recv, qint64 total);
|
||||||
Q_SIGNAL void uploadProgress(qint64 recv, qint64 total);
|
Q_SIGNAL void uploadProgress(qint64 sent, qint64 total);
|
||||||
};
|
};
|
||||||
|
|
||||||
class FluHttp : public QObject
|
class FluHttp : public QObject
|
||||||
@ -32,7 +45,7 @@ class FluHttp : public QObject
|
|||||||
Q_PROPERTY_AUTO(bool,breakPointDownload);
|
Q_PROPERTY_AUTO(bool,breakPointDownload);
|
||||||
QML_NAMED_ELEMENT(FluHttp)
|
QML_NAMED_ELEMENT(FluHttp)
|
||||||
private:
|
private:
|
||||||
QVariant invokeIntercept(QMap<QString, QVariant> request,Qt::ConnectionType type = Qt::BlockingQueuedConnection);
|
QVariant invokeIntercept(QMap<QString, QVariant> request);
|
||||||
QMap<QString, QVariant> toRequest(const QString& url,const QVariant& params,const QVariant& headers,const QString& method);
|
QMap<QString, QVariant> toRequest(const QString& url,const QVariant& params,const QVariant& headers,const QString& method);
|
||||||
QString toHttpId(const QMap<QString, QVariant>& map);
|
QString toHttpId(const QMap<QString, QVariant>& map);
|
||||||
void addQueryParam(QUrl* url,const QMap<QString, QVariant>& params);
|
void addQueryParam(QUrl* url,const QMap<QString, QVariant>& params);
|
||||||
@ -41,9 +54,18 @@ private:
|
|||||||
QString readCache(const QString& httpId);
|
QString readCache(const QString& httpId);
|
||||||
bool cacheExists(const QString& httpId);
|
bool cacheExists(const QString& httpId);
|
||||||
QString getCacheFilePath(const QString& httpId);
|
QString getCacheFilePath(const QString& httpId);
|
||||||
|
void onStart(QPointer<HttpCallable> callable);
|
||||||
|
void onFinish(QPointer<HttpCallable> callable);
|
||||||
|
void onError(QPointer<HttpCallable> callable,int status,QString errorString,QString result);
|
||||||
|
void onSuccess(QPointer<HttpCallable> callable,QString result);
|
||||||
|
void onCache(QPointer<HttpCallable> callable,QString result);
|
||||||
|
void onDownloadProgress(QPointer<HttpCallable> callable,qint64 recv,qint64 total);
|
||||||
|
void onUploadProgress(QPointer<HttpCallable> callable,qint64 sent,qint64 total);
|
||||||
public:
|
public:
|
||||||
explicit FluHttp(QObject *parent = nullptr);
|
explicit FluHttp(QObject *parent = nullptr);
|
||||||
~FluHttp();
|
~FluHttp();
|
||||||
|
Q_INVOKABLE HttpRequest* newRequest();
|
||||||
|
Q_INVOKABLE void get2(HttpRequest* request,HttpCallable* callable);
|
||||||
//神坑!!! 如果参数使用QVariantMap会有问题,在6.4.3版本中QML一调用就会编译失败。所以改用QMap<QString, QVariant>
|
//神坑!!! 如果参数使用QVariantMap会有问题,在6.4.3版本中QML一调用就会编译失败。所以改用QMap<QString, QVariant>
|
||||||
Q_INVOKABLE void get(QString url,HttpCallable* callable,QMap<QString, QVariant> params= {},QMap<QString, QVariant> headers = {});
|
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 post(QString url,HttpCallable* callable,QMap<QString, QVariant> params= {},QMap<QString, QVariant> headers = {});
|
||||||
|
@ -41,7 +41,7 @@ Item{
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
Component.onCompleted: {
|
Component.onCompleted: {
|
||||||
setGeometry(0,0,screenshot_background.width,screenshot_background.height)
|
setGeometry(0,0,screenshot_background.width,screenshot_background.height+1)
|
||||||
}
|
}
|
||||||
ScreenshotBackground{
|
ScreenshotBackground{
|
||||||
id:screenshot_background
|
id:screenshot_background
|
||||||
|
Loading…
Reference in New Issue
Block a user