Files
doc-mini-app/pages/details/details.js

205 lines
4.4 KiB
JavaScript

// pages/details/details.js
const request = require('../../utils/request')
const ui = require('../../utils/ui')
const wxParse = require("../../wxParse/wxParse.js");
Page({
/**
* 页面的初始数据
*/
data: {
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() {
}
})