yiming
2024-03-13 f9d4b09377c5471e1202be2fef2c89de27b6654d
pages/resourceDetails/document/index.js
@@ -1,18 +1,50 @@
// pages/resourceDetails/document/index.js
const app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    webpageSrc: '',
    navBarHeight: '',
    barHeight: '',
    activeId: '',
    bookId: '',
    bookName: '',
    cmsId: '',
    parentName: '',
    parentProductLinkPath: '',
    productLinkPath: '',
    showData: '',
    titleName: '',
    selectType: '',
    zipData: '',
    naturalResources: [],
    titleName: '',
    pdfDatA: [],
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    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,
      cmsId: options.cmsId,
      parentName: options.parentName,
      parentProductLinkPath: options.parentProductLinkPath,
      productLinkPath: options.productLinkPath
    })
    this.resourceDetailsData()
  },
  /**
@@ -62,5 +94,174 @@
   */
  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
  }
    }
    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 (!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) {
    //图片
    if (item.selectType == 'picture') {
      this.setData({
        showData: app.config.requestCtx + '/file/api/ApiDownload?md5=' + item.file,
      })
    }
    // 下载文件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);
    });
  }
})