vue 微信分享自定义

指定页面分享进入自己本页面,其他页面分享进入首页

App.vue

 created() {
    let source = parseQueryString(window.location.href).source || ''
    this.$store.dispatch('SetSource', source)
    let path = window.location.href 
    this.$store.dispatch('SetCurrentPath', path)
    let from = getQueryString('from')
    this.$store.dispatch('SetFrom', from)
},

router.js

router.beforeEach((to, from, next) => {
  if (to.name === '指定的页面名' || store.state.loaded) { //如果是指定的页面
    next()
   } else {    //否则分享是首页
     next('/')
   }
})

router.afterEach((to, from) => {
  if (store.state.wechatConfig === null) {  
     //请求后台接口获取分享的数据信息(把页面的当前路径传给后台)
     getWechatShare(store.state.from, store.state.currentPath).then(res => {
       let data = res.data
       console.log(data)
       store.dispatch('SetWechatConfig', { ...data, debug: false, jsApiList: ['onMenuShareTimeline', 'onMenuShareAppMessage'] })
       configWechat(to)
     })
  } else {
    configWechat(to)
  }
})

function configWechat(to) {
  let path = `${window.location.origin}${window.location.pathname}${window.location.search}#${to.path}`
  wx.config(store.state.wechatConfig)
  wx.ready(function () {
    console.log('wx.ready')
    // 分享到朋友圈
    wx.onMenuShareTimeline({
      title: '', // 分享标题
      link: path, // 分享链接,该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
      // link:store.state.wechatConfig.shareUrl,
      imgUrl: 'https://www.com/icon.jpg', // 分享图标
      success: function () {
        // 用户确认分享后执行的回调函数
      },
      fail: function (res) { }
    })
    //分享给朋友
    wx.onMenuShareAppMessage({
      title: '', // 分享标题
      desc: '描述。。。', // 分享描述
      // link:store.state.wechatConfig.shareUrl,
      link: path, // 分享链接,该链接域名或路径必须与当前页面对应的公众号JS安全域名一致location.href
      imgUrl: 'https://www.com/icon.jpg', // 分享图标/images/wxshare.jpg
      type: 'link', // 分享类型,music、video或link,不填默认为link
      success: function () {
        // 用户确认分享后执行的回调函数
        // alert("分享成功")
      },
      cancel: function () {
        // 用户取消分享后执行的回调函数
        // alert("分享失败")
      },
      fail: function (res) {
        // alert(JSON.stringify(res));
      }
    })
  })
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值