小程序全局配置分享好友/朋友圈,可带参数

本文介绍了两种在微信小程序中配置全局分享的方法,一种是可能导致模拟器失效,而另一种则是亲测有效的解决方案,包括`onShareAppMessage`和`onShareTimeline`的定制,适合开发者优化分享体验。

小程序全局分享配置

方法一,本人验证不生效

App({
    
    onLaunch: function(options) {
        this.onShareAppMessage()
    },
    onShareAppMessage() {
    // 监听路由变化
    wx.onAppRoute((res) => {
      const page = getCurrentPage()
      if (page) {
        // 这里和下面 onShareAppMessage, onShareTimeLine 二选一
        wx.showShareMenu({
          withShareTicket: true,
          menus: ['shareAppMessage', 'shareTimeline'],
        })
        // ! 模拟器可能不生效,在真机查看
        page.onShareAppMessage = () => {
          {
            return {
              title: 'xxxxx!',
              path: `pages/index/index`,
              imageUrl: 'https://www.example.com/share.png',
            }
          }
        }
        page.onShareTimeline = () => {
          return {
            title: 'xxxxx!',
            query: `a=1&b=2`,
          }
        }
      }
    })
  },
})

方法二,亲测可用,推荐

// 方法二,此方法验证可行 date:2022-5-20

// app.js
!(function () {
  var PageOld = Page
  Page = function (options) {
    options = Object.assign(
      {
        getIdCustomer() {
          return getApp().globalData.idCustomer
        },
        onShareAppMessage() {
          const params = encodeURIComponent(`type=1&val=${this.getIdCustomer()}`)
          {
            return {
              title: '惺惺惜惺惺!',
              path: `/${this.route}?qp=${params}`,
              imageUrl: config.imagePrexDoamin + '/share.png',
            }
          }
        },
        onShareTimeline() {
          const params = encodeURIComponent(`type=3&val=${this.getIdCustomer()}`)
          return {
            title: '嘻嘻嘻嘻!',
            query: `qp=${params}`,
          }
        },
      },
      options
    )
    PageOld(options)
  }
})()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值