vue 微信 js-sdk分享功能

无意间接触到的一个东西

第一步:

npm install weixin-js-sdk -S
或者直接index.html里面引入js文件 http://res.wx.qq.com/open/js/jweixin-1.6.0.js

官方文档

第二步:

import wx from 'weixin-js-sdk'

在main.js里面把wx挂载到Vue下面 Vue.prototype.$wx = wx
// 找后台获取签名,(拿签名的接口只需要向后台传递当前页面的url就行了,其他参数更具业务自定,如果后台找你要授权验证的code,就让他自己看文档去)
this.$store.dispatch('getAutographCode', {vm: this, devType: 'yanshi', url: window.location.href}).then(res => {
  if(res && res.data){
    this.$wx.config({
      debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
      appId: res.data.appId, // 必填,公众号的唯一标识
      timestamp: res.data.timestamp, // 必填,生成签名的时间戳
      nonceStr: res.data.noncestr, // 必填,生成签名的随机串
      signature: res.data.signature, // 必填,签名
      jsApiList: ['onMenuShareTimeline', 'onMenuShareAppMessage'] // 必填,需要使用的JS接口列表
    })
    this.$wx.ready(() => {      //需在用户可能点击分享按钮前就先调用
      console.log(this.$wx, 'readyreadyreadyreadyreadyreadyreadyready')
      this.$wx.onMenuShareTimeline({
        title: this.title, // 分享标题
        link: this.$serverPath, // 分享链接,该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
        imgUrl: this.cover , // 分享图标
        success: () => {
          // 设置成功
        }
      })
      this.$wx.onMenuShareAppMessage({
        title: this.title, // 分享标题
        link: this.$serverPath, // 分享链接,该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
        imgUrl: this.cover, // 分享图标
        success: () => {
          // 设置成功
        }
      })
    });
    this.$wx.error(() => {      //需在用户可能点击分享按钮前就先调用
      console.log(this.$wx, 'errorerrorerrorerrorerrorerrorerrorerror')
    });
  }
})

代码调取成功后,微信那边并不会弹出窗口让选择是分享朋友圈还是分享给好友,只是自定义了微信右上角三个点里面的分享功能

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值