1.安装weixin-js-sdk(目前使用的是1.6.0) 2.新建一个js文件,在main.js中引入** **3.封装方法****
wxShare.share = function (appId, timestamp, nonceStr, signature, options,callBack) {
console.log('9999', appId, timestamp, nonceStr, signature, options);
wx.config({
// 微信签名
debug: false,
appId: appId,
timestamp: timestamp,
nonceStr: nonceStr,
signature: signature,
jsApiList: ["onMenuShareAppMessage","onMenuShareTimeline"],
// 下面的是新的微信分享接口,不支持回调
// jsApiList: ["updateAppMessageShareData","updateTimelineShareData"],
});
// "onMenuShareAppMessage", "onMenuShareTimeline", "showAllNonBaseMenuItem"
wx.ready(function () {
console.log('readyreadyready');
wx.showOptionMenu();
// 2. 分享接口
// 2.1 监听“分享给朋友”,按钮点击、自定义分享内容及分享结果接口
// onMenuShareAppMessage
wx.onMenuShareAppMessage({
title: options.title, //分享标题
desc: options.desc, // 分享描述
link: options.link, // 分享链接
imgUrl: options.imgUrl, // 分享图标
success: function (res) {
// 分享成功的回调;用户分享后记录用户分享动作
console.log("微信分享成功");
},
cancel: function (res) {
console.log("微信分享已取消");
},
fail: function (res) {
},
});
// //分享给朋友圈onMenuShareTimeline
wx.onMenuShareTimeline({
title: options.title,
link: options.link,
imgUrl: options.imgUrl,
success: function (res) {
// 分享成功的回调;用户分享后记录用户分享动作
//alert('已分享');
},
cancel: function (res) {
//alert('已取消');
},
fail: function (res) {
},
});
});
}
// 隐藏分享方法
wxShare.notShare = function (appId, timestamp, nonceStr, signature) {
console.log('隐藏分享', appId, timestamp, nonceStr, signature);
wx.config({
debug: false,
appId: appId,
timestamp: timestamp,
nonceStr: nonceStr,
signature: signature,
jsApiList: ["hideMenuItems"],
});
wx.ready(function () {
wx.hideMenuItems({
menuList: ['menuItem:share:appMessage', 'menuItem:share:timeline', 'menuItem:share:qq', 'menuItem:share:QZone', 'menuItem:openWithSafari', 'menuItem:openWithQQBrowser', 'menuItem:favorite']
})
});
wx.error(function (res) {
console.log(res, "分享-错误信息");
});
}
4.在需要分享的页面调用**** 调用接口,拿到签名,appId,timestamp,nonceStr,signature; options:微信分享的内容(标题,描述,链接,图标)
wxShare.share(this.wxParams.appId, this.wxParams.timestamp, this.wxParams.nonceStr, this.wxParams.signature, options,()=>{
// 回调
});