This commit is contained in:
zhuzichu 2023-07-20 18:26:47 +08:00
parent 98e0aafb44
commit d014997d52
11 changed files with 2697 additions and 4 deletions

View File

@ -26,7 +26,7 @@ file(TO_CMAKE_PATH "/" PATH_SEPARATOR)
#
add_definitions(-DVERSION=1,3,7,4)
find_package(Qt6 REQUIRED COMPONENTS Quick Svg)
find_package(Qt6 REQUIRED COMPONENTS Quick Svg Network)
if(QT_VERSION VERSION_GREATER_EQUAL "6.3")
qt_standard_project_setup()
@ -106,7 +106,8 @@ set_target_properties(example PROPERTIES
if (FLUENTUI_BUILD_STATIC_LIB)
target_link_libraries(example PRIVATE
Qt6::Quick
Qt::Svg
Qt6::Svg
Qt6::Network
fluentui
fluentuiplugin
FramelessHelper::Core
@ -115,7 +116,8 @@ if (FLUENTUI_BUILD_STATIC_LIB)
else()
target_link_libraries(example PRIVATE
Qt6::Quick
Qt::Svg
Qt6::Svg
Qt6::Network
fluentuiplugin
FramelessHelper::Core
FramelessHelper::Quick

View File

@ -7,6 +7,23 @@ import FluentUI
Window {
id: app
flags: Qt.SplashScreen
FluHttpInterceptor{
id:interceptor
function onIntercept(request){
if(request.method === "get"){
request.params["method"] = "get"
}
if(request.method === "post"){
request.params["method"] = "post"
}
request.headers["token"] ="yyds"
request.headers["os"] ="pc"
console.debug(JSON.stringify(request))
return request
}
}
Component.onCompleted: {
FluApp.init(app)
FluTheme.darkMode = FluThemeType.System
@ -21,6 +38,7 @@ Window {
"/singleInstanceWindow":"qrc:/example/qml/window/SingleInstanceWindow.qml"
}
FluApp.initialRoute = "/"
FluApp.httpInterceptor = interceptor
FluApp.run()
}
}

View File

@ -320,6 +320,12 @@ FluObject{
FluPaneItemExpander{
title:lang.other
icon:FluentIcons.Shop
FluPaneItem{
title:"Http"
onTap:{
navigationView.push("qrc:/example/qml/page/T_Http.qml")
}
}
FluPaneItem{
id:item_other
title:"RemoteLoader"

147
example/qml/page/T_Http.qml Normal file
View File

@ -0,0 +1,147 @@
import QtQuick
import QtCore
import QtQuick.Layouts
import QtQuick.Window
import QtQuick.Controls
import QtQuick.Dialogs
import FluentUI
import "qrc:///example/qml/component"
FluScrollablePage{
title:"Http"
FluHttp{
id:http_get
url:"https://api.github.com/search/repositories"
onStart: {
showLoading()
}
onFinish: {
hideLoading()
}
onError:
(status,errorString)=>{
showError(errorString)
}
onSuccess:
(result)=>{
window_result.result = result
window_result.show()
}
}
FluHttp{
id:http_post
url:"https://www.wanandroid.com/article/query/0/json"
onStart: {
showLoading()
}
onFinish: {
hideLoading()
}
onError:
(status,errorString)=>{
showError(errorString)
}
onSuccess:
(result)=>{
window_result.result = result
window_result.show()
}
}
FluHttp{
id:http_download
url:"http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4"
onStart: {
btn_download.disabled = true
}
onFinish: {
btn_download.disabled = false
}
onDownloadFileProgress:
(recv,total)=>{
var precent = (recv/total * 100).toFixed(0) + "%"
btn_download.text = "下载中..."+precent
}
onError:
(status,errorString)=>{
showError(errorString)
}
onSuccess:
(result)=>{
showSuccess(result)
}
}
FluArea{
Layout.fillWidth: true
Layout.topMargin: 20
height: 160
paddings: 10
ColumnLayout{
spacing: 14
anchors.verticalCenter: parent.verticalCenter
FluButton{
text:"Get请求"
onClicked: {
http_get.get({q:"FluentUI"})
}
}
FluButton{
text:"Post请求"
onClicked: {
http_post.post({k:"jitpack"})
}
}
FluButton{
id:btn_download
text:disabled ? "下载中..." : "下载文件"
onClicked: {
file_dialog.open()
}
}
}
}
FolderDialog {
id: file_dialog
currentFolder: StandardPaths.standardLocations(StandardPaths.DownloadLocation)[0]
onAccepted: {
var path = selectedFolder.toString().replace("file:///","") + "/big_buck_bunny.mp4"
http_download.download(path)
}
}
Window{
property string result : ""
id:window_result
width: 600
height: 400
Item{
anchors.fill: parent
Flickable{
id:scrollview
width: parent.width
height: parent.height
contentWidth: width
contentHeight: text_info.height
ScrollBar.vertical: FluScrollBar {}
FluText{
id:text_info
width: scrollview.width
wrapMode: Text.WrapAnywhere
text:window_result.result
padding: 14
}
}
}
}
}

View File

@ -30,7 +30,6 @@ FluApp::~FluApp(){
void FluApp::init(QQuickWindow *window){
this->appWindow = window;
FluContentDialogType::ButtonFlag::NegativeButton;
}
void FluApp::run(){

View File

@ -9,6 +9,7 @@
#include <QJsonObject>
#include <QQmlEngine>
#include "FluRegister.h"
#include "FluHttpInterceptor.h"
#include "stdafx.h"
/**
@ -27,6 +28,11 @@ class FluApp : public QObject
*/
Q_PROPERTY_AUTO(QJsonObject,routes);
/**
* @brief http拦截器
*/
Q_PROPERTY_AUTO(FluHttpInterceptor*,httpInterceptor);
QML_NAMED_ELEMENT(FluApp)
QML_SINGLETON
private:

137
src/FluHttp.cpp Normal file
View File

@ -0,0 +1,137 @@
#include "FluHttp.h"
#include <QThreadPool>
#include "HttpClient.h"
#include "FluApp.h"
using namespace AeaQt;
FluHttp::FluHttp(QObject *parent)
: QObject{parent}
{
enabledBreakpointDownload(false);
}
Q_INVOKABLE void FluHttp::postString(QString params,QVariantMap headers){
QVariantMap request = invokeIntercept(params,headers,"postString").toMap();
QThreadPool::globalInstance()->start([=](){
HttpClient client;
Q_EMIT start();
client.post(_url)
.bodyWithRaw(request[params].toString().toUtf8())
.headers(request["headers"].toMap())
.onSuccess([=](QString result) {
Q_EMIT success(result);
Q_EMIT finish();
})
.onFailed([=](QNetworkReply* reply) {
Q_EMIT error(reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(),reply->errorString());
Q_EMIT finish();
})
.block()
.exec();
});
}
void FluHttp::post(QVariantMap params,QVariantMap headers){
QVariantMap request = invokeIntercept(params,headers,"post").toMap();
QThreadPool::globalInstance()->start([=](){
HttpClient client;
Q_EMIT start();
client.post(_url)
.headers(headers)
.bodyWithFormData(request["params"].toMap())
.headers(request["headers"].toMap())
.onSuccess([=](QString result) {
Q_EMIT success(result);
Q_EMIT finish();
})
.onFailed([=](QNetworkReply* reply) {
Q_EMIT error(reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(),reply->errorString());
Q_EMIT finish();
})
.block()
.exec();
});
}
void FluHttp::postJson(QVariantMap params,QVariantMap headers){
QVariantMap request = invokeIntercept(params,headers,"postJson").toMap();
QThreadPool::globalInstance()->start([=](){
HttpClient client;
Q_EMIT start();
client.post(_url)
.headers(headers)
.bodyWithRaw(QJsonDocument::fromVariant(request["params"]).toJson())
.headers(request["headers"].toMap())
.onSuccess([=](QString result) {
Q_EMIT success(result);
Q_EMIT finish();
})
.onFailed([=](QNetworkReply* reply) {
Q_EMIT error(reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(),reply->errorString());
Q_EMIT finish();
})
.block()
.exec();
});
}
void FluHttp::get(QVariantMap params,QVariantMap headers){
QVariantMap request = invokeIntercept(params,headers,"get").toMap();
QThreadPool::globalInstance()->start([=](){
HttpClient client;
Q_EMIT start();
client.get(_url)
.queryParams(request["params"].toMap())
.headers(request["headers"].toMap())
.onSuccess([=](QString result) {
Q_EMIT success(result);
Q_EMIT finish();
})
.onFailed([=](QNetworkReply* reply) {
Q_EMIT error(reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(),reply->errorString());
Q_EMIT finish();
})
.block()
.exec();
});
}
Q_INVOKABLE void FluHttp::download(QString path,QVariantMap params,QVariantMap headers){
QVariantMap request = invokeIntercept(params,headers,"download").toMap();
qDebug()<<request["headers"].toMap();
QThreadPool::globalInstance()->start([=](){
HttpClient client;
Q_EMIT start();
client.get(_url)
.download(path)
.enabledBreakpointDownload(_enabledBreakpointDownload)
.queryParams(request["params"].toMap())
.headers(request["headers"].toMap())
.onDownloadProgress([=](qint64 recv, qint64 total) {
Q_EMIT downloadFileProgress(recv,total);
})
.onDownloadFileSuccess([=](QString result) {
Q_EMIT success(result);
Q_EMIT finish();
})
.onDownloadFileFailed([=](QString errorString) {
Q_EMIT error(-1,errorString);
Q_EMIT finish();
})
.block()
.exec();
});
}
QVariant FluHttp::invokeIntercept(const QVariant& params,const QVariant& headers,const QString& method){
QVariantMap requet = {
{"params",params},
{"headers",headers},
{"method",method}
};
QVariant target;
QMetaObject::invokeMethod(FluApp::getInstance()->httpInterceptor(), "onIntercept",Q_RETURN_ARG(QVariant,target),Q_ARG(QVariant, requet));
return target;
}

32
src/FluHttp.h Normal file
View File

@ -0,0 +1,32 @@
#ifndef FLUHTTP_H
#define FLUHTTP_H
#include <QObject>
#include <QtQml/qqml.h>
#include <QNetworkAccessManager>
#include "stdafx.h"
class FluHttp : public QObject
{
Q_OBJECT
Q_PROPERTY_AUTO(QString,url);
Q_PROPERTY_AUTO(bool,enabledBreakpointDownload)
QML_NAMED_ELEMENT(FluHttp)
private:
QVariant invokeIntercept(const QVariant& params,const QVariant& headers,const QString& method);
public:
explicit FluHttp(QObject *parent = nullptr);
Q_SIGNAL void start();
Q_SIGNAL void finish();
Q_SIGNAL void error(int status,QString errorString);
Q_SIGNAL void success(QString result);
Q_SIGNAL void downloadFileProgress(qint64 recv, qint64 total);
Q_INVOKABLE void get(QVariantMap params = {},QVariantMap headers = {});
Q_INVOKABLE void post(QVariantMap params = {},QVariantMap headers = {});
Q_INVOKABLE void postJson(QVariantMap params = {},QVariantMap headers = {});
Q_INVOKABLE void postString(QString params = "",QVariantMap headers = {});
Q_INVOKABLE void download(QString path,QVariantMap params = {},QVariantMap headers = {});
};
#endif // FLUHTTP_H

View File

@ -0,0 +1,7 @@
#include "FluHttpInterceptor.h"
FluHttpInterceptor::FluHttpInterceptor(QObject *parent)
: QObject{parent}
{
}

18
src/FluHttpInterceptor.h Normal file
View File

@ -0,0 +1,18 @@
#ifndef FLUHTTPINTERCEPTOR_H
#define FLUHTTPINTERCEPTOR_H
#include <QObject>
#include <QtQml/qqml.h>
class FluHttpInterceptor : public QObject
{
Q_OBJECT
QML_NAMED_ELEMENT(FluHttpInterceptor)
public:
explicit FluHttpInterceptor(QObject *parent = nullptr);
signals:
};
#endif // FLUHTTPINTERCEPTOR_H

2321
src/HttpClient.h Normal file

File diff suppressed because it is too large Load Diff