微信小程序分享链接及分享朋友圈,及mpvue无法指定分享名的bug修复方法

分享链接

参数携带:

<button open-type="share" hover-class="none">分享</button>

onShareAppMessage: function (res) {
    if (res.from === 'button') {      // 来自页面内转发按钮
      wx.showLoading();
      return new Promise((resolve, reject) => {
        wx.showLoading({
          title: '正在请求分享数据...',
          icon: 'none'
        })
        var vm = this
        //此处为发送请求,可以使用ajax或者axios代替
        request(`/getLink`, 请求参数, 'POST').then((res) => {
          wx.hideLoading();
          resolve({
            title: '职位详情',
            path: `/pages/detail/main?jobId=${vm.jobId}&linkContent=${res.linkContent}`, // 分享出去之后要进入的页面路径
          })
        })
      })
    } else {//来自页面外右上角转发
      console.log('不是button', res)
    }
  },

参数接收:

onLoad(options) {
    //获取分享的参数
    console.log('进入界面传入的参数:', options)
    wx.showToast({
      title: JSON.stringify(options),
      icon: 'none'
    })

    //将获取到的分享数据发送到后台(测试用)
    request('/getFxLinkParam', options).then((res) => {
      console.log(res)
    })

  },

测试结果:(后台接收到onLoad方法发送的请求及参数接收)

 分享朋友圈

先点亮分享链接及分享朋友圈的图标

代码如下:

onShow: function () {
    this._getData()
    // wx.startPullDownRefresh()

    wx.showShareMenu({
      withShareTicket: true,
      menus: ["shareAppMessage", "shareTimeline"]
    })
  },

参数携带:

//分享到朋友圈
  onShareTimeline: function (res) {
    var type = '测试';
    return {
      title: 'XXX招聘平台', // 可不填
      query: `id=${123}&type=${type}`, // 可不填 传递的参数,只能是这种格式
      imageUrl: '' // 可不填,可以是网络路径也可以是本地路径,分享到朋友圈显示的图标
    }
  },

参数接收:

onLoad(options) {
    //获取分享的参数
    console.log('进入界面传入的参数:', options)
    wx.showToast({
      title: JSON.stringify(options),
      icon: 'none'
    })

    //将获取到的分享数据发送到后台(测试用)
    request('/getFxPyqParam', options).then((res) => {
      console.log(res)
    })
  },

 另外:此处讲一个小插曲,一个mpvue的bug。

 分享朋友圈时无法使用参数,只能使用默认参数。即以下红框内容无效:

解决办法:(修改mpvue的源码)

1.  LIFECYCLE_HOOKS 的数组中添加元素 onShareTimeline

 

2.添加代码:

// 分享朋友圈
        onShareTimeline: rootVueVM.$options.onShareTimeline
          ? function (options) {
            return callHook$1(rootVueVM, 'onShareTimeline', options);
          } : null,

 添加位置如下:(搜索:onShareAppMessage)

3.项目重启即可。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值