Share.vue 7.99 KB
<template>
  <div
    class="share-box"
    v-show="showShare"
  >
    <div class="sheet">
      <van-share-sheet
        v-model="showShare"
        title="立即分享给好友"
        :options="options"
        @select="onSelect"
        @cancel="cancel"
        @click-overlay="cancel"
      />
    </div>
    <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 , userId },
        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' || apiName == 'shopmallApi') {
          if (apiName == 'shopmallApi'  && !userId) {
            options[1].className = 'disabled'
          } else {
            options[1].className = 'disabled'
          }
          this.showShare = true
        }
        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') {
          this.showLoading()
          shopmallApi.getGoodsLink({ id }).then(data => {
            console.log('商品链接', data)
            shopmallApi.getGoodsPoster({ id }).then(res => {
              console.log('商品海报', res)
              this.activityData = {
                posterUrl: res,
                url: data
              }
              this.showShare = true
              this.hideLoading()
            })
          })
        }
      }
    }
  },
  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, desc, couponName)
                else this.shareAppMessage(res, 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') {
            if (apiName == 'activityApi') {
              if (userId) this.sendChatMessage(activityData.linkUrl, '', activityData.activityName, activityData.posterUrl)
              else this.shareAppMessage(activityData.linkUrl, '', activityData.activityName, activityData.posterUrl)
            } else {
              if (userId) this.sendChatMessage(activityData.cdnUrl, '', currentItem.name, currentItem.shareImgUrl)
              else this.shareAppMessage(activityData.cdnUrl, '', currentItem.name, currentItem.shareImgUrl)
            }
          }
          if (apiName == 'equityApi' || apiName == 'shopmallApi') {
            let { id } = currentItem
            if (apiName == 'equityApi') {
              equityApi.getEquityShared({ id }).then(res => {
                let desc = `您的专属客服顾问为您发来了会员套餐,点击查看哦`
                if (userId) this.sendChatMessage(res.url, desc, currentItem.packageName)
                else this.shareAppMessage(res.url, desc, currentItem.packageName)
              })
            } else {
              console.log('________________________', activityData.url)
              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) {
            this.showToast('无对应内容')
            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,
            imgUrl
          },
          enterChat: false
        })
        .then(() => {
          this.showToast('发送成功')
        })
    },
    shareAppMessage(link, desc, title, imgUrl) {
      window.$wx
        .shareAppMessage({
          title,
          desc,
          link,
          imgUrl
        })
        .then(() => {
          this.showToast('发送成功')
        })
    }
  }
}
</script>

<style  lang='scss'>
.van-share-sheet__option.disabled {
  opacity: 0.5;
}
.sheet {
  position: relative;
  z-index: 1;
}
.share-box {
  position: fixed;
  width: 100%;
  height: 100vh;
  top: 0;
  left: 0;
  z-index: 100;
}
</style>