QYF-GitLab1
4 天以前 0b04ed8c61813de116127100c5236bb2ff040682
packageDomain/pages/resourceDetails/document/index.js
@@ -4,8 +4,8 @@
import {
  loginInfo
} from '../../../../assets/js/login';
import XLSX from '../../../../utils/xlsx.mini.min';
Page({
  /**
   * 页面的初始数据
   */
@@ -26,21 +26,41 @@
    parentName: '',
    parentProductLinkPath: '',
    productLinkPath: '',
    isBuy: false,
    showData: '',
    titleName: '',
    selectType: '',
    learnSelectType: '',
    zipData: '',
    naturalResources: [],
    titleName: '',
    pdfDatA: [],
    startTime: "", //进入页面当前时间
    pauseTime: 0, //暂停时间
    formPath: '',
    applyState: '',
    deadline: '',
    lzoomFlag: false, //定义 缩放事件 节流阀,防止一次缩放触发两次缩放事件
    distance: 0, //记录手指移动距离
    scale: 1, //定义初始化的页面缩放大小
    newScale: 1, //记录新的页面缩放大小
    pdfSrc: '',
    buyIdList: [],
    buyList: [],
    learnClassData: null,
    isLearnDataBuy: false,
    bookDetail: null,
    learnStartTime: 0,
    learnTimeList: [],
    learnTimeData: 0,
    threeLeveData: [],
    selectTypeData: '',
    promptVisable: false,
    showMd5: '',
    confirmBtn: {
      content: '确定',
      variant: 'base',
    },
  },
  /**
   * 生命周期函数--监听页面加载
@@ -49,7 +69,6 @@
    wx.setNavigationBarTitle({
      title: '资源详情'
    });
    console.log(options);
    const systInfo = wx.getSystemInfoSync();
    const menu = wx.getMenuButtonBoundingClientRect(); // 胶囊信息
    const navBarHeight = (menu.top - systInfo.statusBarHeight) * 2 + menu.height; // 导航栏高度
@@ -65,30 +84,21 @@
      parentProductLinkPath: options.parentProductLinkPath,
      productLinkPath: options.productLinkPath,
      applyState: options.applyState,
      deadline: options.deadline
      deadline: options.deadline,
      formPath: options.formPath,
    })
    const token = wx.getStorageSync(app.config.tokenKey)
    if (!token) {
      loginInfo(app, (data) => {
        if (data) {
          this.resourceDetailsData()
        } else {
          this.resourceDetailsData()
        }
    if (options.formPath == 'jsek_cloudLearning') {
      this.setData({
        learnStartTime: Date.now(),
      })
    } else {
      this.resourceDetailsData()
      this.getLearnTime()
    }
    this.getBookInfo(this.data.bookId)
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
  },
  onReady() {},
  /**
   * 生命周期函数--监听页面显示
   */
@@ -97,7 +107,6 @@
      startTime: Date.now()
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
@@ -105,12 +114,15 @@
    this.setData({
      pauseTime: Date.now()
    })
    if (wx.getStorageSync(app.config.tokenKey) && epubObj && epubObj.bookBuy) {
    if (wx.getStorageSync(app.config.tokenKey)) {
      let duration = this.data.pauseTime - this.data.startTime
      this.count(duration)
      if (this.data.formPath == 'jsek_cloudLearning') {
        let cloudDuration = this.data.pauseTime - this.data.learnStartTime
        this.cloudCount(cloudDuration)
      }
    }
  },
  /**
   * 生命周期函数--监听页面卸载
   */
@@ -118,12 +130,15 @@
    this.setData({
      pauseTime: Date.now()
    })
    if (wx.getStorageSync(app.config.tokenKey) && epubObj && epubObj.bookBuy) {
    if (wx.getStorageSync(app.config.tokenKey)) {
      let duration = this.data.pauseTime - this.data.startTime
      this.count(duration)
      if (this.data.formPath == 'jsek_cloudLearning') {
        let cloudDuration = this.data.pauseTime - this.data.learnStartTime
        this.cloudCount(cloudDuration)
      }
    }
  },
  count(timeStr) {
    const data = {
      appRefCode: app.config.appRefCode,
@@ -142,20 +157,55 @@
    }
    app.MG.job.newJobWithApiNewEvent(data).then((res) => {})
  },
  getLearnTime() {
    app.MG.identity
      .getUserKey({
        domain: 'cloudLearningTime',
        keys: [this.data.bookId]
      })
      .then((res) => {
        if (res.length > 0) {
          if (JSON.parse(res[0].value) && JSON.parse(res[0].value).length > 0) {
            this.setData({
              learnTimeList: JSON.parse(res[0].value),
              learnTimeData: JSON.parse(res[0].value)[0].learnTime,
            })
          } else {
            this.setData({
              learnTimeList: [],
              learnTimeData: 0
            })
          }
        }
      })
  },
  cloudCount(timeStr) {
    let time = Number(this.data.learnTimeData) + Number(timeStr)
    this.setData({
      learnTimeList: [{
        learnTime: time
      }],
    })
    app.MG.identity
      .setUserKey({
        setKeyRequests: [{
          domain: 'cloudLearningTime',
          key: this.data.bookId,
          value: JSON.stringify(this.data.learnTimeList)
        }]
      })
      .then((res) => {
        console.log(timeStr, '学习时长')
      })
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
  },
  onPullDownRefresh() {},
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
  },
  onReachBottom() {},
  /**
   * 用户点击右上角分享
   */
@@ -164,16 +214,49 @@
  goBack() {
    wx.navigateBack()
  },
  // 获取图书详情
  getBookInfo(id) {
    const query = {
      path: "*",
      queryType: "*",
      productId: id,
      favoriteTypes: "FavoriteBookCity",
      itemFields: {
        "SysType=": "CmsFolder",
      },
      coverSize: {
        height: 1,
      },
      fields: {},
    };
    if (this.data.storeInfo) query['storeInfo'] = this.data.storeInfo
    app.MG.store.getProductDetail(query).then(async (res) => {
      this.setData({
        bookDetail: res.datas
      })
      this.resourceDetailsData();
    });
  },
  // 判断当前目标是否已购买
  async getCmsDataByQrcode() {
    const query = {
      cmsItemId: Number(this.data.activeId)
    };
    return app.MG.file.checkCmsItem(query)
  },
  resourceDetailsData() {
    wx.showLoading({
      title: '正在加载...',
    });
    let query = {
      path: '*',
      queryType: '*',
      cmsType: '*',
      productId: this.data.bookId,
      cmsPath: this.data.parentProductLinkPath,
      cmsPath: this.data.bookDetail.rootCmsItemId,
      itemFields: {
        SysType: 'CmsFolder',
        // 资源类型,试读文件,是否允许下载等参数
        selectType: [],
        freeFile: [],
        file: [],
@@ -193,17 +276,53 @@
    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
    app.MG.store.getProductDetail(query).then(async (res) => {
      let linkData = this.data.productLinkPath.split("\\");
      linkData.reverse()
      let dataList = linkData.map(item => {
        let data = res.datas.cmsDatas[0].datas.find(citem => citem.id == item);
        if (data) return data;
      })
      let isBuy = 1;
      for (let i = 0; i < dataList.length; i++) {
        const dataItem = dataList[i];
        if (dataItem) {
          if (dataItem.saleMethod.length > 0) {
            isBuy = 2;
            if (this.data.bookDetail.purchasedSaleMethodIdList.indexOf(dataItem.saleMethod[0].Id) > -1) {
              isBuy = 3;
              break;
            } else {
              break;
            }
          }
        }
      }
      if (isBuy == 2) {
        // 未购买
        if (dataList[0].freeFile) {
          this.handleTeachData(dataList[0], true)
        } else {
          wx.hideLoading();
          wx.showModal({
            title: '温馨提示',
            content: '该资源未购买,将返回购买',
            success: (res) => {
              if (res.confirm) {
                if (this.data.source == 'qrcode') {
                  wx.navigateTo({
                    url: `/packageBookService/pages/bookServices/detail/index?id=${this.data.bookDetail.id}&name=${this.data.bookDetail.name}&tabValue=${this.data.formPath}`,
                  });
                } else {
                  wx.navigateBack()
                }
              }
            }
          })
        }
      })
      } else {
        this.handleTeachData(dataList[0], false)
      }
    })
  },
  //zpi文件下载
@@ -218,14 +337,15 @@
        })
      }
    }
    if (!item || !item.file) {
    if (!item || !item.file || !item.protectedFile) {
      wx.showToast({
        title: '文件信息缺失',
        icon: 'none'
      });
      return;
    }
    const downloadUrl = app.config.requestCtx + '/file/api/ApiDownload?md5=' + item.file;
    let file = item.file ? item.file : item.protectedFile
    const downloadUrl = app.config.requestCtx + '/file/api/ApiDownload?md5=' + file;
    wx.showLoading({
      title: '正在下载...',
    });
@@ -281,53 +401,87 @@
    });
  },
  //刚进来的时候调用
  handleTeachData(item) {
  handleTeachData(item, isTry) {
    this.setData({
      titleName: item.name,
      selectType: item.selectType,
      learnSelectType: item.learnSelectType,
    })
    let that = this
    //图片
    if (item.selectType == 'picture') {
    if (item.selectType == 'picture' || item.learnSelectType == 'picture') {
      let file = isTry ? item.freeFile : item.protectedFile || item.file
      this.setData({
        showData: item.file ? app.config.requestCtx + '/file/api/ApiDownload?md5=' + item.file : app.config.requestCtx + '/file/api/ApiDownload?md5=' + item.freeFile,
        showData: app.config.requestCtx + '/file/api/ApiDownload?md5=' + file,
      })
      console.log(this.data.showData, 'showData');
      wx.hideLoading();
    }
    // 下载文件zip
    if (item.selectType == 'zip') {
    if (item.selectType == 'zip' || item.learnSelectType == 'zip') {
      this.setData({
        zipData: item
      })
      wx.hideLoading();
    }
    //网页
    if (item.selectType == 'webpage') {
    if (item.selectType == 'webpage' || item.learnSelectType == 'webpage') {
      this.setData({
        webpageSrc: item.jsek_link
      })
      wx.hideLoading();
    }
    //文档等
    if (item.selectType == 'pdf' || item.selectType == 'document') {
      app.MG.file.getPdfInfo({
        md5: item.file
      }).then((res) => {
        let naturalResources = []
        if (res && JSON.parse(res).totalPages) {
          for (let i = 0; i < JSON.parse(res).totalPages; i++) {
            const src = app.config.requestCtx + '/file/GetPdfPageImage?md5=' + item.file + '&index=' + (i + 1) + '&dpi=300'
            naturalResources.push(src)
          }
        }
    if (item.selectType == 'document' || item.selectType == 'pdf' || item.learnSelectType == 'document' || item.learnSelectType == 'pdf') {
      let md5 = isTry ? item.freeFile : item.protectedFile || item.file || item.freeFile
      if (item.fileMap[md5]?.extension == 'xlsx' || item.fileMap[md5]?.extension == 'xls') {
        this.setData({
          naturalResources
          selectTypeData: 'excel',
          showMd5: md5,
        })
        console.log('ppt', this.data.naturalResources);
      })
        wx.request({
          url: app.config.requestCtx + '/file/api/ApiDownload?md5=' + md5,
          method: 'GET', // 请求方式
          responseType: 'arraybuffer', // 指定返回类型为 arraybuffer
          success: (res) => {
            if (res.statusCode === 200) {
              let data = new Uint8Array(res.data)
              let workbook = XLSX.read(data, {
                type: 'array'
              })
              let worksheet = workbook.Sheets[workbook.SheetNames[0]]
              let innerHTML = XLSX.utils.sheet_to_json(worksheet)
              console.log(innerHTML)
              that.setData({
                naturalResources: innerHTML
              })
              wx.hideLoading();
            } else {
              console.error('请求失败', res.statusCode);
            }
          },
          fail: function (err) {
            console.error('请求失败', err);
          }
        });
      } else {
        app.MG.file.getPdfInfo({
          md5: md5
        }).then((res) => {
          let naturalResources = []
          if (res && res.totalPages) {
            for (let i = 0; i < res.totalPages; i++) {
              const src = app.config.requestCtx + '/file/GetPdfPageImage?md5=' + md5 + '&index=' + (i + 1) + '&dpi=300'
              naturalResources.push(src)
            }
          }
          this.setData({
            naturalResources
          })
          wx.hideLoading();
        })
      }
    }
  },
  handleTap: function () {
    const naturalResources = this.data.naturalResources;
    naturalResources.forEach(function (item) {
      // console.log(item);
    });
  },
  onClick() {
    this.setData({
      images: [
@@ -344,24 +498,20 @@
    } = 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,
    });
@@ -373,56 +523,45 @@
      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
  //     })
  //   }
  // },
  // 判断当前点击的数据是否购买
  cmsItemIsBuy(item) {
    let flag = false
    // 买了
    if (this.data.buyList.length) {
      if (this.data.buyList.some(citem => item.productLinkPath.includes(citem.productLinkPath)))
        flag = true
    } else {
      // 没买且无销售方式
      if (!item.saleMethod.length) {
        flag = true
      } else {
        // 没买且销售方式过期//没买,销售方式价格为0
        const itemSaleMethod = item.saleMethod.find(citem => citem.SaleType == 'Normal')
        if (new Date().getTime() > new Date(itemSaleMethod.EndDate).getTime() || itemSaleMethod.Price == 0) flag = true
      }
    }
    return flag
  },
  //
  scrolltolower(e) {
    const activeData = this.data.threeLeveData.find(item => item.id == this.data.activeId)
    const flag = this.data.naturalResources.some(item => item.includes(activeData.freeFile))
    if ((this.data.showMd5 == activeData.freeFile || flag) && activeData.protectedFile)
      this.setData({
        promptVisable: true
      })
  },
  // 提示弹窗关闭
  closePromapDialog() {
    this.setData({
      promptVisable: false
    })
    if (this.data.source == 'qrcode') {
      wx.navigateTo({
        url: `/packageBookService/pages/bookServices/detail/index?id=${this.data.bookDetail.id}&name=${this.data.bookDetail.name}&tabValue=${this.data.formPath}`,
      });
    } else {
      wx.navigateBack()
    }
  }
})