微信小程序阻止分享按钮弹起

185 阅读1分钟

在点击微信小程序的分享按钮时,要先调用后端接口判断是否可以拉起分享,如果可以直接拉起,如果不可以就提示报错,代码如下


onLoad(() => {
    uni.hideShareMenu()
})
onShareAppMessage(async (res) => {
  // 来自页面内分享按钮
  if (res.from === 'button') {
    try {
      const ret = await getShareCode(couponId.value)
      console.log('ret', ret)
      if (ret.code == 0) {
        const path = `/pages/coupon/coupon-share?couponCode=${ret.data}`
        console.log('path', path)
        return {
          title: '领取优惠券',
          path
        }
      } else {
        toast('错误提示')
      }
    } catch (error) {
      console.error('获取分享码失败', error)
    }
  }
})