// packageBookService/pages/components/webView/index.js const app = getApp() Page({ /** * 页面的初始数据 */ data: { src: "", link: false, epubObj: null, currentPage: '', }, /** * 生命周期函数--监听页面加载 */ onLoad(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, }) this.getProgress(options) } } }, onLoadWeb(e) { console.log(e, "load") }, onError(e) { console.log(e, "err") }, setWebViewSrc: function (data) { var url = app.config.epubUrl + "?md5=" + data.md5 + "&bookName=" + data.fileName + "&url=" + app.config.requestCtx + "&token=" + wx.getStorageSync(app.config.tokenKey) + "&recordLocation=" + this.data.currentPage this.setData({ src: url }) }, // 获取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() { }, /** * 生命周期函数--监听页面隐藏 */ onHide() { }, /** * 生命周期函数--监听页面卸载 */ onUnload() { }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh() { }, /** * 页面上拉触底事件的处理函数 */ onReachBottom() { }, })