index.js 9.26 KB
import {
  showEmoji
} from '~/utils/emoji'
import ActivityApi from '~/api/activityApi/index'
import {
  decimal,
  toUnicode
} from '~/utils/format'
Page({

  data: {
    current: 0,
    productList: [],
    nextStep: 0,
  },

  onLoad(options) {
    if (options && options.id) {
      this.onRoute(options)
    }
  },

  onRoute(options) {
    if (this.isupdate) return
    this.isupdate = true
    this.setData({
      staffAvator: options.avatar || (options.shareStaffImageUrl ? decodeURIComponent(options.shareStaffImageUrl) : ''),
      staffName: options.name || (options.shareStaffName ? this.toChineseStr(options.shareStaffName) : ''),
      merchantName: options.merchantName || (options.merchant ? this.toChineseStr(options.merchant) : ''),
      nextStep: options.showTip && !(wx.$storage.get('closePYQTip')) ? 1 : 0,
      options
      // showToMiniTip:options.shareChannel&&wx.$storage.get('sessionId')
    }, () => {
      this.getTaskInfo(options.missionId)
    })
  },
  toNext(e) {
    const {
      index
    } = e.currentTarget.dataset
    if (index == 2) {
      wx.$storage.set('closePYQTip', true)
    }
    this.setData({
      nextStep: index == 1 ? 2 : 0
    })
  },
  jumpZC() {
    let appEnter = wx.getEnterOptionsSync()
    if (appEnter.scene === 1154) {
      wx.$eventbus.emit('showTimeLineToast')
      return
    }
    let query = {
      id: this.data.options.id,
      exclusiveUuid: this.data.options.shareStaffUuid,
      merchantId: this.data.options.merchantId,
      isExclusive: 1,
      shareStaffUuid: this.data.options.shareStaffUuid,
      form: 3
    }
    if (!this.data.options.tabType || this.data.options.tabType == 0) {
      wx.$router.go('/pages/crowdfunding/index/index', query)
    } else {
      query.notIntercept = true
      wx.$router.go('/packageShopmall/packageExpandedGold/pages/activity/index/index', query)
    }
  },
  // 获取任务详情
  getTaskInfo(missionId) {
    ActivityApi.getTaskShareDetail({
      missionId: missionId
    }).then(res => {
      if (!res) {
        wx.showToast({
          title: '获取任务详情失败',
          icon: 'none'
        })
        return
      }
      // 处理轮播素材
      let materialList = []
      let taskText = ''
      if (res.platformMerchantMissionMaterialResponseList?.length) {
        res.platformMerchantMissionMaterialResponseList.map(item => {
          switch (item.materialType) {
            case 1:
              taskText = `<view>${showEmoji(item.materialContent)}</view>`
              break;
            case 2:
              materialList.push({
                type: 'image',
                url: item.materialContent,
                firstFrameUrl: item.firstFrameUrl
              })
              break;
            case 3:
              materialList.push({
                type: 'video',
                url: item.materialContent,
                firstFrameUrl: item.firstFrameUrl
              })
              break;
            default:
              break
          }
        })
        res.materialList = materialList
        materialList.map(async (e, index) => {
          let height = 1000
          if(e.type == 'image'){
            const img = await wx.getImageInfo({
              src: e.url
            })
            height = img.height
          }
          if(e.type == 'video'){
            try {
                const video = await wx.getVideoInfo({
                    src: e.url
                })
                height = video?.height
            } catch (error) {
                height = 1000
            }
          }
          e.height = height
          if (index == 0) {
            this.setData({
              height
            })
          }
        })
        this.setData({
          productList: materialList,
          producktDots: (128 / materialList.length) + 'rpx',
          taskText
        })

      }
      this.setData({
        textContent: res.textContent
      })
      this.setShareInfo(res)
    })
  },
  onChange(e) {
    const current = e.detail.current,
      {
        productList
      } = this.data
    this.setData({
      current,
      height: productList[current].height
    })
  },
  previewMedia(e) {
    wx.previewMedia({
      sources: this.data.productList,
      current: e.currentTarget.dataset.index
    })
  },
  toChineseStr(unicodeStr) {
    console.log(unicodeStr, 'unicodeStr');
    if (unicodeStr == '') {
      return 'Please input hexadecimal Unicode';
    }
    if (unicodeStr.indexOf('@') == -1) {
      return unicodeStr
    }
    unicodeStr = unicodeStr.split('@');
    console.log(unicodeStr, 'unicodeStr');
    let chineseStr = '';
    for (let i = 1, iLength = unicodeStr.length; i < iLength; i++) {
      chineseStr += String.fromCharCode(parseInt(unicodeStr[i], 16));
    }
    return chineseStr.trim();
  },
  setShareInfo(data) {
    let name = '',
      openId = '',
      logo = '',
      userPhone = ''
    const sharedTime = new Date().getTime()
    const userInfo = wx.$storage.get('userInfo') || {},
      params = {
        shareStaffUuid: userInfo.uuid,
        exclusiveUuid: userInfo.uuid,
        merchantId: userInfo.merchantId,
        isExclusive: 1,
      },
      {
        activityId,
        posterImg,
        activityImage,
        activityName,
        materialList,
      } = data
    if (userInfo) {
      name = userInfo.name || ''
      openId = userInfo.weChatOpenId || ''
      logo = userInfo.sta_avatarUrl || userInfo.weChatAvatarUrl || ''
      userPhone = userInfo.phone || ''
    }
    let imagesurl = ''
    if (materialList && materialList.length) {
      imagesurl = materialList[0].url
      if (materialList[0].type == 'video') {
        imagesurl = materialList[0].firstFrameUrl
      }
    }
    let query = `id=${activityId}&shareStaffType=1&shareStaffName=${toUnicode(name)}&shareStaffImageUrl=${encodeURIComponent(logo)}&openId=${openId}&sharedTime=${sharedTime}&merchant=${toUnicode(this.data.merchantName)}`
    if (userPhone) query = `${query}&userPhone=${userPhone}`
    if (params && params.shareStaffUuid) query = `${query}&shareStaffUuid=${params.shareStaffUuid}`
    if (params && params.merchantId) query = `${query}&merchantId=${params.merchantId}`
    if (params && params.isExclusive) query = `${query}&isExclusive=${params.isExclusive}`
    if (params && params.exclusiveUuid) query = `${query}&exclusiveUuid=${params.exclusiveUuid}`
    const title = `${name}邀请您参加${activityName}`
    this.shareAppMessage = {
      title,
      path: `/${this.route}?${query}`,
      imageUrl: imagesurl || posterImg || activityImage,
      query
    }
    this.activityName = activityName
    console.log(this.shareAppMessage);
  },
  tracker() {
    const USERINFO = wx.$storage.get('userInfo')
    let data = {
      action_type: 4, // 事件类型固定为4-分享
      item_type: 3, // 商品类型,固定为3-众筹活动
      merchant_id: USERINFO.merchantId, // 门店号
      staff_uuid: USERINFO.uuid,
      business_type: 1, // 业务类型,固定为1
      add_way: 104, // 来源,固定为104
      create_time: new Date().format('yyyy-MM-dd hh:mm:ss'), // 分享时间
      item_id: this.data.options ? this.data.options.id : 0,
      share_staff_type: 3
    }
    let jsonList = [JSON.stringify(data)]
    ActivityApi.taskShareReport({
      jsonList
    }).then(res => {})
  },
  judgeCustomer() {
    const userInfo = wx.$util.getUserInfo()
    const sharedTime = new Date().getTime()
    const staff = wx.$storage.get('userInfo')
    if (userInfo || (!staff && !userInfo)) {
      const {
        shareStaffUuid,
        merchantId,
        exclusiveUuid,
        isExclusive,
        id,
        shareStaffName,
        shareStaffImageUrl,
        openId,
        userPhone,
        merchant
      } = this.options
      let query = `id=${id}&shareStaffType=1&shareStaffName=${shareStaffName}&shareStaffImageUrl=${shareStaffImageUrl}&openId=${openId}&sharedTime=${sharedTime}`
      if (userPhone) query = `${query}&userPhone=${userPhone}`
      if (shareStaffUuid) query = `${query}&shareStaffUuid=${shareStaffUuid}`
      if (merchantId) query = `${query}&merchantId=${merchantId}`
      if (isExclusive) query = `${query}&isExclusive=${isExclusive}`
      if (exclusiveUuid) query = `${query}&exclusiveUuid=${exclusiveUuid}`
      if(merchant)query = `${query}&merchant=${merchant}`
      const title = `${this.data.staffName}邀请您参加${this.activityName}`
      this.shareAppMessage.title = title
      this.shareAppMessage.path = `/${this.route}?${query}`
      this.shareAppMessage.query = query
    }
  },
  onShareAppMessage() {
    this.tracker()
    this.judgeCustomer()
    let {
      shareAppMessage
    } = this,
    path = `${shareAppMessage.path}&shareChannel=1&inType=2&missionId=${this.options.missionId}&tabType=${this.options.tabType}`
    console.log('右上角转发', path)
    return {
      title: shareAppMessage.title,
      path,
      imageUrl: shareAppMessage.imageUrl
    }
  },
  /**
   * 用户点击右上角分享朋友圈
   */
  onShareTimeline() {
    this.tracker()
    this.judgeCustomer()
    let {
      shareAppMessage
    } = this,
    query = `${shareAppMessage.query}&shareChannel=2&missionId=${this.options.missionId}&tabType=${this.options.tabType}`
    console.log('右上角转发朋友圈', query)
    return {
      title: shareAppMessage.title,
      query,
      imageUrl: shareAppMessage.imageUrl
    }
  }
})