[Mini Program] WeChat Mini Program sharing function onShareAppMessage(options)

When the onShareAppMessage function is defined in the js file of the page, the page can indicate that the changed page can be forwarded. You can set the page forwarding information in the function.

  1. Only if this function is defined, there will be a forward button in the menu at the upper right corner of the applet

  2. This function is called back when the user clicks the forward button

  3. This function needs to return an Object, which contains the forwarded information (the content of forwarding can be customized)

There are two places on the page that can trigger the forwarding time:

One is the forward button in the upper-right menu    and the
  other is the button with the attribute open-type and the value of share on the page. (Note: it must be a button component, setting open-type="share" in other components is invalid)

<button data-name="shareBtn" open-type="share">转发</button>

Function called after the sharing event is triggered:

onShareAppMessage: function( options ){
    
    
  var that = this;
  // 设置菜单中的转发按钮触发转发事件时的转发内容
  var shareObj = {
    
    
    title: "转发的标题",    // 默认是小程序的名称(可以写slogan等)
    path: '/pages/share/share',    // 默认是当前页面,必须是以‘/'开头的完整路径
    imageUrl: '',   //自定义图片路径,可以是本地文件路径、代码包文件路径或者网络图片路径,支持PNG及JPG,不传入 imageUrl 则使用默认截图。显示图片长宽比是 5:4
    success: function(res){
    
    
      // 转发成功之后的回调
      if(res.errMsg == 'shareAppMessage:ok'){
    
    
      }
    },
    fail: function(){
    
    
      // 转发失败之后的回调
      if(res.errMsg == 'shareAppMessage:fail cancel'){
    
    
        // 用户取消转发
      }else if(res.errMsg == 'shareAppMessage:fail'){
    
    
        // 转发失败,其中 detail message 为详细失败信息
      }
    },
    complete: fucntion(){
    
    
      // 转发结束之后的回调(转发成不成功都会执行)
    }
  };
  // 来自页面内的按钮的转发
  if( options.from == 'button' ){
    
    
    var eData = options.target.dataset;
    console.log( eData.name );   // shareBtn
    // 此处可以修改 shareObj 中的内容
    shareObj.path = '/pages/btnname/btnname?btn_name='+eData.name;
  }
  // 返回shareObj
  return shareObj;
}

Guess you like

Origin blog.csdn.net/uk_51/article/details/114211555