FluentUI/example/qml/page/T_Http.qml

395 lines
13 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-09-08 17:39:10 +08:00
property bool isDownCompleted: false
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
}
onCache:
(result)=>{
text_info.text = 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: {
2023-09-08 00:21:28 +08:00
var request = http.newRequest("https://httpbingo.org/get")
http.get(request,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: {
2023-09-08 00:21:28 +08:00
var request = http.newRequest("https://httpbingo.org/post")
var params = {}
params.custname = "朱子楚"
params.custtel = "1234567890"
params.custemail = "zhuzichu520@gmail.com"
request.params = params
http.post(request,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 Json请求"
onClicked: {
2023-09-08 00:21:28 +08:00
var request = http.newRequest("https://httpbingo.org/post")
var params = {}
params.custname = "朱子楚"
params.custtel = "1234567890"
params.custemail = "zhuzichu520@gmail.com"
request.params = params
http.postJson(request,callable)
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: {
2023-09-08 00:21:28 +08:00
var request = http.newRequest("https://httpbingo.org/post")
request.params = "我命由我不由天"
http.postString(request,callable)
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
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 "继续下载"
}
}
2023-09-08 00:21:28 +08:00
HttpRequest{
id:request_breakpoint_download
url: "http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4"
downloadSavePath: FluTools.getApplicationDirPath()+ "/download/big_buck_bunny.mp4"
}
2023-09-06 18:07:51 +08:00
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)=>{
2023-09-08 17:39:10 +08:00
if(!isDownCompleted){
tour.open()
isDownCompleted = true
}
2023-09-06 18:07:51 +08:00
showSuccess(result)
}
onDownloadProgress:
(recv,total)=>{
btn_breakpoint_download.progress = recv/total
}
}
2023-09-06 14:05:29 +08:00
Component.onCompleted: {
2023-09-08 00:21:28 +08:00
progress = http_breakpoint_download.getBreakPointProgress(request_breakpoint_download)
2023-09-06 14:05:29 +08:00
}
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){
2023-09-08 00:21:28 +08:00
FluTools.showFileInFolder(request_breakpoint_download.downloadSavePath)
2023-09-06 14:05:29 +08:00
}else{
2023-09-08 00:21:28 +08:00
http_breakpoint_download.download(request_breakpoint_download,callable_breakpoint_download)
2023-09-06 14:05:29 +08:00
}
}
FluMenu{
id:menu_breakpoint_download
width: 120
FluMenuItem{
text: "删除文件"
onClicked: {
2023-09-08 00:21:28 +08:00
if(FluTools.removeFile(request_breakpoint_download.downloadSavePath)){
2023-09-06 14:05:29 +08:00
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: {
2023-09-08 00:21:28 +08:00
var request = http.newRequest("https://httpbingo.org/post")
request.params = {cacheMode:"FirstCacheThenRequest"}
http_cache_firstcachethenrequest.post(request,callable)
2023-09-04 18:37:55 +08:00
}
}
FluButton{
implicitWidth: parent.width
implicitHeight: 36
text: "RequestFailedReadCache缓存"
onClicked: {
2023-09-08 00:21:28 +08:00
var request = http.newRequest("https://httpbingo.org/post")
request.params = {cacheMode:"RequestFailedReadCache"}
http_cache_requestfailedreadcache.post(request,callable)
2023-09-04 18:37:55 +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: "IfNoneCacheRequest缓存"
onClicked: {
2023-09-08 00:21:28 +08:00
var request = http.newRequest("https://httpbingo.org/post")
request.params = {cacheMode:"IfNoneCacheRequest"}
http_cache_ifnonecacherequest.post(request,callable)
2023-09-04 18:37:55 +08:00
}
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
}
}
2023-09-08 17:39:10 +08:00
FluTour{
id:tour
steps:[
{title:"友情提示",description: "下载已完成,左击这里可以打开文件所在路径,右击可以弹出菜单删除文件!",target:()=>btn_breakpoint_download}
]
}
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-08 00:21:28 +08:00
var request = http.newRequest("https://httpbingo.org/post")
var params = {}
2023-09-04 18:37:55 +08:00
for(var i=0;i<selectedFiles.length;i++){
var fileUrl = selectedFiles[i]
var fileName = FluTools.getFileNameByUrl(fileUrl)
var filePath = FluTools.toLocalPath(fileUrl)
2023-09-08 00:21:28 +08:00
params[fileName] = filePath
2023-07-26 22:06:55 +08:00
}
2023-09-08 00:21:28 +08:00
request.params = params
http.upload(request,callable_upload)
2023-09-04 18:37:55 +08:00
}
}
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-09-08 00:21:28 +08:00
var request = http.newRequest("http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4")
request.downloadSavePath = FluTools.toLocalPath(currentFolder)+ "/big_buck_bunny.mp4"
http.download(request,callable_download)
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
}
}
}
}