// pages/resourceDetails/document/index.js import Toast from 'tdesign-miniprogram/toast/index'; const app = getApp() import { loginInfo } from '../../../../assets/js/login'; Page({ /** * 页面的初始数据 */ data: { visible: false, showIndex: false, closeBtn: false, deleteBtn: false, images: [], webpageSrc: '', navBarHeight: '', barHeight: '', activeId: '', storeInfo: '', bookId: '', bookName: '', cmsId: '', parentName: '', parentProductLinkPath: '', productLinkPath: '', showData: '', titleName: '', selectType: '', zipData: '', naturalResources: [], titleName: '', pdfDatA: [], startTime: "", //进入页面当前时间 pauseTime: 0, //暂停时间 applyState: '', deadline: '', lzoomFlag: false, //定义 缩放事件 节流阀,防止一次缩放触发两次缩放事件 distance: 0, //记录手指移动距离 scale: 1, //定义初始化的页面缩放大小 newScale: 1, //记录新的页面缩放大小 }, /** * 生命周期函数--监听页面加载 */ 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, storeInfo: options.storeInfo, cmsId: options.cmsId, parentName: options.parentName, parentProductLinkPath: options.parentProductLinkPath, productLinkPath: options.productLinkPath, applyState: options.applyState, deadline: options.deadline }) const token = wx.getStorageSync(app.config.tokenKey) if (!token) { loginInfo(app, (data) => { if (data) { this.resourceDetailsData() } else { this.resourceDetailsData() } }) } }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady() { }, /** * 生命周期函数--监听页面显示 */ onShow() { this.setData({ startTime: Date.now() }) }, /** * 生命周期函数--监听页面隐藏 */ onHide() { this.setData({ pauseTime: Date.now() }) if (wx.getStorageSync(app.config.tokenKey) && epubObj && 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 && 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 } } if (this.data.storeInfo) { query.storeInfo = this.data.storeInfo } 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') { if (!this.data.deadline == '永久') { const flag = new Date(this.data.deadline) > new Date() 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, }) console.log(this.data.showData, 'showData'); } // 下载文件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); }); }, onClick() { this.setData({ images: [ this.data.showData, // 'https://tdesign.gtimg.com/mobile/demos/swiper2.png', ], showIndex: true, visible: true, }); }, onChange(e) { const { index } = e.detail; console.log('change', index); }, onDelete(e) { const { index } = e.detail; Toast({ context: this, selector: '#t-toast', message: `删除第${index + 1}个`, }); }, onClose(e) { const { trigger } = e.detail; console.log(trigger); this.setData({ visible: false, }); }, //预览图片,放大预览 preview(event) { let urls = [event.currentTarget.dataset.src] wx.previewImage({ urls: urls // 需要预览的图片http链接列表 }) }, // myTouchStart(e) { // //---------------------记录缩放事件信息--------------------- // // 当两根手指放上去的时候,将距离(distance)初始化。 // let xMove = e.touches[1].clientX - e.touches[0].clientX; // let yMove = e.touches[1].clientY - e.touches[0].clientY; // //计算开始触发两个手指坐标的距离 // const distance = Math.sqrt(xMove * xMove + yMove * yMove); // this.setData({ // distance: distance // }) // //---------------------记录缩放事件信息end--------------------- // }, // myTouchMove(e) { // // ----------------监听手势缩小放大事件---------------- // // 单手指缩放不做任何操作 // if (e.touches.length != 1) { // //双手指运动 x移动后的坐标和y移动后的坐标 // let xMove = e.touches[1].clientX - e.touches[0].clientX; // let yMove = e.touches[1].clientY - e.touches[0].clientY; // //双手指运动新的 ditance // let newDistance = Math.sqrt(xMove * xMove + yMove * yMove); // //计算移动的过程中实际移动了多少的距离 // let distanceDiff = newDistance - this.data.distance; // // newScale = scale + 0.005 * distanceDiff // console.log('移动距离', distanceDiff); // this.setData({ // newScale: this.data.newScale + 0.005 * distanceDiff // }) // // 打开缩放监听 // // zoomFlag = true // this.setData({ // lzoomFlag: true // }) // return // } // // ----------------监听手势缩小放大事件end---------------- // }, // myTouchEnd() { // if (this.data.lzoomFlag) { // if (this.data.newScale > 1.3) { // console.log("放大了", this.data.newScale); // } else if (this.data.newScale < 0.7, this.data.newScale) { // console.log("缩小了"); // } // // 关闭缩放监听 // // zoomFlag = false // this.setData({ // lzoomFlag: false // }) // } // }, })