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

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

@@ -7,178 +7,199 @@ const wxParse = require("../../wxParse/wxParse.js");
Page({
/**
* 页面的初始数据
*/
data: {
/**
* 页面的初始数据
*/
data: {
navTitle: "详情",
date: "",
title: "",
content: "",
aid: 0,
isDow: false,
ext_download_link: ""
},
/**
* 生命周期函数--监听页面加载
*/
onLoad(options) {
let that = this;
if (!options.aid) {
wx.navigateBack()
this.triggerEvent('onBack')
}
that.setData({
aid: options.aid
})
that.GetDetailsInfo()
console.log(options);
//
},
initContent() {
let that = this;
console.log(that.data.content);
if (that.data.content.length > 0) {
wxParse.wxParse('content', 'html', that.data.content, that, 0);
}
},
GetDetailsInfo() {
let that = this;
request.request({
method: "GET",
// https://app.19year.cn/api.php/doc/details?id=51
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,
})
if (res.data.data.ext_download_link.length > 0) {
that.setData({
isDow: true
})
}
that.initContent()
}
}).catch(err => {
console.log('ERROR')
});
},
downloadFileOpen() {
let that = this;
let link = that.data.ext_download_link
wx.setClipboardData({
data: link,
success: function (res) {
wx.getClipboardData({
success: function (res) {
wx.showToast({
title: '复制成功'
})
}
})
}
})
//复制到剪切板
// 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('打开文档成功')
// }
// })
// }
// }
// })
},
/**
* 生命周期函数--监听页面初次渲染完成
*/
onReady() {
},
/**
* 生命周期函数--监听页面显示
*/
onShow() {
},
/**
* 生命周期函数--监听页面隐藏
*/
onHide() {
},
/**
* 生命周期函数--监听页面卸载
*/
onUnload() {
},
/**
* 页面相关事件处理函数--监听用户下拉动作
*/
onPullDownRefresh() {
},
/**
* 页面上拉触底事件的处理函数
*/
onReachBottom() {
},
/**
* 用户点击右上角分享
*/
onShareAppMessage() {
navTitle: "详情",
date: "",
title: "",
content: "",
aid: 0,
isDow: false,
ext_download_link: "",
ext_dow_ext: "",
dowBtnTxt: "下载附件",
},
/**
* 生命周期函数--监听页面加载
*/
onLoad(options) {
let that = this;
if (!options.aid) {
wx.navigateBack()
this.triggerEvent('onBack')
}
that.setData({
aid: options.aid
})
that.GetDetailsInfo()
console.log(options);
//
},
initContent() {
let that = this;
console.log(that.data.content);
if (that.data.content.length > 0) {
wxParse.wxParse('content', 'html', that.data.content, that, 0);
}
},
GetDetailsInfo() {
let that = this;
request.request({
method: "GET",
// https://app.19year.cn/api.php/doc/details?id=51
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', err)
});
},
downloadFileOpen() {
let that = this;
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: ext_download_link,
success: function (res) {
wx.getClipboardData({
success: function (res) {
wx.showToast({
title: '复制成功'
})
}
})
}
})
}
},
/**
* 生命周期函数--监听页面初次渲染完成
*/
onReady() {
},
/**
* 生命周期函数--监听页面显示
*/
onShow() {
},
/**
* 生命周期函数--监听页面隐藏
*/
onHide() {
},
/**
* 生命周期函数--监听页面卸载
*/
onUnload() {
},
/**
* 页面相关事件处理函数--监听用户下拉动作
*/
onPullDownRefresh() {
},
/**
* 页面上拉触底事件的处理函数
*/
onReachBottom() {
},
/**
* 用户点击右上角分享
*/
onShareAppMessage() {
}
})

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>