FluentUI/example/qml/page/T_Http.qml

374 lines
12 KiB
QML
Raw Normal View History

2023-08-24 15:50:37 +08:00
import QtQuick 2.15
import Qt.labs.platform 1.1
import QtQuick.Layouts 1.15
import QtQuick.Window 2.15
import QtQuick.Controls 2.15
import QtQuick.Dialogs 1.3
import FluentUI 1.0
2023-07-20 18:26:47 +08:00
import "qrc:///example/qml/component"
2023-08-26 17:20:30 +08:00
import "../component"
2023-07-20 18:26:47 +08:00
2023-07-26 17:48:35 +08:00
FluContentPage{
2023-07-20 18:26:47 +08:00
title:"Http"
2023-09-04 18:37:55 +08:00
property string cacheDirPath: FluTools.getApplicationDirPath() + "/cache/http"
2023-07-20 18:26:47 +08:00
FluHttp{
2023-07-26 17:48:35 +08:00
id:http
2023-09-05 18:38:16 +08:00
cacheDir:cacheDirPath
2023-07-20 18:26:47 +08:00
}
2023-09-06 14:05:29 +08:00
FluHttp{
id:http_breakpoint_download
cacheDir:cacheDirPath
breakPointDownload: true
}
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-07-26 22:06:55 +08:00
Flickable{
id:layout_flick
2023-09-04 18:37:55 +08:00
width: 200
2023-07-26 22:06:55 +08:00
clip: true
anchors{
top: parent.top
topMargin: 20
bottom: parent.bottom
left: parent.left
2023-07-20 18:26:47 +08:00
}
2023-07-26 22:06:55 +08:00
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)
2023-07-26 17:48:35 +08:00
}
2023-07-20 18:26:47 +08:00
}
2023-07-26 22:06:55 +08:00
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)
2023-07-26 17:48:35 +08:00
}
2023-07-20 18:26:47 +08:00
}
2023-07-26 22:06:55 +08:00
FluButton{
implicitWidth: parent.width
implicitHeight: 36
text: "Post Json请求"
onClicked: {
var param = {}
param.custname = "朱子楚"
param.custtel = "1234567890"
param.custemail = "zhuzichu520@gmail.com"
2023-08-10 16:08:27 +08:00
http.postJson("https://httpbingo.org/post",callable,param)
2023-07-26 17:48:35 +08:00
}
2023-07-26 22:06:55 +08:00
}
FluButton{
implicitWidth: parent.width
implicitHeight: 36
text: "Post String请求"
onClicked: {
var param = "我命由我不由天"
2023-08-10 16:08:27 +08:00
http.postString("https://httpbingo.org/post",callable,param)
2023-07-26 17:48:35 +08:00
}
2023-07-26 22:06:55 +08:00
}
2023-09-05 16:48:04 +08:00
FluProgressButton{
2023-07-26 22:06:55 +08:00
id:btn_download
implicitWidth: parent.width
implicitHeight: 36
text: "下载文件"
2023-08-16 13:14:00 +08:00
onClicked: {
folder_dialog.open()
}
}
2023-09-06 14:05:29 +08:00
FluProgressButton{
2023-09-06 18:07:51 +08:00
property bool downloading: false
2023-09-06 14:05:29 +08:00
property string saveFilePath: FluTools.getApplicationDirPath()+ "/download/big_buck_bunny.mp4"
property string resourcePath: "http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4"
id:btn_breakpoint_download
implicitWidth: parent.width
implicitHeight: 36
2023-09-06 18:07:51 +08:00
text: {
if(downloading){
return "暂停下载"
}
if(progress === 0){
return "断点下载文件"
}else if(progress === 1){
return "打开文件"
}else{
return "继续下载"
}
}
HttpCallable{
id:callable_breakpoint_download
onStart: {
btn_breakpoint_download.downloading = true
}
onFinish: {
btn_breakpoint_download.downloading = false
}
onError:
(status,errorString,result)=>{
console.debug(status+";"+errorString+";"+result)
}
onSuccess:
(result)=>{
showSuccess(result)
}
onDownloadProgress:
(recv,total)=>{
btn_breakpoint_download.progress = recv/total
}
}
2023-09-06 14:05:29 +08:00
Component.onCompleted: {
progress = http_breakpoint_download.breakPointDownloadProgress(resourcePath,saveFilePath)
}
onClicked: {
2023-09-06 18:07:51 +08:00
if(downloading){
http_breakpoint_download.cancel()
return
}
2023-09-06 14:05:29 +08:00
if(progress === 1){
FluTools.showFileInFolder(saveFilePath)
}else{
http_breakpoint_download.download(resourcePath,callable_breakpoint_download,saveFilePath)
}
}
FluMenu{
id:menu_breakpoint_download
width: 120
FluMenuItem{
text: "删除文件"
onClicked: {
if(FluTools.removeFile(btn_breakpoint_download.saveFilePath)){
btn_breakpoint_download.progress = 0
}
}
}
}
MouseArea {
anchors.fill: parent
acceptedButtons: Qt.RightButton
onClicked: {
if(btn_breakpoint_download.progress === 1){
menu_breakpoint_download.popup()
}
}
}
}
2023-09-05 16:48:04 +08:00
FluProgressButton{
2023-08-16 13:14:00 +08:00
id:btn_upload
implicitWidth: parent.width
implicitHeight: 36
text: "文件上传"
2023-07-26 22:06:55 +08:00
onClicked: {
file_dialog.open()
2023-07-26 17:48:35 +08:00
}
2023-07-20 18:26:47 +08:00
}
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-07-20 18:26:47 +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-16 13:14:00 +08:00
}
2023-09-04 22:49:50 +08:00
FluButton{
implicitWidth: parent.width
implicitHeight: 36
text: "打开缓存路径"
onClicked: {
Qt.openUrlExternally("file:///"+cacheDirPath)
}
}
2023-09-04 18:37:55 +08:00
FluButton{
implicitWidth: parent.width
implicitHeight: 36
text: "删除缓存"
onClicked: {
console.debug(FluTools.removeDir(cacheDirPath))
}
2023-08-16 13:14:00 +08:00
}
2023-09-04 18:37:55 +08:00
FluButton{
implicitWidth: parent.width
implicitHeight: 36
text: "清空右边数据"
onClicked: {
text_info.text = ""
}
2023-08-16 13:14:00 +08:00
}
2023-09-04 18:37:55 +08:00
}
}
HttpCallable{
id:callable_upload
onStart: {
btn_upload.disabled = true
}
onFinish: {
btn_upload.disabled = false
}
onError:
(status,errorString,result)=>{
2023-09-05 16:48:04 +08:00
btn_upload.progress = 0
2023-08-16 13:14:00 +08:00
text_info.text = result
console.debug(result)
}
2023-09-04 18:37:55 +08:00
onSuccess:
(result)=>{
2023-08-16 13:14:00 +08:00
text_info.text = result
}
2023-09-04 18:37:55 +08:00
onUploadProgress:
(sent,total)=>{
2023-09-05 16:48:04 +08:00
btn_upload.progress = sent/total
2023-08-16 13:14:00 +08:00
}
}
2023-09-04 18:37:55 +08:00
FileDialog {
id: file_dialog
2023-07-26 22:06:55 +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-07-26 22:06:55 +08:00
}
2023-09-04 18:37:55 +08:00
http.upload("https://httpbingo.org/post",callable_upload,param)
}
}
HttpCallable{
id:callable_download
onStart: {
2023-09-06 00:22:37 +08:00
btn_download.progress = 0
2023-09-04 18:37:55 +08:00
btn_download.disabled = true
}
onFinish: {
btn_download.disabled = false
}
onError:
(status,errorString,result)=>{
2023-09-05 16:48:04 +08:00
btn_download.progress = 0
2023-09-04 18:37:55 +08:00
showError(errorString)
console.debug(status+";"+errorString+";"+result)
2023-07-26 22:06:55 +08:00
}
2023-09-04 18:37:55 +08:00
onSuccess:
(result)=>{
2023-07-26 22:06:55 +08:00
showSuccess(result)
}
2023-09-04 18:37:55 +08:00
onDownloadProgress:
(recv,total)=>{
2023-09-05 16:48:04 +08:00
btn_download.progress = recv/total
2023-07-26 22:06:55 +08:00
}
2023-09-04 18:37:55 +08:00
}
FolderDialog {
id: folder_dialog
currentFolder: StandardPaths.standardLocations(StandardPaths.DownloadLocation)[0]
onAccepted: {
2023-07-26 22:06:55 +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-07-20 18:26:47 +08:00
}
}
2023-07-26 17:48:35 +08:00
FluArea{
anchors{
2023-07-26 22:06:55 +08:00
top: layout_flick.top
bottom: layout_flick.bottom
left: layout_flick.right
2023-07-26 17:48:35 +08:00
right: parent.right
2023-07-26 22:06:55 +08:00
leftMargin: 8
2023-07-26 17:48:35 +08:00
}
Flickable{
clip: true
id:scrollview
2023-08-16 13:14:00 +08:00
boundsBehavior:Flickable.StopAtBounds
2023-07-26 17:48:35 +08:00
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
2023-07-20 18:26:47 +08:00
}
}
}
}