WeChat Mini Program "Share, Forward" Event

WeChat Mini Program "Share, Forward" function
1. Custom sharing in the page
2. "..." sharing behavior in the upper right corner of the page

When the event "onShareAppMessage" is not added to the page js, the "..." event in the upper right corner will not appear.
If there is an event, but the content of the event is not defined, the forwarded card is the screenshot information of the current page.

1) Use the "..." in the upper right corner of the default page to share events

Page({
    
    
  onShareAppMessage: function (res) {
    
    
    return {
    
    
      title: '这是默认转发',
      path: '/pages/index/index?id=123',
      imageUrl: '****.png'//这个是分享的图片
    }
  }
})

2) When there is a custom "Share" button on the page

    <!--index.wxml页面-->
    <button class="share_icon" open-type = "share">
        自定义分享按钮
    </button>
/*index.js*/
Page({
    
    
  onShareAppMessage: function (res) {
    
    
	let title,imageUrl;
    if (res.from === 'button') {
    
    
      // 来自页面内转发按钮
      title= ‘这个是页面自定义的分享事件~’;
      imageUrl='***.png';
    }
    if(res.from ==='menu'){
    
    
	  title= ‘这个是页面右上角的分享事件~’;
	  imageUrl='***.png';
    }
    return {
    
    
      title: title,
      imageUrl: imageUrl,//这个是分享的图片
      path: '/page/user?id=123'}
  }
})

3) When there are multiple custom share buttons on the page

    <button open-type='share' id="share1">这个第一个分享按钮</button>
    <button open-type='share' id="share2">这个第一个分享按钮</button>
/*index.js*/
Page({
    
    
  onShareAppMessage: function (res) {
    
    
    let title,imageUrl;
    console.log(res.target)if (res.from === 'button' && res.target.id == 'share1') {
    
    
      title= ‘这个是页面自定义分享按钮share1的分享事件~’;
      imageUrl='***.png';
    }
    if (res.from === 'button' && res.target.id == 'share2') {
    
    
      title= ‘这个是页面自定义分享按钮share2的分享事件~’;
      imageUrl='***.png';
    }
    if(res.from ==='menu'){
    
    
	  title= ‘这个是页面右上角的分享事件~’;
	  imageUrl='***.png';
    }
    return {
    
    
      title: title,
      imageUrl: imageUrl,//这个是分享的图片
      path: '/page/user?id=123'}
  }
})

Guess you like

Origin blog.csdn.net/shengmeshi/article/details/93612876