如何在wepy2开发的小程序中开启朋友圈分享小程序功能
1、说明
分享朋友圈功能暂时只支持安卓,并且输入beta版本。一句话,并不是很成熟。
但是, 有时候,产品就是需要。
2、官网文档
https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/share-timeline.html
看文档: 当前页面,需要先开启发送朋友功能后,才能进一步分享到盆友圈功能。
对应的api文档如下:
3、实操
在我们的页面中,methods方法同级后面,设置分享方法:
wepy.page({
data: {},
methods:{},
// 朋友分享
onShareAppMessage(e) {
let userid = wx.getStorageSync('userid ') || ''
let url = '/pages/index?id=' + userid + '&aaa=1'
return {
title: '分享朋友标题文字',
path: url,
imageUrl: '/static/images/share.jpg'
}
},
// 分享朋友圈
onShareTimeline(e) {
return {
title: '分享到朋友圈标题',
query: '',
imageUrl: '/static/images/share.jpg'
}
},
onLoad(options) {
wx.showShareMenu({
withShareTicket: true,
menus: ['shareAppMessage', 'shareTimeline']
})
}
})
在onLoad 或 onReady 中,执行wx.showShareMenu
,朋友圈分享才真正可以用。
另外一种解决方式:
wepy.page({
data: {},
onReady() {},
},
{
lifecycle: {
page: () => {
return ['onShareTimeline']
}
}
})
在lifecycle
中添加这段代码,分享会生效,但是导致当前页面的按钮不能点击等。至于原因,未深入研究。
采用第一种方案。