闫增涛
2024-11-05 50ca1ad0d141d7dc0ddf29b2d3051b85ceff8f56
packageBookService/pages/components/webView/index.js
@@ -1,5 +1,8 @@
// packageBookService/pages/components/webView/index.js
const app = getApp()
import {
  loginInfo
} from '../../../../assets/js/login';
Page({
  /**
@@ -12,12 +15,16 @@
    currentPage: '',
    pdfList: [],
    skeletonLoding: true,
    startTime: "", //进入页面当前时间
    pauseTime: 0 //暂停时间
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log(options)
    if (options && options.link) {
      this.setData({
        src: decodeURIComponent(options.link),
@@ -29,13 +36,23 @@
        src: ''
      })
      if (options) {
        console.log(options)
        wx.setNavigationBarTitle({
          title: options.fileName,
          skeletonLoding: true
        })
        this.getProgress(options)
      }
    }
    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)
    }
  },
@@ -51,7 +68,7 @@
    let that = this;
    var url = "";
    if (data.fileType == 'epub') {
      if (data.bookBuy) {
      if (data.bookBuy == "true") {
        url = app.config.epubUrl +
          "?md5=" +
          data.md5 +
@@ -72,40 +89,36 @@
          "&token=" +
          wx.getStorageSync(app.config.tokenKey) +
          "&recordLocation=" +
          this.data.currentPage + "&freeEpubPage=" + data.freePage
          that.data.currentPage + "&isPreview=true&freeEpubPage=" + data.freePage
      }
      this.setData({
      that.setData({
        src: url,
        skeletonLoding: false,
      })
    } else {
      this.setData({
      that.setData({
        navigationBarTitleText: data.fileName
      })
      app.MG.file.getPdfInfo({
        md5: data.md5
      }).then((res) => {
        let pageCount = data.bookBuy ? res.totalPages : Number(data.freePage)
        let pageCount = data.bookBuy == "true" ? 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)
          }
          console.log(list)
          this.setData({
          that.setData({
            pdfList: list,
            skeletonLoding: false,
          })
        }
      })
    }
  },
  // 获取UserKey
  getProgress: function (dataObj) {
@@ -179,21 +192,62 @@
   * 生命周期函数--监听页面显示
   */
  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) => {})
  },
  //预览图片,放大预览
  preview(event) {
    let urls = [event.currentTarget.dataset.src]
    wx.previewImage({
      urls: urls // 需要预览的图片http链接列表
    })
  },
  /**
@@ -209,4 +263,9 @@
  onReachBottom() {
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {},
  onShareTimeline() {},
})