vue history模式下的微信分享

// 微信验证
export function requireConfig() {
  let url = window.location.href

  systemApi.wxoption({
    url: url
  }).then(res => {
    if (res.code === 200) {
      wx.config({
        debug: false,
        appId: res.data.appid, // 必填,企业号的唯一标识,此处填写企业号corpid
        timestamp: res.data.timestamp, // 必填,生成签名的时间戳
        nonceStr: res.data.nonceStr, // 必填,生成签名的随机串
        signature: res.data.signature, // 必填,签名,见附录1
        jsApiList: [
          'onMenuShareAppMessage',
          'onMenuShareTimeline'
        ]
      })
    }
  })
}

// 验证分享
export function requireShare(title, desc, link, imgUrl) {
  let u = navigator.userAgent
  // 安卓需要重新验证
  if (u.indexOf('Android') > -1) {
    requireConfig()
  }
  wx.ready(function() {
    // 分享给朋友
    wx.onMenuShareAppMessage({
      title: title, // 分享标题
      desc: desc, // 分享描述
      link: `https://www.baidu.com${link}`, // 分享链接
      imgUrl: imgUrl, // 分享图标
      success: function() {
        // 用户确认分享后执行的回调函数
      },
      cancel: function() {
        // 用户取消分享后执行的回调函数
      }
    })
    // 分享给朋友圈
    wx.onMenuShareTimeline({
      title: title, // 分享标题
      link: `https://www.baidu.com${link}`, // 分享链接
      imgUrl: imgUrl, // 分享图标
      success: function() {
        // 用户确认分享后执行的回调函数
      },
      cancel: function() {
        // 用户取消分享后执行的回调函数
      }
    })
  })
}
App.vue

// 微信分享
requireConfig()



其它需要使用分享的页面

// 分享
      requireShare(
        '学堂',
        '学堂',
        `/course/index`,
        'http://share.baidu.com/pic.png'
      )

猜你喜欢

转载自www.cnblogs.com/lanshengzhong/p/10308184.html
今日推荐