最近入职的公司主要作微信端的h5,因此在所不免要引用sdk。虽然官方文档写的还算清楚,可是仍是有坑。javascript
<script type="text/javascript" src="http://res.wx.qq.com/open/js/jweixin-1.4.0.js"></script>
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) }) } }
import WXConfig from './assets/js/wx' //微信分享 Vue.prototype.WXConfig = WXConfig
微信JS-SDK说明文档:同一个url仅需调用一次,对于变化url的SPA的web app可在每次url变化时进行调用。html
因此 咱们在你须要的页面 mounted
时, this.WXConfig.wxShowMenu(this);
调用就能够。vue
我这里将this传入 只是为了能直接在wx.js 调用vue上的一些方法。好比axiosjava
mounted: function () { this.WXConfig.wxShare(this); },
经过按钮自定义触发ios
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('分享给朋友 旧') } }); },
新接口 'updateAppMessageShareData' //自定义“分享给朋友”及“分享到QQ”按钮的分享内容(1.4.0) 'updateTimelineShareData',//自定义“分享到朋友圈”及“分享到QQ空间”按钮的分享内容(1.4.0) 老接口 'onMenuShareTimeline', //分享到朋友圈 'onMenuShareAppMessage',//分享给盆友
注意git
success
回调函数 指的是 你的那些title desc...自定义设置成功了的回调函数,而不是用户主动点击微信右上角的三个点,点击分享给朋友或者朋友圈,分享成功的回调函数。 success
回调函数 是指 用户成功分享给朋友或者朋友圈的回调函数ios
下表现正常 ,在部分安卓机下失效了建议使用老接口 无此问题web
还有一点, link: this.dataCode.link,// 分享连接
该连接域名或路径必须与当前页面对应的公众号JS安全域名一致算法
分享连接请不要出现奇怪的字符,或者URL编码一下 好比:|,会致使 连接域名与js安全域名一致 但依然报安全域名的错误axios