页面内发起转发
通过给 button 组件设置属性 open-type=“share”,可以在用户点击按钮后触发Page.onShareAppMessage
<button data-name="shareBtn" open-type="share">转发</button>
注意:
1、必须是button组件,其他组件中设置 open-type=“share” 无效
2、button 自带有样式,通过plain="true"来控制按钮是否镂空,背景色透明,再通过控制样式 button[plain]{ border:0 } ,就可以比较随便的自定义样式了。
JS
//定义了 onShareAppMessage 函数时,页面可以表示改页面可以转发。
//只有定义了该函数,小程序右上角的菜单中才会有转发按钮
onShareAppMessage() {
return {
title: '自定义转发标题', // 默认是小程序的名称
// 跳转连接可拼接参数,用户点击分享内容时,参数可以在 onload(options) 的options的拿到
// 默认是当前页面,必须是以‘/'开头的完整路径
path: '/page/user?id=123',
imageUrl: '', //自定义图片路径,可以是本地文件路径、代码包文件路径或者网络图片路径,支持PNG及JPG,不传入 imageUrl 则使用默认截图。显示图片长宽比是 5:4
}
}
监听用户点击页面内转发按钮(button 组件 open-type=“share”)或右上角菜单“转发”按钮的行为
onShareAppMessage: function (options) {
if (options.from == 'button') {
} else {
}
}
参考:
开放能力 /转发 /转发
转发 /wx.updateShareMenu
分享至朋友圈
小程序页面默认不可被分享到朋友圈,开发者需主动设置“分享到朋友圈”。页面允许被分享到朋友圈,需满足以下条件:
页面需设置onShareAppMessage允许分享,页面需设置onShareTimeline允许“分享到朋友圈”,同时可自定义标题、分享图等。
从基础库 2.11.3 开始支持
此功能为beta版,暂仅在Android平台支持
onShareTimeline(res){
console.log(res)
return {
title: '测试小程序分享至朋友圈',
path: '../test/test',
imageUrl:'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1594374964481&di=3ceba827e91e126012c43de3887a58c7&imgtype=0&src=http%3A%2F%2Fdmimg.5054399.com%2Fallimg%2Fpkm%2Fpk%2F13.jpg'
}
},
onShareAppMessage: function(ops) {
return {
title: "分享卡片",
path: '../test/test',
}
}
参考:
开放能力 /分享到朋友圈Beta
隐藏当前页面的转发按钮
wx.hideShareMenu({
menus: ['shareAppMessage', 'shareTimeline']
})