|
|
@@ -35,7 +35,6 @@ export default {
|
|
|
data() {
|
|
|
return {
|
|
|
isOpening: false, // 防抖标志
|
|
|
- localFileCache: {}, // 本地文件缓存
|
|
|
}
|
|
|
},
|
|
|
methods: {
|
|
|
@@ -45,10 +44,10 @@ export default {
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
- if (item.filetype == 'ppt') {
|
|
|
+ if (item.filetype == 'ppt' || item.filetype == 'pdf') {
|
|
|
this.isOpening = true;
|
|
|
uni.showLoading({
|
|
|
- title: '正在打开...',
|
|
|
+ title: item.filetype == 'ppt' ? '正在打开PPT...' : '正在打开PDF...',
|
|
|
mask: true
|
|
|
});
|
|
|
|
|
|
@@ -57,22 +56,31 @@ export default {
|
|
|
if (file) {
|
|
|
// 检查本地缓存
|
|
|
const cacheKey = file.sat_coursewaredetailid || item.sat_coursewaredetailid;
|
|
|
- if (this.localFileCache[cacheKey]) {
|
|
|
- // 检查本地文件是否有效
|
|
|
- uni.getFileInfo({
|
|
|
- filePath: this.localFileCache[cacheKey],
|
|
|
- success: () => {
|
|
|
- // 文件有效,直接打开
|
|
|
- this.openDocument(this.localFileCache[cacheKey], file.postfix);
|
|
|
- },
|
|
|
- fail: () => {
|
|
|
- // 文件无效,重新下载
|
|
|
- this.downloadAndOpenFile(file, cacheKey);
|
|
|
- }
|
|
|
- });
|
|
|
- } else {
|
|
|
- // 无缓存,直接下载
|
|
|
- this.downloadAndOpenFile(file, cacheKey);
|
|
|
+ const cacheKeyStr = `${item.filetype}_file_cache_${cacheKey}`;
|
|
|
+
|
|
|
+ try {
|
|
|
+ const cachedFile = uni.getStorageSync(cacheKeyStr);
|
|
|
+ if (cachedFile) {
|
|
|
+ // 检查本地文件是否有效
|
|
|
+ uni.getFileInfo({
|
|
|
+ filePath: cachedFile,
|
|
|
+ success: () => {
|
|
|
+ // 文件有效,直接打开
|
|
|
+ this.openDocument(cachedFile, file.postfix);
|
|
|
+ },
|
|
|
+ fail: () => {
|
|
|
+ // 文件无效,重新下载
|
|
|
+ this.downloadAndOpenFile(file, cacheKeyStr);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ } else {
|
|
|
+ // 无缓存,直接下载
|
|
|
+ this.downloadAndOpenFile(file, cacheKeyStr);
|
|
|
+ }
|
|
|
+ } catch (error) {
|
|
|
+ console.log("获取缓存失败:", error);
|
|
|
+ // 缓存获取失败,重新下载
|
|
|
+ this.downloadAndOpenFile(file, cacheKeyStr);
|
|
|
}
|
|
|
} else {
|
|
|
this.isOpening = false;
|
|
|
@@ -90,12 +98,16 @@ export default {
|
|
|
},
|
|
|
|
|
|
// 下载文件并打开
|
|
|
- downloadAndOpenFile(file, cacheKey) {
|
|
|
+ downloadAndOpenFile(file, cacheKeyStr) {
|
|
|
uni.downloadFile({
|
|
|
url: file.url,
|
|
|
success: (res) => {
|
|
|
- // 缓存本地文件路径
|
|
|
- this.localFileCache[cacheKey] = res.tempFilePath;
|
|
|
+ // 缓存本地文件路径到本地存储
|
|
|
+ try {
|
|
|
+ uni.setStorageSync(cacheKeyStr, res.tempFilePath);
|
|
|
+ } catch (error) {
|
|
|
+ console.log("缓存文件路径失败:", error);
|
|
|
+ }
|
|
|
this.openDocument(res.tempFilePath, file.postfix);
|
|
|
},
|
|
|
fail: (err) => {
|