FluentUI/example/qml-Qt6/page/T_Http.qml

324 lines
9.6 KiB
QML
Raw Normal View History

2023-08-24 15:50:37 +08:00
import QtQuick
import Qt.labs.platform
import QtQuick.Layouts
import QtQuick.Window
import QtQuick.Controls
import QtQuick.Dialogs
import FluentUI
import "qrc:///example/qml/component"
FluContentPage{
title:"Http"
2023-09-04 18:37:55 +08:00
property string cacheDirPath: FluTools.getApplicationDirPath() + "/cache/http"
2023-08-24 15:50:37 +08:00
FluHttp{
id:http
}
2023-09-04 18:37:55 +08:00
FluHttp{
id:http_cache_ifnonecacherequest
cacheMode:FluHttpType.IfNoneCacheRequest
cacheDir:cacheDirPath
}
FluHttp{
id:http_cache_requestfailedreadcache
cacheMode:FluHttpType.RequestFailedReadCache
cacheDir:cacheDirPath
}
FluHttp{
id:http_cache_firstcachethenrequest
cacheMode:FluHttpType.FirstCacheThenRequest
cacheDir:cacheDirPath
}
HttpCallable{
id:callable
onStart: {
showLoading()
}
onFinish: {
hideLoading()
}
onError:
(status,errorString,result)=>{
console.debug(status+";"+errorString+";"+result)
}
onSuccess:
(result)=>{
text_info.text = result
console.debug(result)
}
onCache:
(result)=>{
text_info.text = result
console.debug(result)
}
}
2023-08-24 15:50:37 +08:00
Flickable{
id:layout_flick
2023-09-04 18:37:55 +08:00
width: 200
2023-08-24 15:50:37 +08:00
clip: true
anchors{
top: parent.top
topMargin: 20
bottom: parent.bottom
left: parent.left
}
ScrollBar.vertical: FluScrollBar {}
contentHeight:layout_column.height
Column{
spacing: 2
id:layout_column
width: parent.width
FluButton{
implicitWidth: parent.width
implicitHeight: 36
text: "Get请求"
onClicked: {
http.get("https://httpbingo.org/get",callable)
}
}
FluButton{
implicitWidth: parent.width
implicitHeight: 36
text: "Post表单请求"
onClicked: {
var param = {}
param.custname = "朱子楚"
param.custtel = "1234567890"
param.custemail = "zhuzichu520@gmail.com"
http.post("https://httpbingo.org/post",callable,param)
}
}
FluButton{
implicitWidth: parent.width
implicitHeight: 36
text: "Post Json请求"
onClicked: {
var param = {}
param.custname = "朱子楚"
param.custtel = "1234567890"
param.custemail = "zhuzichu520@gmail.com"
http.postJson("https://httpbingo.org/post",callable,param)
}
}
FluButton{
implicitWidth: parent.width
implicitHeight: 36
text: "Post String请求"
onClicked: {
var param = "我命由我不由天"
http.postString("https://httpbingo.org/post",callable,param)
}
}
FluButton{
id:btn_download
implicitWidth: parent.width
implicitHeight: 36
text: "下载文件"
onClicked: {
folder_dialog.open()
}
}
FluButton{
id:btn_upload
implicitWidth: parent.width
implicitHeight: 36
text: "文件上传"
onClicked: {
file_dialog.open()
}
}
2023-09-04 18:37:55 +08:00
FluButton{
implicitWidth: parent.width
implicitHeight: 36
text: "FirstCacheThenRequest缓存"
onClicked: {
var param = {}
param.cacheMode = "FirstCacheThenRequest"
http_cache_firstcachethenrequest.post("https://httpbingo.org/post",callable,param)
}
}
FluButton{
implicitWidth: parent.width
implicitHeight: 36
text: "RequestFailedReadCache缓存"
onClicked: {
var param = {}
param.cacheMode = "RequestFailedReadCache"
http_cache_requestfailedreadcache.post("https://httpbingo.org/post",callable,param)
}
}
2023-08-24 15:50:37 +08:00
2023-09-04 18:37:55 +08:00
FluButton{
implicitWidth: parent.width
implicitHeight: 36
text: "IfNoneCacheRequest缓存"
onClicked: {
var param = {}
param.cacheMode = "IfNoneCacheRequest"
http_cache_ifnonecacherequest.post("https://httpbingo.org/post",callable,param)
}
2023-08-24 15:50:37 +08:00
}
2023-09-04 18:37:55 +08:00
FluButton{
implicitWidth: parent.width
implicitHeight: 36
text: "删除缓存"
onClicked: {
console.debug(FluTools.removeDir(cacheDirPath))
}
2023-08-24 15:50:37 +08:00
}
2023-09-04 18:37:55 +08:00
FluButton{
implicitWidth: parent.width
implicitHeight: 36
text: "清空右边数据"
onClicked: {
text_info.text = ""
}
2023-08-24 15:50:37 +08:00
}
2023-09-04 18:37:55 +08:00
}
}
HttpCallable{
id:callable_upload
onStart: {
btn_upload.disabled = true
}
onFinish: {
btn_upload.disabled = false
btn_upload.text = "上传文件"
layout_upload_file_size.visible = false
text_upload_file_size.text = ""
}
onError:
(status,errorString,result)=>{
2023-08-24 15:50:37 +08:00
text_info.text = result
console.debug(result)
}
2023-09-04 18:37:55 +08:00
onSuccess:
(result)=>{
2023-08-24 15:50:37 +08:00
text_info.text = result
}
2023-09-04 18:37:55 +08:00
onUploadProgress:
(sent,total)=>{
2023-08-24 15:50:37 +08:00
var locale = Qt.locale()
var precent = (sent/total * 100).toFixed(0) + "%"
btn_upload.text = "上传中..."+precent
text_upload_file_size.text = "%1/%2".arg(locale.formattedDataSize(sent)).arg(locale.formattedDataSize(total))
layout_upload_file_size.visible = true
}
}
2023-09-04 18:37:55 +08:00
FileDialog {
id: file_dialog
2023-08-24 15:50:37 +08:00
onAccepted: {
2023-09-04 18:37:55 +08:00
var param = {}
for(var i=0;i<selectedFiles.length;i++){
var fileUrl = selectedFiles[i]
var fileName = FluTools.getFileNameByUrl(fileUrl)
var filePath = FluTools.toLocalPath(fileUrl)
param[fileName] = filePath
2023-08-24 15:50:37 +08:00
}
2023-09-04 18:37:55 +08:00
http.upload("https://httpbingo.org/post",callable_upload,param)
}
}
HttpCallable{
id:callable_download
onStart: {
btn_download.disabled = true
}
onFinish: {
btn_download.disabled = false
btn_download.text = "下载文件"
layout_download_file_size.visible = false
text_download_file_size.text = ""
}
onError:
(status,errorString,result)=>{
showError(errorString)
console.debug(status+";"+errorString+";"+result)
2023-08-24 15:50:37 +08:00
}
2023-09-04 18:37:55 +08:00
onSuccess:
(result)=>{
2023-08-24 15:50:37 +08:00
showSuccess(result)
}
2023-09-04 18:37:55 +08:00
onDownloadProgress:
(recv,total)=>{
2023-08-24 15:50:37 +08:00
var locale = Qt.locale()
var precent = (recv/total * 100).toFixed(0) + "%"
btn_download.text = "下载中..."+precent
text_download_file_size.text = "%1/%2".arg(locale.formattedDataSize(recv)).arg(locale.formattedDataSize(total))
layout_download_file_size.visible = true
}
2023-09-04 18:37:55 +08:00
}
FolderDialog {
id: folder_dialog
currentFolder: StandardPaths.standardLocations(StandardPaths.DownloadLocation)[0]
onAccepted: {
2023-08-24 15:50:37 +08:00
var path = FluTools.toLocalPath(currentFolder)+ "/big_buck_bunny.mp4"
2023-09-04 18:37:55 +08:00
http.download("http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4",callable_download,path)
2023-08-24 15:50:37 +08:00
}
}
FluArea{
anchors{
top: layout_flick.top
bottom: layout_flick.bottom
left: layout_flick.right
right: parent.right
leftMargin: 8
}
Flickable{
clip: true
id:scrollview
boundsBehavior:Flickable.StopAtBounds
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
padding: 14
}
}
}
FluRectangle{
id:layout_download_file_size
radius: [4,4,4,4]
height: 36
width: 160
visible: false
x:layout_flick.width
y: 173 - layout_flick.contentY
FluText{
id:text_download_file_size
anchors.centerIn: parent
}
}
FluRectangle{
id:layout_upload_file_size
radius: [4,4,4,4]
height: 36
width: 160
visible: false
x:layout_flick.width
y: 210 - layout_flick.contentY
FluText{
id:text_upload_file_size
anchors.centerIn: parent
}
}
}