小程序: 通过页面中的按钮转发、分享

1. wxml -- 添加以下代码后即可直接触发

<button class="btn" type="default" plain open-type='share'>
    点击进行分享
</button>

 

2. 如果想对分享的内容进行配置,如下所示

 onShareAppMessage(options) {
// 设置转发内容 -- 适用于: 页面右上角 ... 和 页面按钮 var shareObj = { title: "转发的标题", path: '别人查看分享的内容后 进入的页面路径', // 默认是当前页面; 自定义: 必须是以'/'开头的完整路径 // 注意点: uni-app中 -- 使用的是 imageUrl
       imgUrl: '图片url', //转发时显示的图片路径,支持网络和本地,不传则使用当前页默认截图。 success: function(res) { // 转发成功之后的回调 if (res.errMsg == 'shareAppMessage:ok') { console.log('---转发成功---'); } }, fail: function() { // 转发失败之后的回调 if (res.errMsg == 'shareAppMessage:fail cancel') { // 用户取消转发 console.log('---用户取消转发---'); } else if (res.errMsg == 'shareAppMessage:fail') { // 转发失败,其中 detail message 为详细失败信息 console.log('---转发失败---'); } }, complete: function() { // 转发结束之后的回调(转发成不成功都会执行) console.log('---转发完成---'); } };


     // 页面按钮的分享: 可在里面进行相应的转发配置 if (options.from == 'button') { console.log('---页面分享按钮---'); // 修改图片和路径
       shareObj.title = '标题';
shareObj.path = '页面路径';
       shareObj.imgUrl = '图片';       
        } 

return shareObj;
}

 

3.如果只有页面按钮的分享(不使用右上角  三点...的分享),可使用下面的代码

 onShareAppMessage(options) {

       // 页面按钮的分享
        if (options.from == 'button') {
            console.log('---页面分享按钮---');
       
           return  {
              title: "转发的标题",
              path: '别人查看分享的内容后 进入的页面路径', // 默认是当前页面; 自定义: 必须是以'/'开头的完整路径
              imgUrl: '图片url', //转发时显示的图片路径,支持网络和本地,不传则使用当前页默认截图。
              success: function(res) {
                 // 转发成功之后的回调
                 if (res.errMsg == 'shareAppMessage:ok') {
                    console.log('---转发成功---');
                 }
              },
              fail: function() {
                 // 转发失败之后的回调
                 if (res.errMsg == 'shareAppMessage:fail cancel') {
                    // 用户取消转发
                    console.log('---用户取消转发---');
                 } else if (res.errMsg == 'shareAppMessage:fail') {
                    // 转发失败,其中 detail message 为详细失败信息
                    console.log('---转发失败---');
                 }
              },
              complete: function() {
                 // 转发结束之后的回调(转发成不成功都会执行)
                 console.log('---转发完成---');
              }
          }; 
      
      } 
 }

 

posted @ 2020-08-26 15:44  点点点旺旺旺  阅读(1169)  评论(0编辑  收藏  举报