小程序分享

/**
   * 用户点击右上角分享
   */
  onShareAppMessage: function(options) {
    let image = this.data.shareInfo.imagePath ? this.data.shareInfo.imagePath : '/assets/images/headPhoto.png' // 动态图片地址
    // 分享配置
    let shareObj = {
      title: "分享标题",
      path: '/pages/invite/invite?firstUserName=' + app.globalData.user.username + '&inviteName=' + this.data.user.terminalName, // 分享地址
      imageUrl: image, // 分享图片
      success: function(res) {
        if (res.errMsg == 'shareAppMessage:ok') {
          console.log('ok')
        }
      },
      fail: function() {
        if (res.errMsg == 'shareAppMessage:fail cancel') {
          console.log('cancel')
        } else if (res.errMsg == 'shareAppMessage:fail') {
          console.log('fail')
        }
      },
      complete: function() {}
    }
    return shareObj
  }
分享按钮

<button class="share-btn" data-id="shareBtn" open-type="share">
      <text class="title">分享邀请</text>
</button>
/**
* 用户点击右上角分享
*/
onShareAppMessage: function(options) {
let image = this.data.shareInfo.imagePath ? this.data.shareInfo.imagePath : '/assets/images/headPhoto.png'
console.log(image, app.globalData.user.username)
let shareObj = {
title: this.data.shareInfo.name,
path: '/pages/invite/invite?firstUserName=' + app.globalData.user.username + '&inviteName=' + this.data.user.terminalName,
imageUrl: image,
success: function(res) {
if (res.errMsg == 'shareAppMessage:ok') {
console.log('ok')
}
},
fail: function() {
if (res.errMsg == 'shareAppMessage:fail cancel') {
console.log('cancel')
} else if (res.errMsg == 'shareAppMessage:fail') {
console.log('fail')
}
},
complete: function() {}
}
if (options.from == 'button') {
//var dataid = options.target.dataset; //上方data-id=shareBtn设置的值
// 此处可以修改 shareObj 中的内容
//shareObj.path = '/pages/btnname/btnname?id=' + dataid.id;  
}
return shareObj
}
原文地址:https://www.cnblogs.com/lezuw/p/12020608.html