<template>
  <div class="share">
    <cr-share
      v-model="showShare"
      :types="types"
      @close="shareCloseChange"
      @click-share-item="bundleShareClickItem"
    />
  </div>
</template>
<script>
import { isWxMp, isApp } from '@/service/validation.service';
import Bridge from '@qg/js-bridge';
import MpBridge from '@/service/mp';
export default {
  name: 'GroupShare',
  components: {},
  props: {},
  data() {
    return {
      nativeBridge: null,
      showShare: false,
      types: [
        { value: 'weixin', disabled: false },
        { value: 'pic', disabled: false }
      ]
    };
  },
  computed: {
    notBwoser() {
      return !isWxMp && !isApp;
    }
  },
  watch: {},
  created() {
    if (isApp) this.nativeBridge = new Bridge();
    else if (isWxMp) this.nativeBridge = new MpBridge();
  },
  methods: {
    async open() {
      try {
        let options = await this.getShareData();

        // 判断是否时小程序平台
        if (!isWxMp) {
          this.showShare = true;
        }
        // 判断是否是App
        if (!isApp) {
          this.shareAppChange(options);
        }
      } catch (err) {
        console.log(err);
        this.$toast('调用分享失败!');
      }
    },

    async getShareData() {
      return Promise.resolve({
        title: '随便砍砍就能拿，价格你来定',
        desc: '我正在免费拿商品，请你帮帮我',
        link: `${window.location.origin}/activity/zeroBuy`, // 页面地址
        imgUrl: 'https://appsync.lkbang.net/shareIcon.png' // 图片地
      });
      // await
    },

    shareCloseChange() {
      this.$emit('hideShare', false);
    },
    bundleShareClickItem(ev) {
      console.log(ev);
      console.log(this.nativeBridge);
    },
    shareAppChange({ title, desc, link, imgUrl }) {
      const data = {
        event: 'showShareView',
        data: {
          platform: ['weChat', 'timeLine', 'QQ', 'QQZone', 'CopyLink'], //依次分别是微信、朋友圈、QQ好友、QQ空间、复制链接
          shareDic: {
            title,
            desc,
            link, // 页面地址
            imgUrl // 图片地址
          }
        }
      };
      this.nativeBridge.showShareView(data);
    }
  }
};
</script>
<style lang="less"></style>
