// packageBookService/pages/components/webView/index.js const app = getApp() import { loginInfo } from '../../../../assets/js/login'; Page({ /** * 页面的初始数据 */ data: { src: "", link: false, epubObj: null, currentPage: '', pdfList: [], skeletonLoding: true, startTime: "", //进入页面当前时间 pauseTime: 0 //暂停时间 }, /** * 生命周期函数--监听页面加载 */ onLoad(options) { console.log(options) if (options && options.link) { this.setData({ src: decodeURIComponent(options.link), link: options.link ? true : false, }) } else { this.setData({ epubObj: options, src: '' }) if (options) { wx.setNavigationBarTitle({ title: options.fileName, skeletonLoding: true }) } } const token = wx.getStorageSync(app.config.tokenKey) if (!token) { loginInfo(app, (data) => { if (data) { this.getProgress(options) } else { this.getProgress(options) } }) } else { this.getProgress(options) } }, onLoadWeb(e) { console.log(e, "load") }, onError(e) { console.log(e, "err") }, setWebViewSrc: function (data) { let that = this; var url = ""; if (data.fileType == 'epub') { if (data.bookBuy == "true") { url = app.config.epubUrl + "?md5=" + data.md5 + "&bookName=" + data.fileName + "&url=" + app.config.requestCtx + "&token=" + wx.getStorageSync(app.config.tokenKey) + "&recordLocation=" + this.data.currentPage } else { url = app.config.epubUrl + "?md5=" + data.md5 + "&bookName=" + data.fileName + "&url=" + app.config.requestCtx + "&token=" + wx.getStorageSync(app.config.tokenKey) + "&recordLocation=" + that.data.currentPage + "&isPreview=true&freeEpubPage=" + data.freePage } that.setData({ src: url, skeletonLoding: false, }) } else { that.setData({ navigationBarTitleText: data.fileName }) app.MG.file.getPdfInfo({ md5: data.md5 }).then((res) => { let pageCount = data.bookBuy == "true" ? JSON.parse(res).totalPages : Number(data.freePage) console.log(pageCount, "connt") if (pageCount) { let list = []; for (let i = 0; i < pageCount; i++) { const src = app.config.requestCtx + '/file/GetPdfPageImage?md5=' + data.md5 + '&index=' + (i + 1) + '&dpi=300' list.push(src) } that.setData({ pdfList: list, skeletonLoding: false, }) } }) } }, // 获取UserKey getProgress: function (dataObj) { app.MG.identity .getUserKey({ domain: "bookReadProgress", keys: [this.data.epubObj.md5], }) .then((res) => { if (res.length) { try { this.setData({ currentPage: JSON.parse(res[0].value).page, }); } catch (error) { this.setData({ currentPage: "", }); } if (this.data.currentPage > this.data.probationPage) { this.setData({ currentPage: "", }); } } this.setWebViewSrc(dataObj); }); }, // 设置userKey setProgress: function (data) { if (data.page && data.page != this.data.currentPage) { app.MG.identity .setUserKey({ setKeyRequests: [{ domain: "bookReadProgress", key: this.data.epubObj.md5, value: JSON.stringify(data), }, ], }) .then((res) => {}); } }, drawBack(e) { let { currentLocation, percentage, type } = e.detail.data[0]; if (type == "progress" && type != "backDetail") { this.setProgress({ page: currentLocation, progress: percentage, }); } else if (type == "backDetail") { this.setProgress({ page: 1, progress: "0%", }); } }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady() { }, /** * 生命周期函数--监听页面显示 */ onShow() { this.setData({ startTime: Date.now() }) }, /** * 生命周期函数--监听页面隐藏 */ onHide() { this.setData({ pauseTime: Date.now() }) if (wx.getStorageSync(app.config.tokenKey) && this.data.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) && this.data.epubObj.bookBuy == 'true') { 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.epubObj.bookId) { data.productId = this.data.epubObj.bookId } //阅读资源的id // if (product.cmsItemId) { // data.cmsItemId = product.cmsItemId // } app.MG.job.newJobWithApiNewEvent(data).then((res) => {}) }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh() { }, /** * 页面上拉触底事件的处理函数 */ onReachBottom() { }, /** * 用户点击右上角分享 */ onShareAppMessage() {}, onShareTimeline() {}, })