小程序分享好友实现

页面内使用button触发分享

bug:组件内不支持onShareAppMessage方法

<button open-type='share'>分享</button>

js中方法

 onShareAppMessage: function (res) {
    console.log(res);
    if (res.from == 'button') {     //menu右上角转发
      return {
        title: '鼎立拍卖珠宝玉石字画',
        desc: res.target.dataset.name,
        path: '/pages/index/index?share_detail=1&goods_id=' + this.data.goods_id,  //点击分享打开首页,在首页判断分享路径,再跳转到详情页
        imageUrl: res.target.dataset.img,  //分享的图片路径
        success: (res) => {    // 成功后要做的事情
          console.log('哈哈哈哈哈哈');
        }
      }
    }
  },

点击分享卡片进入首页处理

 onLoad: function (options) {
    console.log(11111111111111111111111);
    console.log(options);

    // 如果是点击详情页分享卡片进来,跳转到详情页
    if (options.share_detail) {
      //判断是否授权过
      wx.getSetting({
        success: res => {
          if (res.authSetting['scope.userInfo']) {
            // 已经授权,可以直接调用 getUserInfo 获取头像昵称,不会弹框
            wx.getUserInfo({
              success: res => {
                app.globalData.userInfo = res.userInfo;
                if (app.userInfoReadyCallback) {
                  app.userInfoReadyCallback(res)
                }
              }
            })
            //已经授权但是没有登录
            if (!wx.getStorageSync('token')) {
              wx.reLaunch({
                url: '/pages/login/numberLogin/numberLogin',
              })
            } else {
              if (options.share_detail) {
                wx.navigateTo({
                  url: '/pages/goods_detail/goods_detail?goods_id=' + options.goods_id
                })
              }
            }
          } else {
            //没有授权
            wx.switchTab({ url: '/pages/my/my' })
          }
        }
      })
    }
  },

组件内分享

// 组件内,该页面组件内有多个分享,可以定义data-id 区分,也可以使用data传参数

				<!-- 分享 -->
                 <button open-type="share" data-img="{{item.imagess[0]}}" data-dec="{{item.content}}">
                    <image class="share" src="{{imgurl}}c-fenxiang.png" mode='widthFix' />
                </button>
                
				 button {
				           background-color: transparent;
				           margin-left: -20rpx;
				       }

// 页面中直接定义分享的方法, res.target.dataset.id 接收参数

// 圈子关注——分享
  /**
     * 用户点击右上角分享
     */
  onShareAppMessage: function (res) {
    console.log(res);
    if (res.from == 'button') {     //menu右上角转发
      return {
        title: '鼎立拍卖珠宝玉石字画',
        desc: res.target.dataset.dec,
        path: '/page/circle/circle',  //当前页面路径
        imageUrl: res.target.dataset.img,  //分享的图片路径
        success: (res) => {    // 成功后要做的事情
          console.log('哈哈哈哈哈哈');

        }
      }
    }
  },
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小曲曲

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值