1.安装weixin-js-sdk(目前使用的是1.6.0) 2.新建一个js文件,在main.js中引入 3.封装方法
1 wxShare.share = function (appId, timestamp, nonceStr, signature, options,callBack) { 2 console.log('9999', appId, timestamp, nonceStr, signature, options); 3 wx.config({ 4 // 微信签名 5 debug: false, 6 appId: appId, 7 timestamp: timestamp, 8 nonceStr: nonceStr, 9 signature: signature, 10 jsApiList: ["onMenuShareAppMessage","onMenuShareTimeline"], 11 // 下面的是新的微信分享接口,不支持回调 12 // jsApiList: ["updateAppMessageShareData","updateTimelineShareData"], 13 }); 14 // "onMenuShareAppMessage", "onMenuShareTimeline", "showAllNonBaseMenuItem" 15 16 wx.ready(function () { 17 console.log('readyreadyready'); 18 wx.showOptionMenu(); 19 // 2. 分享接口 20 // 2.1 监听“分享给朋友”,按钮点击、自定义分享内容及分享结果接口 21 // onMenuShareAppMessage 22 wx.onMenuShareAppMessage({ 23 title: options.title, //分享标题 24 desc: options.desc, // 分享描述 25 link: options.link, // 分享链接 26 imgUrl: options.imgUrl, // 分享图标 27 success: function (res) { 28 // 分享成功的回调;用户分享后记录用户分享动作 29 console.log("微信分享成功"); 30 }, 31 cancel: function (res) { 32 console.log("微信分享已取消"); 33 }, 34 fail: function (res) { 35 }, 36 }); 37 38 // //分享给朋友圈onMenuShareTimeline 39 wx.onMenuShareTimeline({ 40 title: options.title, 41 link: options.link, 42 imgUrl: options.imgUrl, 43 success: function (res) { 44 // 分享成功的回调;用户分享后记录用户分享动作 45 //alert('已分享'); 46 }, 47 cancel: function (res) { 48 //alert('已取消'); 49 }, 50 fail: function (res) { 51 }, 52 }); 53 }); 54 } 55// 隐藏分享方法 56 wxShare.notShare = function (appId, timestamp, nonceStr, signature) { 57 console.log('隐藏分享', appId, timestamp, nonceStr, signature); 58 wx.config({ 59 debug: false, 60 appId: appId, 61 timestamp: timestamp, 62 nonceStr: nonceStr, 63 signature: signature, 64 jsApiList: ["hideMenuItems"], 65 }); 66 wx.ready(function () { 67 wx.hideMenuItems({ 68 menuList: ['menuItem:share:appMessage', 'menuItem:share:timeline', 'menuItem:share:qq', 'menuItem:share:QZone', 'menuItem:openWithSafari', 'menuItem:openWithQQBrowser', 'menuItem:favorite'] 69 }) 70 }); 71 wx.error(function (res) { 72 console.log(res, "分享-错误信息"); 73 }); 74 }
4.在需要分享的页面调用 调用接口,拿到签名,appId,timestamp,nonceStr,signature; options:微信分享的内容(标题,描述,链接,图标)
1wxShare.share(this.wxParams.appId, this.wxParams.timestamp, this.wxParams.nonceStr, this.wxParams.signature, options,()=>{ 2// 回调 3});
