uniapp 分享接口

注意:仅支持分享微信小程序到微信聊天界面,想进入朋友圈需改为分享图片方式,在图片中包含小程序码。一般通过canvas绘制图片,插件市场有很多生成图片的插件。

小程序分享:

// 小程序分享
onShareAppMessage() {
  return {
    title: 'test',
    path: 'pages/index/index'
  }
}

只有定义了此事件处理函数,小程序右上角菜单才会显示“转发”按钮。

App分享:

首先要在 manfest.json中的 App模块权限配置 中,勾选上分享

然后在 AppSDK配置 中,设置分享模块,需要填写对应的信息。

添加按钮:

<!-- #ifdef APP-PLUS -->
<button type="primary" @click="share">分享</button>
<!-- #endif -->

方法:

// 分享好友
share() {
  // 分享图文到微信聊天界面
  uni.share({
    provider: "weixin", // 服务商
    scene: "WXSceneSession", // 场景 微信好友WXSceneSession  朋友圈WXSceneTimeLine
    type: 0, // 图文0 文字1 图片2
    href: "http://uniapp.dcloud.io/", // 分享h5地址
    title: "uni-app分享",
    summary: "我正在使用HBuilderX开发uni-app,赶紧跟我一起来体验!", // 描述
    imageUrl: "https://img-cdn-qiniu.dcloud.net.cn/uniapp/images/uni@2x.png",
    success: function (res) {
        console.log("success:" + JSON.stringify(res));
    },
    fail: function (err) {
        console.log("fail:" + JSON.stringify(err));
    }
  });
}

分享图文:href、imageUrl 为必选参数,title/summary 二选一,最好将这四个参数都选上。

更多分享,请参考文档:

https://uniapp.dcloud.io/api/plugins/share?id=share 

posted on 2020-05-19 16:38  JoeYoung  阅读(4100)  评论(1编辑  收藏  举报