Share.vue 7.18 KB
<template>
  <div class="share">
    <van-share-sheet
      v-model="showShare"
      title="立即分享给好友"
      :options="options"
      @select="onSelect"
      @cancel="cancel"
      @click-overlay="cancel"
    />
    <poster
      :value="showPoster"
      @close="showPoster = false"
      :apiName="apiName"
      :currentItem="currentItem"
      :activityData="activityData"
    />
  </div>
</template>

<script type='text/ecmascript-6'>
import { shopmallApi, activityApi, couponApi, liveApi, boutiquecouponApi, equityApi } from '@/api'
import { Notify } from 'vant'
const staticOptions = [
  { name: '企业微信', icon: require('@/assets/images/icon/wx.png'), type: 1 },
  { name: '分享海报', icon: require('@/assets/images/icon/poster.png'), type: 2 },
  { name: '复制链接', icon: require('@/assets/images/icon/copy.png'), type: 3 },
]
export default {
  name: 'Share',
  props: ['value', 'apiName', 'currentItem'],
  data () {
    return {
      showPoster: false,
      showShare: false,
      options: [],
      activityData: {},
      component_id: '',
      component_api_name: ''
    }
  },
  watch: {
    value (n) {
      console.log(n)
      let { currentItem: { id }, component_id, apiName, component_api_name } = this
      if (component_id == id && n && component_api_name == apiName) {
        this.showShare = true
        return
      }
      if (n) {
        let options = this.tools.deepClone(staticOptions)
        if (apiName == 'couponApi' || apiName == 'boutiquecouponApi' || apiName == 'equityApi') {
          options[1].className = 'disabled'
        }
        this.options = options
        if (apiName == 'activityApi' || apiName == 'liveApi') {
          this.showLoading()
          let target = apiName == 'activityApi' ? activityApi : liveApi
          target.getShared({ id }).then(({ code, data, message }) => {
            if (code == '0') {
              this.component_id = id
              this.component_api_name = apiName
              this.showShare = true
              this.activityData = data
              this.hideLoading()
            } else {
              this.hideLoading()
              Notify(message)
              this.$emit('cancel')
            }
          })
          return
        }
        if (apiName == 'shopmallApi') {
          shopmallApi.getGoodsLink({ id }).then(data => {
            console.log(data)
            this.activityData = {
              url: data
            }
          })
          shopmallApi.getGoodsPoster({ id }).then(res => {
            console.log(res)
            this.activityData = {
              posterUrl: res
            }
          })
        }
        this.showShare = true
      }
    }
  },
  mounted () {
  },
  methods: {
    closePoster () {
      this.showPoster = false
    },
    onSelect ({ type, className }) {
      let { currentItem, apiName, activityData } = this
      switch (type) {
        case 1:
          let { userId } = currentItem
          /**卡券分享微信 */
          if (apiName == 'couponApi' || apiName == 'boutiquecouponApi') {
            let { id, couponName } = currentItem
            const desc = '您的专属服务顾问为您发来了优惠券,请点击领取~'
            if (apiName == 'couponApi') {
              this.couponShare(currentItem, 1, res => {
                if (userId) this.sendChatMessage(res.url, desc, couponName)
                else this.shareAppMessage(res.url, desc, couponName)
              })
            } else {
              this.getBoutiqueCoupon(id, res => {
                if (userId) this.sendChatMessage(res, desc, couponName)
                else this.shareAppMessage(res, desc, couponName)
              })
            }
          }
          if (apiName == 'activityApi' || apiName == 'liveApi') {
            let { activityData } = this
            if (apiName == 'activityApi') {
              if (userId) this.sendChatMessage(activityData.linkUrl, '', activityData.activityName)
              else this.shareAppMessage(activityData.linkUrl, '', activityData.activityName)
            } else {
              if (userId) this.sendChatMessage(activityData.cdnUrl, '', currentItem.name, currentItem.imgUrl)
              else this.shareAppMessage(activityData.cdnUrl, '', currentItem.name, currentItem.imgUrl)
            }
          }
          if (apiName == 'equityApi' || apiName == 'shopmallApi') {
            let { id } = currentItem
            if (apiName == 'equityApi') {
              equityApi.getEquityShared({ id }).then(res => {
                let desc = `价值${currentItem.packagePrice}元的${currentItem.list.length || 0}项权益`
                if (userId) this.sendChatMessage(res.url, desc, currentItem.packageName)
                else this.shareAppMessage(res.url, desc, currentItem.packageName)
              })
            } else {
              let { activityData } = this
              if (userId) this.sendChatMessage(activityData.url, '', currentItem.goodsName, currentItem.goodsImage)
              else this.shareAppMessage(activityData.url, '', currentItem.goodsName, currentItem.goodsImage)
            }
          }
          this.cancel()
          break;
        case 2:
          if (className) return
          this.showPoster = true
          break;
        case 3:
          let { id } = currentItem
          this.showLoading()
          if (apiName == 'shopmallApi') {
            let { activityData } = this
            this.setClipboardData(activityData.url)
          } else if (apiName == 'activityApi') {
            this.setClipboardData(activityData.linkUrl)
          } else if (apiName == 'liveApi') {
            this.setClipboardData(activityData.pagePath)
          } else if (apiName == 'couponApi') {
            this.couponShare(currentItem, 0, 3)
          } else if (apiName == 'boutiquecouponApi') {
            this.getBoutiqueCoupon(id, res => {
              this.setClipboardData(res)
            })
          } else {
            equityApi.getEquityShared({ id }).then(res => {
              this.setClipboardData(res.url)
            })
          }
          break;
      }
    },
    cancel () {
      this.$emit('cancel')
    },
    couponShare (item, shareType, callBack) {
      let { userId, id } = item
      let obj = {
        couponId: id,
        shareType
      }
      if (userId) obj.userList = [
        {
          userId
        }
      ]
      else obj.shareType = 0
      couponApi.getCouponShared(obj).then(res => {
        if (shareType == 0) this.setClipboardData(res.url)
        if (shareType == 1) callBack(res.url)
      })
    },
    getBoutiqueCoupon (id, callBack) {
      boutiquecouponApi.getBoutiqueCoupon({ id }).then(res => {
        callBack(res)
      })
    },
    sendChatMessage (link, desc, title, imgUrl) {
      window.$wx.sendChatMessage({
        msgtype: 'news',
        news: {
          link, //H5消息页面url 必填
          desc,
          title
        },
        enterChat: false
      }).then(() => {
        this.showToast('发送成功')
      })
    },
    shareAppMessage (link, desc, title, imgUrl) {
      window.$wx.shareAppMessage({
        title,
        desc,
        link,
        imgUrl
      })
    }
  }
}
</script>

<style  lang='scss'>
.van-share-sheet__option.disabled {
  opacity: 0.5;
}
</style>