shareIOU.vue 2.62 KB
<template>
  <v-layout id="shareIOU" column justify-center align-center>
    <v-card id="mainBlock" flat>
      <div>分享本页面给微信好友或朋友圈</div>
      <div>或者</div>
      <div>让让对方扫一扫二维码</div>
      <div class="qr" v-html="img">

      </div>
    </v-card>
  </v-layout>
</template>

<script>
export default {
  async asyncData({store, query, app, redirect, route}) {
    console.log(route)
    let {data} = await app.$axios.get(`/iou/shareInfo?id=${query.id}&userId=${store.state.user.id}`)
    if (data.success) {
      return {img: data.img, url: data.url}
    } else {
      store.dispatch('displayMessage', data.msg)
      redirect(301, '/')
    }
  },
  data: () => ({
    info:{},
    ticket: {}
  }),
  computed: {
    userInfo() {
      return this.$store.state.user
    }
  },
  async mounted() {
    const u = navigator.userAgent
    console.log(u)
    const isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/);
    console.log(isiOS)
    let {data}  = await this.$axios.post(`/wechatTicket`, {
      userId: this.$store.state.user.id,
      href: isiOS ? this.$store.state.firstUrl : window.location.href
    })
    if (data.success) {
      window.wx.config({
        debug: true, 
        appId: data.config.appId, // 必填,公众号的唯一标识
        timestamp: data.config.timestamp, // 必填,生成签名的时间戳
        nonceStr: data.config.nonceStr, // 必填,生成签名的随机串
        signature: data.config.signature,// 必填,签名
        jsApiList: ['onMenuShareTimeline', 'onMenuShareAppMessage'] // 必填,需要使用的JS接口列表
      })
      const shareConfig = {
        title: `${this.userInfo.name}邀请您确认借条`, // 分享标题
        link: `https://www.51liuliang.cc/netiou/shareRedirect?id=${this.$route.query.id}`, // 分享链接,该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
        imgUrl: this.userInfo.headimgurl, // 分享图标
        success: function () {},
        cancel: function () {}
      }
      window.wx.ready(() => {
        window.wx.onMenuShareTimeline(shareConfig)
        window.wx.onMenuShareAppMessage(shareConfig)
      })
      window.wx.error(function(res){
        console.log(res)
      })
    }
  },
}
</script>

<style lang="scss" scoped>
  #shareIOU {
    #mainBlock {
      width: 100vw;
      background-color: transparent;
      text-align: center;
      font-size: 1.3rem;
      font-weight: bold;

      .qr {
        width: 60%;
        background-color: #fff;
        margin: 1rem 20%;

        svg {
          vertical-align: bottom;
        }
      }
    }
  }
</style>