shareIOU.vue 2.28 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}) {
    let [{data}, ticket] = await Promise.all([
      app.$axios.get(`/iou/shareInfo?id=${query.id}&userId=${store.state.user.id}`),
      app.$axios.get(`/wechatTicket?id=${query.id}&userId=${store.state.user.id}`)
    ])      
    if (data.success && ticket.data.success) {
      return {img: data.img, url: data.url, ticket: ticket.data.config}
    } else {
      store.dispatch('displayMessage', (data.msg || ticket.msg))
      redirect(301, '/')
    }
  },
  data: () => ({
    info:{},
  }),
  computed: {
    userInfo() {
      return this.$store.state.user
    }
  },
  mounted() {
    wx.config({
      debug: true, 
      appId: this.ticket.appId, // 必填,公众号的唯一标识
      timestamp: this.ticket.timestamp, // 必填,生成签名的时间戳
      nonceStr: this.ticket.nonceStr, // 必填,生成签名的随机串
      signature: this.ticket.signature,// 必填,签名
      jsApiList: ['onMenuShareTimeline', 'onMenuShareAppMessage'] // 必填,需要使用的JS接口列表
    })
    const shareConfig = {
      title: `${this.userInfo.name}邀请您确认借条`, // 分享标题
      link: this.url, // 分享链接,该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
      imgUrl: this.userInfo.headimgurl, // 分享图标
      success: function () {},
      cancel: function () {}
    }
    wx.ready(() => {
      wx.onMenuShareTimeline(shareConfig)
      wx.onMenuShareAppMessage(shareConfig)
    })
    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>