分享功能的函数:onShareAppMessage。这个函数通用,既可以右上角分享,也可以在页面内自定义button 分享。button的opentype =“share”.
实现参数回调的函数:
onShareAppMessage: function (res) {
console.log("用户点击了按钮开始分享", res)
var userNid = wx.getStorageSync("unionid");
var vid = wx.getStorageSync("v_vid");
var b_bid = wx.getStorageSync("ke_id");
if (res.from === 'button') {
// 来自页面内转发按钮
console.log(res.target)
}
var title = wx.getStorageSync('v_vname');
var path = '/pages/study/study?bid='+b_bid+"&isShare=1";
console.log(path)
return {
title: title,
path: path,
success: function (res) {
// 转发成功
wx.request({
url: app.globalData.Url + "/api/users/event/addshare/",
data: {
vid: vid,
unionid: userNid,
userAction: res
},
method: "POST",
header: {
'content-type': 'application/x-www-form-urlencoded' // 默认值
},
success: function (res) {
console.log('--转发结果', res)
}
})
var shareTickets = res.shareTickets,
shareTicket = shareTickets;
wx.getShareInfo({
shareTicket: shareTicket,
success: function (res) {
wx.showToast({
title: '转发成功',
duration: 5000
})
},
fail: function (res) {
wx.showToast({
title: 'fail:' + res.errMsg,
duration: 5000
})
}
});
wx.showShareMenu({
// 要求小程序返回分享目标信息
withShareTicket: true
});
},
fail: function (res) {
// 转发失败
console.log("用户点击了取消", res)
}
}
}
结果回调在success里面,可以请求服务器地址并发送分享的结果。
传递参数,通过缓存来获取存储的数据,以链接参数的形式传递,在返回的path页面可以打印结果。
打印结果在返回页面的onLoad函数中。
onLoad: function (options) {
console.log(options)
if(options.isShare == "1"){ //从分享页过来
wx.navigateTo({
url: '/pages/video/video?bid='+options.bid ,
})
}
}
并通过这种跳转方式来实现打开分享的也没按带有返回按钮,就是左上角的返回按钮