小程序-公共分享。

在utils文件夹中的util.js文件中
const shareEvent= function(option, obj){
  let shareObj = {
    title: obj.title,
    path: obj.path + '?spread_spid=' + JSON.stringify(obj.scan),
    imageUrl: obj.imageUrl,
    success(res) { // 转发成功之后的回调
      if (res.errMsg == 'shareAppMessage:ok') { }
    },
    fail(res) { // 转发失败之后的回调
      if (res.errMsg == 'shareAppMessage:fail cancel') {//用户取消转发
      } else if (res.errMsg == 'shareAppMessage:fail') {// 转发失败,其中 detail message 为详细失败信息
      }
    },
    complete() {// 转发结束之后的回调(转发成不成功都会执行)
    }
  };
  if (option.from === 'button') {// 来自页面内转发按钮
    console.log(option.target)
  }
  return shareObj;
}

  

之后在需要分享的页面中操作
  onShareAppMessage: function (option) {
    let imageUrl = '/images/share.jpg';
    let sharePath = "/pages/index/index";
    let shareTitle = "快来进来吧~";
    let obj = {
      title: shareTitle,
      path: sharePath,
      imageUrl: imageUrl,
      scan:app.globalData.userInfo.uid
      
    };
    return util.shareEvent(option, obj);
  }

  

posted @ 2022-11-11 10:34  MiniDuck  阅读(17)  评论(0编辑  收藏  举报