修改工作区下载逻辑;云端文件夹基本功能已完成。

This commit is contained in:
2026-07-01 11:35:20 +08:00
parent 488bebcc87
commit 3544208baa
8 changed files with 3722 additions and 863 deletions

View File

@@ -317,28 +317,13 @@
uni.downloadFile({
url: downloadUrl,
name: fileName, // 重要:指定文件名,保证打开正常
name: fileName,
success: (downloadRes) => {
uni.hideLoading();
if (downloadRes.statusCode === 200) {
// 下载成功 → 直接打开文件
uni.openDocument({
filePath: downloadRes.tempFilePath,
showMenu: true, // 右上角显示 分享/保存 按钮
success: () => {
uni.showToast({
title: '打开成功',
icon: 'success'
});
},
fail: (err) => {
console.error('打开失败:', err);
uni.showToast({
title: '无法打开此文件',
icon: 'error'
});
}
});
const tempPath = downloadRes.tempFilePath;
// 下载成功,保存到 yxd 目录
saveFileToYxd(fileName, tempPath);
} else {
uni.showToast({ title: '下载失败', icon: 'error' });
}
@@ -356,6 +341,133 @@
}
};
// 将下载的文件保存到外部存储 yxd 目录
const saveFileToYxd = (fileName, tempPath) => {
// #ifdef APP-PLUS
// App 端:复制文件到 /storage/emulated/0/yxd/
const YXD_PATH = '/storage/emulated/0/yxd/';
const ensureYxdDir = (callback) => {
plus.io.resolveLocalFileSystemURL(YXD_PATH, (dirEntry) => {
callback(dirEntry);
}, () => {
plus.io.resolveLocalFileSystemURL('/storage/emulated/0/', (rootEntry) => {
rootEntry.getDirectory('yxd', { create: true }, (dirEntry) => {
callback(dirEntry);
}, (err) => {
console.error('创建 yxd 目录失败:', JSON.stringify(err));
uni.showToast({ title: '创建目录失败', icon: 'error' });
openFileConfirm(fileName, tempPath);
});
}, (err) => {
console.error('访问外部存储根目录失败:', JSON.stringify(err));
uni.showToast({ title: '无法访问存储目录', icon: 'error' });
openFileConfirm(fileName, tempPath);
});
});
};
ensureYxdDir((yxdDirEntry) => {
plus.io.resolveLocalFileSystemURL(tempPath, (fileEntry) => {
fileEntry.copyTo(
yxdDirEntry,
fileName,
() => {
const targetPath = YXD_PATH + fileName;
console.log('文件已保存到:', targetPath);
openFileConfirm(fileName, targetPath);
},
(err) => {
console.error('复制文件失败:', JSON.stringify(err));
openFileConfirm(fileName, tempPath);
}
);
}, (err) => {
console.error('读取临时文件失败:', JSON.stringify(err));
openFileConfirm(fileName, tempPath);
});
});
// #endif
// #ifndef APP-PLUS
uni.saveFile({
tempFilePath: tempPath,
success: (saveRes) => {
openFileConfirm(fileName, saveRes.savedFilePath);
},
fail: () => {
openFileConfirm(fileName, tempPath);
}
});
// #endif
};
// 下载成功后确认是否打开文件
const openFileConfirm = (fileName, filePath) => {
uni.showActionSheet({
title: `${fileName} 下载完成`,
itemList: ['打开文件', '选择其他应用打开', '仅保存'],
success: (res) => {
switch (res.tapIndex) {
case 0:
openFile(filePath);
break;
case 1:
openFileWithMenu(filePath);
break;
case 2:
uni.showToast({
title: '文件已保存到 yxd 目录',
icon: 'success'
});
break;
}
},
fail: () => {
uni.showToast({
title: '文件已保存到 yxd 目录',
icon: 'success'
});
}
});
};
// 使用系统默认应用打开
const openFile = (filePath) => {
uni.openDocument({
filePath: filePath,
success: () => uni.showToast({
title: '打开成功',
icon: 'success'
}),
fail: (err) => {
console.error('打开文件失败:', err);
uni.showToast({
title: '无法打开此文件',
icon: 'error'
});
}
});
};
// 选择其他应用打开
const openFileWithMenu = (filePath) => {
uni.openDocument({
filePath: filePath,
showMenu: true,
success: () => uni.showToast({
title: '打开成功',
icon: 'success'
}),
fail: (err) => {
console.error('打开文件失败:', err);
uni.showToast({
title: '无法打开此文件',
icon: 'error'
});
}
});
};
// 删除
const handleDelete = (folder) => {
uni.showModal({