The WeChat applet implements the forwarding and sharing function through buttons. Judge whether a friend clicked in or clicked in by yourself

Renderings show:

Code display:

share.wxml

        

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

 share.js

Page({
  /**
   * 页面的初始数据
   */
  data: {
 
  },
 
  /**
   * 用户点击右上角 和 button  实现分享
   */
  onShareAppMessage: function (res) {
    if(res.from === 'button'){
      console.log('触发页面内的分享按钮',res.target);
    }else if(res.from === 'menu'){
      console.log('触发右上角的分享按钮')
    }
    return{
      title:'你好,我是标题',    // 转发标题
      path: '',     // 当前页面 path ,必须是以 / 开头的完整路径
      imageUrl:'../../images/lemon.png'   
    }
  }
})

Judging whether a friend clicked in or clicked in by yourself:

onShareAppMessage: function(res) {
    if (res.from === 'button') {
      console.log("来自页面内转发按钮");
      console.log(res.target);
      return {
        title: this.data.shareTitle,
        imageUrl: this.data.shareImage, // 图片 URL
        path: '/pages/my_dial/my_dial_new?rootOpenId=' + wx.getStorageSync('openId') + '&activityId=' + activityId
      }
    } else {
      console.log("来自右上角转发菜单")
      console.log(res);
      return {
        title: this.data.shareTitle,
        imageUrl: this.data.shareImage, // 图片 URL
        path: '/pages/my_dial/my_dial_new?activityId=' + activityId
      }
    }
},

Guess you like

Origin blog.csdn.net/aaa123aaasqw/article/details/130683630