import wx from ‘weixin-jsapi’
先在vue全局封装,再Vue.use()
exports.install = function (Vue, options) {
Vue.prototype.shareList = function (imgUrl, link, desc, title,userId) {
let wechaturl = window.location.href.split('#')[0];
//获取微信配置信息
接口地址( {url: wechaturl}).then(function (res) {
wx.config({
debug: true, // true:调试时候弹窗
appId: res.data.appId, // 微信appid
timestamp: res.data.timestamp, // 时间戳
nonceStr: res.data.nonceStr, // 随机字符串
signature: res.data.signature, // 签名
jsApiList: [
// 所有要调用的 API 都要加到这个列表中
'onMenuShareTimeline', // 分享到朋友圈接口
'onMenuShareAppMessage', // 分享到朋友接口
'onMenuShareQQ', // 分享到QQ接口
'onMenuShareWeibo' // 分享到微博接口
]
})
wx.checkJsApi({
jsApiList: [
// 所有要调用的 API 都要加到这个列表中
'onMenuShareTimeline', // 分享到朋友圈接口
'onMenuShareAppMessage', // 分享到朋友接口
'onMenuShareQQ', // 分享到QQ接口
'onMenuShareWeibo', // 分享到微博接口
// 'updateAppMessageShareData',//(分享微信,QQ)
// 'updateTimelineShareData'//(微信朋友圈,QQ空间)
],
success: function (res) {
}
})
wx.ready(function () {
// 微信分享的数据
var shareData = {
imgUrl: imgUrl, // 分享显示的缩略图地址
link: newUrl, // 分享地址
desc: desc, // 分享描述
title: title, // 分享标题
success: function () {
// 分享成功可以做相应的数据处理
alert('分享成功')
alert('appId:' + res.appId)
alert('timestamp:' + res.timestamp)
alert('nonceStr:' + res.nonceStr)
alert('signature:' + res.signature)
},
fail: function () {
alert('调用失败')
},
complete: function () {
alert('调用结束')
}
}
wx.onMenuShareQQ(shareData)
wx.onMenuShareWeibo(shareData)
wx.onMenuShareTimeline(shareData)
wx.onMenuShareAppMessage(shareData)
})
wx.error(function (res) {
// config信息验证失败会执行error函数,如签名过期导致验证失败,
// 具体错误信息可以打开config的debug模式查看,也可以在返回的res参数中查看,
// 对于SPA可以在这里更新签名。
alert('分享失败')
})
}
}, function (res) {
alert(res)
})
}
}
- let wechaturl = window.location.href.split(’#’)[0];,获取本页面的域名地址,这个域名地址必须在微信公众号js安全域名下设置
- 即将下架的微信方法,和目前推荐的微信方法,不能冲突,例如updateAppMessageShareData’,onMenuShareAppMessage,这两个同时分享到朋友