<template>
  <div class="shareBox">
    <div>
      <h4 @click="a()">分享到</h4>
      <van-grid square :border="false">
        <van-grid-item v-if="value.id == 'weixin'" @click="query_share(value, 'WXSceneSession')" v-for="(value, index) in shares" :key="index" icon="photo-o" text="微信朋友">
          <icon name="weixin" slot="icon"></icon>
        </van-grid-item>
        <van-grid-item
          v-if="value.id == 'weixin'"
          :key="index + 1"
          v-for="(value, index) in shares"
          icon="photo-o"
          text="微信朋友圈"
          @click="query_share(value, 'WXSceneTimeline')"
        >
          <icon name="pyq" slot="icon"></icon>
        </van-grid-item>
        <van-grid-item v-if="value.id == 'qq'&&isShowQQ" :key="index + 1" v-for="(value, index) in shares" icon="photo-o" text="QQ" @click="query_share(value, 'WXSceneTimeline')">
          <icon name="qq" slot="icon"></icon>
        </van-grid-item>
      </van-grid>
    </div>
  </div>
</template>

<script>
export default {
  name: 'share',
  model: {
    prop: 'share_data',
    event: 'share_data_update'
  },
  props: {
    share_data: {
      type: Object
    },
    isShowQQ: {
      type: Boolean,
      default: false
    }
  },
  data () {
    return {
      data: {},
      plus: {},
      shares: []
    }
  },
  created () {
    let self = this
    if (window.plus) {
      self.plus = plus
      try {
        plus.share.getServices( // 获取分享服务
          function (s) {
            console.log(JSON.stringify(s))
            self.shares = s
          },
          function (e) {
            alert('获取分享服务列表失败：' + e.message)
          }
        )
      } catch (error) {
        return false
      }
    }
  },
  methods: {
    a () {
      this.$emit('successFx', true)
    },
    query_share (s, type) {
      let self = this
      this.$emit('share', { type })
      // "WXSceneSession"分享到微信的“我的好友”； "WXSceneTimeline"分享到微信的“朋友圈”中；
      if (s.id == 'qq') {
        let href = self.share_data.href
        try {
          s.send(
            { content: self.share_data.content,
              title: self.share_data.title,
              href: href,
              thumbs: this.share_data.thumbs,
              extra: { scene: type }
            },
            function (ret) {
              self.$emit('successFx', true)
            },
            function (e) {
              // alert('分享失败：' + e.message)
            }
          )
        } catch (error) {
          return false
        }
      }
      if (s.id == 'weixin') {
        let href = self.share_data.href
        try {
          s.send(
            { content: self.share_data.content,
              title: self.share_data.title,
              href: href,
              thumbs: this.share_data.thumbs,
              type: this.share_data.type,
              extra: { scene: type }
            },
            function (ret) {
              self.$emit('successFx', true)
            },
            function (e) {
              // alert('分享失败：' + e.message)
            }
          )
        } catch (error) {
          return false
        }
      }
    }
  }
}
</script>

<style scoped>
.shareBox {
  padding: 0.16rem 0.32rem;
  padding-bottom: 0.32rem;
}
.shareBox h4 {
  padding: 0.16rem 0;
  margin: 0;
}
.shareBox svg {
  width: 1.2rem;
}
</style>
