<template>
<view class=
"content"
>
<image class=
"logo"
src=
"/static/logo.png"
></image>
<view class=
"text-area"
>
<text class=
"title"
>{{
title
}}</text>
</view>
<view class=
"input-area"
>
<input
type
=
"text"
v-model=
"serverAddress"
placeholder=
"请输入服务器地址"
/>
</view>
<view class=
"select-area"
>
<picker mode=
"selector"
:range=
"departments"
@change=
"onDepartmentChange"
>
<view class=
"picker"
>
当前选择:{{ departments[selectedDepartment] }}
</view>
</picker>
</view>
<button class=
"download-button"
@click=
"downloadFiles"
>资料更新</button>
</view>
</template>
<script>
export
default {
data() {
return {
title
:
'资料更新'
,
serverAddress:
''
,
departments: [
'部门1'
,
'部门2'
,
'部门3'
],
selectedDepartment: 0
};
},
methods: {
onDepartmentChange(event) {
this
.selectedDepartment = event.detail.value;
},
downloadFiles() {
if
(!
this
.serverAddress) {
uni.showToast({
title
:
'请输入服务器地址'
,
icon:
'none'
});
return;
}
const department =
this
.departments[
this
.selectedDepartment];
const encodedDepartment = encodeURIComponent(department +
'/files.zip'
);
const url = `${
this
.serverAddress}/download.php
?
file=${encodedDepartment}`;
console.log(
'Download URL:'
, url); // 打印下载 URL,用于调试
uni.downloadFile({
url: url,
success: (res) => {
if
(res.statusCode === 200) {
const tempFilePath = res.tempFilePath;
// 保存文件到设备的 Download 文件夹
const savePath = `${wx.env.USER_DATA_PATH}/Download/${department}_files.zip`;
wx.getFileSystemManager().saveFile({
tempFilePath: tempFilePath,
filePath: savePath,
success: (result) => {
uni.showToast({
title
:
'下载成功'
,
icon:
'success'
});
},
fail: (err) => {
console.log(
'保存文件失败:'
, err);
uni.showToast({
title
:
'保存文件失败'
,
icon:
'none'
});
}
});
}
else
{
console.log(
'Download failed with status code:'
, res.statusCode);
uni.showToast({
title
: `下载失败,状态码:${res.statusCode}`,
icon:
'none'
});
}
},
fail: (err) => {
console.log(
'Download failed:'
, err);
uni.showToast({
title
:
'下载失败'
,
icon:
'none'
});
}
});
}
}
};
</script>
<style
scoped
>
.content {
display: flex;
flex-direction: column;
align
-items: center;
justify-content: center;
padding: 20rpx;
background-color: #f5f5f5;
min-height: 100vh;
}
.logo {
height: 200rpx;
width
: 200rpx;
margin-top: 50rpx;
}
.text-area {
display: flex;
justify-content: center;
margin-top: 20rpx;
}
.
title
{
font-
size
: 48rpx;
color: #333;
font-weight: bold;
}
.input-area {
width
: 80%;
margin: 20rpx 0;
}
input {
width
: 100%;
padding: 20rpx;
font-
size
: 32rpx;
border: 1px solid #ccc;
border-radius: 10rpx;
}
.select-area {
width
: 80%;
margin: 20rpx 0;
}
.picker {
width
: 100%;
padding: 20rpx;
font-
size
: 32rpx;
background-color: #fff;
border: 1px solid #ccc;
border-radius: 10rpx;
text-
align
: center;
}
.download-button {
width
: 80%;
padding: 20rpx;
font-
size
: 36rpx;
color: #fff;
background-color: #007aff;
border:
none
;
border-radius: 10rpx;
text-
align
: center;
margin-top: 20rpx;
}
</style>