'处理附件预览与下载的兼容性'

This commit is contained in:
2024-08-27 10:31:25 +08:00
parent 2cdbd5c22d
commit 7041d5b092
2 changed files with 193 additions and 172 deletions

View File

@@ -18,7 +18,9 @@ Page({
content: "",
aid: 0,
isDow: false,
ext_download_link: ""
ext_download_link: "",
ext_dow_ext: "",
dowBtnTxt: "下载附件",
},
/**
@@ -55,31 +57,89 @@ Page({
url: "/api.php/doc/details?id=" + that.data.aid
}).then(res => {
if (res.data.code === 1) {
that.setData({
title: res.data.data.title,
date: res.data.data.date,
content: res.data.data.content,
ext_download_link: res.data.data.ext_download_link,
ext_dow_ext: res.data.data.ext_dow_ext,
})
if (res.data.data.ext_download_link.length > 0) {
that.setData({
isDow: true
})
let dowBtnTxt = ""
let ext_dow_ext = res.data.data.ext_dow_ext
if (ext_dow_ext === "pdf" || ext_dow_ext === "doc" || ext_dow_ext === "docx") {
dowBtnTxt = "下载附件"
} else {
dowBtnTxt = "复制下载地址去浏览器下载附件"
}
that.setData({
dowBtnTxt: dowBtnTxt
})
}
that.initContent()
}
}).catch(err => {
console.log('ERROR')
console.log('ERROR', err)
});
},
downloadFileOpen() {
let that = this;
let link = that.data.ext_download_link
let ext_download_link = that.data.ext_download_link
let ext_dow_ext = that.data.ext_dow_ext
if (ext_dow_ext === "pdf" || ext_dow_ext === "doc" || ext_dow_ext === "docx") {
ui.showLoading('加载中...');
wx.downloadFile({
url: ext_download_link,
success: function (res) {
console.log(res, "wx.downloadFile success res")
if (res.statusCode != 200) {
ui.hideLoading()
ui.showToast("预览文件失败")
return false
}
var Path = res.tempFilePath //返回的文件临时地址,用于后面打开本地预览所用
wx.openDocument({
filePath: Path,
showMenu: true,
success: function (res) {
console.log('打开成功');
util.hideLoading()
}
})
},
fail: function (err) {
console.log(err, "wx.downloadFile fail err");
ui.hideLoading()
ui.showToast("预览文件失败")
}
})
// wx.downloadFile({
// url: ext_download_link,
// success(res) {
// if (res.statusCode === 200) {
// const filePath = res.tempFilePath
// wx.openDocument({
// filePath: filePath,
// showMenu: true, //关键点
// success: function (res) {
// console.log('打开文档成功')
// }
// })
// }
// }
// })
} else {
wx.setClipboardData({
data: link,
data: ext_download_link,
success: function (res) {
wx.getClipboardData({
success: function (res) {
@@ -90,46 +150,7 @@ Page({
})
}
})
//复制到剪切板
// wx.setClipboardData({
// data: link,
// success(res) {
// console.log('copy success');
// //获取剪切板的纸
// // wx.getClipboardData({
// // success(res) {
// // console.log(res) // data
// // that.copyContent = res.data
// // }
// // })
// }
// })
// wx.showLoading({
// title: '下载中',
// })
//下载文件
// wx.downloadFile({
// url: link,
// success (res) {
// if (res.statusCode === 200) {
// const filePath = res.tempFilePath
// wx.openDocument({
// filePath: filePath,
// showMenu:true, //关键点
// success: function (res) {
// console.log('打开文档成功')
// }
// })
// }
// }
// })
}
},

View File

@@ -20,5 +20,5 @@
</view>
</scroll-view>
<view class='btn-view' wx:if="{{isDow}}">
<button bind:tap="downloadFileOpen" style="background: #3c8bf6;;border-radius: 60rpx;width: 85%;color: #fff;font-size: 30rpx;font-weight: normal;">附件下载</button>
<button bind:tap="downloadFileOpen" style="background: #3c8bf6;;border-radius: 60rpx;width: 85%;color: #fff;font-size: 30rpx;font-weight: normal;">{{dowBtnTxt}}</button>
</view>