最近入职的公司主要做微信端的h5,所以在所难免要引用sdk。虽然官方文档写的还算清楚,但是还是有坑。

1.在index.html中 引入微信sdk

<script type="text/javascript" src="http://res.wx.qq.com/open/js/jweixin-1.4.0.js"></script>

2.在assets/js 下新建文件 wx.js

export default {  wxShowMenu: function (that,sign='') {    let url = window.location.href.split('#')[0]    that.$http.post('/xxx',  //请求你们公司后台的接口 获取相关的配置      that.$getSingQuery({        appKey: 'xxx',        url      }))      .then(res => {        var getMsg = res.data.data;        // console.log('微信配置----------')        // console.log(res.data)        wx.config({          debug: false,  //生产环境需要关闭debug模式   测试环境下可以设置为true  可以在开发者工具中查看问题          appId: getMsg.appid, //appId通过微信服务号后台查看          timestamp: getMsg.timestamp, //生成签名的时间戳          nonceStr: getMsg.noncestr, //生成签名的随机字符串          signature: getMsg.sign, //签名          jsApiList: [ //需要调用的JS接口列表            'updateAppMessageShareData', //自定义“分享给朋友”及“分享到QQ”按钮的分享内容(1.4.0) 新接口            'updateTimelineShareData', //自定义“分享到朋友圈”及“分享到QQ空间”按钮的分享内容(1.4.0) 老接口            'onMenuShareTimeline', //分享到朋友圈 老接口            'onMenuShareAppMessage',//分享给盆友 老接口            'getLocation'  //获取定位          ]        });        wx.error(function (res) {          // alert(JSON.stringify(res))          console.log(res)          // config信息验证失败会执行error函数,如签名过期导致验证失败,具体错误信息可以打开config的debug模式查看,也可以在返回的res参数中查看,对于SPA可以在这里更新签名。        });        wx.ready(function () {          if(sign=='location'){ //由于 获取定位往往是页面一加载 就提示获取地理位置 所以可以直接在写在 wx.ready            wx.getLocation({              type: 'wgs84', // 默认为wgs84的gps坐标,如果要返回直接给openLocation用的火星坐标,可传入'gcj02'              success: function (res) {                var latitude = res.latitude; // 纬度,浮点数,范围为90 ~ -90                var longitude = res.longitude; // 经度,浮点数,范围为180 ~ -180。                var speed = res.speed; // 速度,以米/每秒计                var accuracy = res.accuracy; // 位置精度                that.latitude=res.latitude;                that.longitude=res.longitude;                that.geocodeRegeo()//逆地理编码  调用你vue实例里的方法                do something...              }            });          }        });      })      .catch(error => {        alert(error)        console.log(error)      })  }}

3.在main.js 将WXConfig绑在vue原型上 这样哪个页面需要初始化 直接通过原型就可以拿到

import WXConfig from './assets/js/wx'  //微信分享Vue.prototype.WXConfig = WXConfig

4.在需要的页面 进行初始化

微信JS-SDK说明文档:同一个url仅需调用一次,对于变化url的SPA的web app可在每次url变化时进行调用。

所以 我们在你需要的页面 mounted 时, this.WXConfig.wxShowMenu(this);调用就可以。

我这里将this传入 只是为了能直接在wx.js 调用vue上的一些方法。比如axios

  mounted: function () {    this.WXConfig.wxShare(this);  },

通过按钮自定义触发

html<div class="fxbox bor_b"     @click="shareFriend">分享给朋友</div><div class="fxbox bor_b"     @click="shareFriendCircle">分享到朋友圈</div>js    shareFriendCircle () {      let that = this      wx.onMenuShareTimeline({        title: this.dataCode.title, // 分享标题        desc: this.dataCode.desc, //分享描述        link: this.dataCode.link,// 分享链接        imgUrl: this.dataCode.imgUrl, // 分享图标        success () {          console.log('分享给朋友圈 旧')        }      });    },        // 分享给朋友 旧    shareFriend () {      let that = this      wx.onMenuShareAppMessage({        title: this.dataCode.title, // 分享标题        desc: this.dataCode.desc, //分享描述        link: this.dataCode.link,// 分享链接        imgUrl: this.dataCode.imgUrl, // 分享图标        success () {          console.log('分享给朋友 旧')        }      });    },

5.新老接口的区别

新接口      'updateAppMessageShareData' //自定义“分享给朋友”及“分享到QQ”按钮的分享内容(1.4.0)     'updateTimelineShareData',//自定义“分享到朋友圈”及“分享到QQ空间”按钮的分享内容(1.4.0)老接口    'onMenuShareTimeline', //分享到朋友圈     'onMenuShareAppMessage',//分享给盆友 

注意

  • 新接口中的success 回调函数 指的是 你的那些title desc...自定义设置成功了的回调函数,而不是用户主动点击微信右上角的三个点,点击分享给朋友或者朋友圈,分享成功的回调函数。
  • 老接口 success 回调函数 是指 用户成功分享给朋友或者朋友圈的回调函数
  • 经测试 使用新接口 在ios下表现正常 ,在部分安卓机下失效

建议使用老接口 无此问题

6.补充

还有一点, link: this.dataCode.link,// 分享链接
该链接域名或路径必须与当前页面对应的公众号JS安全域名一致

分享链接请不要出现奇怪的字符,或者URL编码一下 比如:|,会导致 链接域名与js安全域名一致 但依然报安全域名的错误

7.扫一扫

往期文章

  • 数据结构与算法-LeetCode 格雷编码(No.89)
  • [数据结构与算法-LeetCode 种花问题(No.605)

](https://juejin.im/post/5cd6c5...

  • LeetCode-电话号码的字母组合(No.17) 递归+hash
  • JavaScript 数据结构与算法 这题你会吗?