psdView.js 6.67 KB
// pages/psdView/psdView.js
import {
  psdApi
} from '~/api/api'
Page({
  /**
   * 页面的初始数据
   */
  data: {
    current: 0,
    params: {},
    html: '',
    showLogin: true,
    autoplayTime: 3000
  },
  loginCallBack(data) {
    this.check(data)
  },
  check(data) {
    if (data) {
      if (data.isLogin) {
        this.setData({
          userInfo: data
        })
        this.viewReadBuriedPoint()
      } else {
        this.setData({
          showLogin: true
        })
      }
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let userInfo = wx.$util.getUserInfo()
    console.log(userInfo)
    if (userInfo && userInfo.phone) {
      this.loginCallBack(userInfo)
    } else {
      wx.$router.go('/pages/Login/Login')
    }
  },
  emitUserInfo({
    userInfo
  }) {
    this.loginCallBack(userInfo)
  },
  viewReadBuriedPoint() {
    let {
      id,
      merchantId,
      staffUuid
    } = this.options
    this.getPsdDetail()
    if (!staffUuid) return
    let {
      phone
    } = this.data.userInfo
    psdApi
      .viewReadBuriedPoint({
        type: 2,
        materialId: id,
        merchantId,
        staffUuid,
        phone
      })
      .then(res => {
        console.log(res)
      })
    wx.$Track.setTrack({
        type: 2,
        materialId: id,
        merchantId,
        staffUuid,
        phone
      },
      6
    )
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},
  getPsdDetail() {
    let {
      id,
      objectCombId
    } = this.options
    console.log(this.options, 'this.optionsthis.options')
    wx.$api.showLoading()
    psdApi
      .getPsdDetail({
        id
      })
      .then(({
        data,
        code,
        message
      }) => {
        wx.hideLoading()
        if (code == 0) {
          console.log(data)
          this.setData({
            obj: data,
            flag: true,
            objectCombId: objectCombId
          })
          // materialType	 素材类型 1 PDF 2 自定义链接 else 富文本
          wx.setNavigationBarTitle({
            title: data.materialName
          })
          if (data.materialType == 1) {
            wx.$api.showLoading('打开中...')
            wx.downloadFile({
              url: data.materialUrl,
              header: {
                'content-type': 'application/json'
              },
              success(res) {
                wx.hideLoading()
                wx.openDocument({
                  filePath: res.tempFilePath
                })
              },
              fail(res) {
                wx.hideLoading()
                wx.$api.showToast('打开失败')
              }
            })
          } else if (data.materialType == 2) {
            console.log('链接')
          } else {
            let fileList = []
            if (data.videoUrl) {
              fileList.push({
                type: 'video',
                url: data.coverUrl,
                videoUrl:data.videoUrl
              })
            }
            if (data.imageUrlList && data.imageUrlList.length) {
              data.imageUrlList.forEach(item => {
                fileList.push({
                  type: 'image',
                  url: item
                })
              })
            }
            fileList.map(async (e, index) => {
              let height = 400
              if (e.type == 'image') {
                const img = await wx.getImageInfo({
                  src: e.url
                })
                height = img.height/img.width * 750
              }
              if (e.type == 'video') {
                try {
                  const video = await wx.getImageInfo({
                    src: e.url
                  })
                  height = video.height/video.width * 750
                } catch (error) {
                  height = 400
                }
              }
              console.log(height)
              e.height = height
              if (index == 0) {
                this.setData({
                  height
                })
              }
            })
            this.setData({
              productList: fileList,
              html: decodeURIComponent(data.materialUrl).replace(/\"/g, `'`)
            })
          }
        } else {
          if (code == 21) {
            wx.$router.go('/pages/Login/Login')
          } else {
            wx.showModal({
              title: '提示',
              content: message,
              showCancel: false,
              confirmText: '知道了',
              confirmColor: '#FC5B32'
            }).then(res => {
              if (res.confirm) {
                wx.$router.goHome()
              }
            })
          }
        }
      })
  },

  jumpActive() {
    // console.log(this.options,'sss')
    const {
      marketingType,
      objectCombId,
      objectType
    } = this.options
    /**
     * marketingType(1秒杀,3众筹,5膨胀金)
     * objectType=13时才有此参数
     */
    let path = {
        13: '/packageShopmall/packageMall/pages/detail/detail',
        14: '/packageShopmall/packageEquity/pages/freecard/freecard',
        15: '/packageShopmall/packageEquity/pages/package/package',
        16: '/packageShopmall/packageMall/pages/coupon/coupon'
      } [objectType],
      params = {
        id: objectCombId,
        notIntercept: true
      }
    if (marketingType && objectType == 13) {
      if (marketingType != 1) path = {
        3:'/pages/crowdfunding/index/index',
        5:'/packageShopmall/packageExpandedGold/pages/activity/index/index'
      }[marketingType]
      else params.isActivity = true
    }
    let query = []
    Object.keys(params).map(key =>{
      query.push(`${key}=${params[key]}`)
    })
    // console.log( wx.$authStatus)
    wx.$router.go(`${path}?${query.join('&')}`, params)
  },
  cancel() {
    wx.$router.goHome()
  },
  startPlay() {
    this.setData({
      autoplayTime: 0
    })
  },
  pause() {
    this.setData({
      autoplayTime: 3000
    })
  },
  played() {
    this.setData({
      autoplayTime: 3000
    })
  },
  onChange(e) {
    const current = e.detail.current,
      {
        productList
      } = this.data
    this.setData({
      current,
      height: productList[current].height
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    wx.$util.customerbehavior(2)
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {}
})