// pages/resourceDetails/document/index.js const app = getApp() Page({ /** * 页面的初始数据 */ data: { webpageSrc: '', navBarHeight: '', barHeight: '', activeId: '', bookId: '', bookName: '', cmsId: '', parentName: '', parentProductLinkPath: '', productLinkPath: '', showData: '', titleName: '', selectType: '', zipData: '', naturalResources: [], titleName: '', pdfDatA: [], startTime: "", //进入页面当前时间 pauseTime: 0, //暂停时间 applyState: '', deadline: '', }, /** * 生命周期函数--监听页面加载 */ onLoad(options) { wx.setNavigationBarTitle({ title: '资源详情' }); console.log(options); const systInfo = wx.getSystemInfoSync(); const menu = wx.getMenuButtonBoundingClientRect(); // 胶囊信息 const navBarHeight = (menu.top - systInfo.statusBarHeight) * 2 + menu.height; // 导航栏高度 this.setData({ navBarHeight: navBarHeight, barHeight: systInfo.statusBarHeight, activeId: options.activeId, bookId: options.bookId, bookName: options.bookName, cmsId: options.cmsId, parentName: options.parentName, parentProductLinkPath: options.parentProductLinkPath, productLinkPath: options.productLinkPath, applyState: options.applyState, deadline: options.deadline }) this.resourceDetailsData() }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady() { }, /** * 生命周期函数--监听页面显示 */ onShow() { this.setData({ startTime: Date.now() }) }, /** * 生命周期函数--监听页面隐藏 */ onHide() { this.setData({ pauseTime: Date.now() }) if (wx.getStorageSync(app.config.tokenKey) && epubObj.bookBuy) { let duration = this.data.pauseTime - this.data.startTime this.count(duration) } }, /** * 生命周期函数--监听页面卸载 */ onUnload() { this.setData({ pauseTime: Date.now() }) if (wx.getStorageSync(app.config.tokenKey) && epubObj.bookBuy) { let duration = this.data.pauseTime - this.data.startTime this.count(duration) } }, count(timeStr) { const data = { appRefCode: app.config.appRefCode, type: 'LearningTime', //统计类型--阅读时长 data: timeStr + '', //统计内容--时长毫秒 event: 'LearningTime', sysType: 'App' } //阅读商品的id if (this.data.bookId) { data.productId = this.data.bookId } //阅读资源的id if (this.data.cmsId) { data.cmsItemId = this.data.cmsId } app.MG.job.newJobWithApiNewEvent(data).then((res) => {}) }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh() { }, /** * 页面上拉触底事件的处理函数 */ onReachBottom() { }, /** * 用户点击右上角分享 */ onShareAppMessage() { }, goBack() { wx.navigateBack() }, resourceDetailsData() { let query = { path: '*', queryType: '*', productId: this.data.bookId, cmsPath: this.data.parentProductLinkPath, itemFields: { SysType: 'CmsFolder', // 资源类型,试读文件,是否允许下载等参数 selectType: [], freeFile: [], file: [], protectedFile: [], resourcesClassification: [], isDownload: [], jsek_resourceBrief: [], jsek_link: [], jsek_questionBank: [], learnSelectType: [] }, pading: { start: 0, size: 999 } } app.MG.store.getProductDetail(query).then((res) => { console.log(res); res.datas.cmsDatas[0].datas.forEach((item) => { //刚刚进来的时候 if (this.data.productLinkPath == item.productLinkPath) { this.handleTeachData(item) this.setData({ titleName: item.name, selectType: item.selectType }) } }) }) }, //zpi文件下载 onDownloadButton() { const item = this.data.zipData; if (this.data.applyState !== 'Normal') { return wx.showToast({ icon: 'none', title: '请先申请下载', }) } else { const flag = new Date(this.data.deadline) > new Date() if (!flag) return wx.showToast({ icon: 'none', title: '请先申请下载', }) } if (!item || !item.file) { wx.showToast({ title: '文件信息缺失', icon: 'none' }); return; } const downloadUrl = app.config.requestCtx + '/file/api/ApiDownload?md5=' + item.file; wx.showLoading({ title: '正在下载...', }); wx.downloadFile({ url: downloadUrl, success(res) { if (res.statusCode === 200) { // 下载成功,可以在这里进行一些操作,例如预览、保存等 // 这里以保存文件到系统为例 wx.saveFile({ tempFilePath: res.tempFilePath, success(saveRes) { wx.hideLoading(); wx.showToast({ title: '保存成功', }); // 获取保存后的文件路径 const savedFilePath = saveRes.savedFilePath; // 可以在这里进行后续操作,例如打开文件 wx.openDocument({ filePath: savedFilePath, success: function () { console.log('打开文档成功') }, fail: function (error) { console.error('打开文档失败', error); } }); }, fail() { wx.hideLoading(); wx.showToast({ title: '保存失败', icon: 'none' }); } }); } else { wx.hideLoading(); wx.showToast({ title: '下载失败', icon: 'none' }); } }, fail() { wx.hideLoading(); wx.showToast({ title: '下载失败', icon: 'none' }); } }); }, //刚进来的时候调用 handleTeachData(item) { //图片 console.log(item, 'item11111'); if (item.selectType == 'picture') { this.setData({ showData: item.file ? app.config.requestCtx + '/file/api/ApiDownload?md5=' + item.file : app.config.requestCtx + '/file/api/ApiDownload?md5=' + item.freeFile, }) } // 下载文件zip if (item.selectType == 'zip') { this.setData({ zipData: item }) } //网页 if (item.selectType == 'webpage') { this.setData({ webpageSrc: item.jsek_link }) } //文档等 if (item.selectType == 'pdf' || item.selectType == 'document') { app.MG.file.getPdfInfo({ md5: item.file }).then((res) => { if (res && res.totalPages) { for (let i = 0; i < res.totalPages; i++) { const src = app.config.requestCtx + '/file/GetPdfPageImage?md5=' + item.file + '&index=' + (i + 1) + '&dpi=300' this.data.naturalResources.push(src) // console.log(this.data.naturalResources, ' this.data.naturalResources'); } } }) } }, handleTap: function () { const naturalResources = this.data.naturalResources; naturalResources.forEach(function (item) { // console.log(item); }); } })