小程序-转发功能,使用button开放功能open-type="share

标签: 微信小程序

 
 

 页面内发起转发

通过给 button 组件设置属性 open-type="share",可以在用户点击按钮后触发 Page.onShareAppMessage 事件,相关组件:button

界面
<button open-type="share">转发</button>

script

// 3 button 的开放功能<button open-type="share">分享</button>
onShareAppMessage: function(res) {
  var that = this;
  //console.log('res=====',res);
  if (res.from === 'button') {
    //console.log('来自页面内转发按钮');
  } else if (res.from === 'menu'){
    //console.log('右上角菜单转发按钮');
  }
  // 返回数据
  return {
    title: that.data.info.name,
    path: '/pages/food/info?id=' + that.data.info.id,
    success: function(res) {
      // 转发成功,可以把当前页面的链接发送给后端,用于记录当前页面被转发了多少次或其他业务
      wx.request({
        url: app.buildUrl("/member/share"),
        data: {
          url: utils.getCurrentPageUrlWithArgs()
        },
        success: function(res) {
          //console.log('成功');
        }
      });
    },
    fail: function(res) {
      // 转发失败
    }
  }
},

 


 

posted @ 2022-09-03 10:29  码哥之旅  阅读(1099)  评论(0编辑  收藏  举报