话不多说 直接来讲述一下操作步骤:
首先 我们需要通过调用 wx.showShareMenu 并且设置 withShareTicket 为 true。这一项必须设置,这样当用户将小程序分享到任一群聊之后,才可以获取到此次分享的 shareTicket,千万不能忘了!
onLoad:
function (options) {
wx.showShareMenu({
// shareTicket 是获取转发目标群信息的票据,只有拥有 shareTicket 才能拿到群信息,用户每次转发都会生成对应唯一的shareTicket 。
withShareTicket:
true
});
},
接下来就是在onShareAppMessage 函数中的操作啦。
/**
* 用户分享
* shareBtn:是否按钮转发
* isshare:是否分享成功 isshare=1 成功 isshare=0 失败
*/
onShareAppMessage:
function (res) {
var that =
this;
console.log(res);
if (res.from ===
'button') {
// 来自页面内转发按钮
that.data.shareBtn =
true;
}
else {
//来自右上角转发
that.data.shareBtn =
false;
}
return {
title:
'自定义转发标题',
path:
'pages/index/index',
complete:
function (res) {
console.log(res);
if (res.errMsg ==
'shareAppMessage:ok') {
//分享为按钮转发
if (that.data.shareBtn) {
//判断是否分享到群
if (res.hasOwnProperty(
'shareTickets')) {
console.log(res.
shareTickets[0]);
//分享到群
that.data.isshare =
1;
}
else {
// 分享到个人
that.data.isshare =
0;
}
}
}
else {
wx.showToast({
title:
'分享失败',
})
that.data.isshare =
0;
}
},
}
}
判断是否为按钮转发,可在页面中做答题复活按钮。