小程序开放平台

文档中心
起步
目录
配置小程序
小程序框架
小程序运行时
自定义组件
基础能力
开放能力
授权
小程序登录
开放数据校验与解密
获取手机号
客服消息
分享
转发
关注
专业号主页自定义商品排序
场景值
获取 POI 来源
性能优化
转发
事件回调
基础库
调试
数据分析
服务端调试

转发

开发
>
指南
>
开放能力
>
转发
>
更新时间:2024-11-28 14:54:17

页面内唤起分享

通过给

button
组件设置属性
open-type="share"
,可以在用户点击按钮后触发 Page.onShareAppMessage 事件,相关组件:button

分享的类型

开发者可以基于 button 组件配置 data-share-type 来唤起不同的分享入口,不同分享入口的对应关系如下:

分享类型
data-share-type
最低版本
小红书站内分享normal3.28.0
朋友圈(微信)分享shareTimeline3.28.0
微信群组分享shareChat3.28.0

代码示例:

带 shareTicket 的分享

客户端 > 8.57, 基础库 > 3.108.x 以后的版本,分享出去的链接将包含 shareTicket 信息,开发者可以通过 xhs.getShareInfo 获取到分享者信息:

代码示例:

  App({ 
     onLaunch(opts, data) {
       console.log('App:onLaunch========', opts.shareTicket);
     }
  })
  
  Page({
  getShareInfo() {
    if (!this.data.shareTicket) {
      xhs.showToast({ title: '请先获取 shareTicket' });
      return;
    }

    let _this = this;

    xhs.getShareInfo({ 
      shareTicket: _this.data.shareTicket,
      success(res) {
        console.log('getShareInfo success', res);
        _this.setData({ shareInfo: '获取 shareInfo 信息成功:' + JSON.stringify(res) });
      },
      fail(err) {
        console.log('getShareInfo fail', err);
        _this.setData({ shareInfo: '获取 shareInfo 信息失败:' + JSON.stringify(err) });
      }
    });
  }
  })

tips

  • data-share-type 默认是 normal (站内分享)