Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

WeChat Apple API Forwarding (Page.onShareAppMessage)


May 19, 2021 WeChat Mini Program Development Document


Table of contents


onShareAppMessage(options)


Define the onShareAppMessage function in Page to set the forwarding information for the page.

  • The Forward button appears in the upper right-hand menu only if this event handler is defined
  • The user calls when they click the forward button
  • This event requires return an Object to customize the forwarding content

Options parameter description

Parameters Type Description The lowest version
from String Forward the source of the event. button: in-page forward button; 1.2.4
target Object If the from value is button, target is the button that triggered the forwarding event, otherwise it is undefined 1.2.4
Custom forwarding fields

Field Description The default The lowest version
title Forward the title The current program name
path Forward the path The current page path must be the full path that begins with /
success Forward a successful callback function 1.1.0
fail Forward a failed callback function 1.1.0
complete Forward The end callback function (Forward succeeds and fails are executed.) 1.1.0

Callback results:

The type of callback errMsg Description
success shareAppMessage:ok The forwarding was successful
fail shareAppMessage:fail cancel The user cancels forwarding
fail shareAppMessage:fail (detail message) The forwarding failed, where the detail message is detailed failure information

Description of the success callback parameters:

Parameters Type Description Minimum version
shareTickets StringArray Shareticket array, each is a shareticket, corresponding to a forwarding object 1.1.0

Sample code:

Page({
  onShareAppMessage: function (res) {
    if (res.from === 'button') {
      // 来自页面内转发按钮
      console.log(res.target)
    }
    return {
      title: '自定义转发标题',
      path: '/page/user?id=123',
      success: function(res) {
        // 转发成功
      },
      fail: function(res) {
        // 转发失败
      }
    }
  }
})

wx.showShareMenu(OBJECT)

Base library version 1.1.0 starts to support, and low versions need to be compatible

The forward button that displays the current page

OBJECT parameter description:

Parameters Type Required Description
withShareTicket Boolean Whether Whether to use forwarding with shareTicket Details
success Function Whether The interface calls a successful callback function
fail Function Whether The interface calls the failed callback function
complete Function Whether Callback function at end of interface call (call succeeds, fails are executed)

Example code:

wx.showShareMenu({
  withShareTicket: true
})

wx.hideShareMenu(OBJECT)

Base library version 1.1.0 starts to support, and low versions need to be compatible

Hide the forward button

OBJECT parameter description:

Parameters Type Required Description
success Function Whether The interface calls a successful callback function
fail Function Whether The interface calls the failed callback function
complete Function Whether Callback function at end of interface call (call succeeds, fails are executed)

Example code:

wx.hideShareMenu()

wx.updateShareMenu(OBJECT)

Base library 1.2.0 starts to support, and low versions need to be compatible

Update the forwarding property

OBJECT parameter description:

Parameters Type Required Description
withShareTicket Boolean Whether Whether to use forwarding with shareTicket Details
success Function Whether The interface calls a successful callback function
fail Function Whether The interface calls the failed callback function
complete Function Whether Callback function at end of interface call (call succeeds, fails are executed)

Example code:

wx.updateShareMenu({
  withShareTicket: true,
  success() {
  }
})

wx.getShareInfo(OBJECT)

Base library 1.1.0 starts to support, and low versions need to be compatible

Get forwarding details

OBJECT parameter description:

Parameters Type Required Description
shareTicket String Is shareTicket
success Function Whether The interface calls a successful callback function
fail Function Whether The interface calls the failed callback function
complete Function Whether Callback function at end of interface call (call succeeds, fails are executed)

CALLBACK Parameter Description:

Parameters Type Description
errMsg String The error message
encryptedData String Encrypted data for complete forwarding information, including sensitive data, can be found in detail in the encrypted data decryption algorithm
String The initial vector of the encryption algorithm can be found in the encryption data decryption algorithm

EncryptedData is decrypted as a JSON structure with fields as follows:

Field Description
openGId The group's unique ID for the current applet

Tip: If you need to show group names, you can use the Open Data component

Get more forwarding information

Often developers want to be able to get some information, such as the identity of a group, when a small program is opened twice. N ow by wx.showShareMenu and withShareTicket to true when the user forwards the small program to any group chat, they can get shareTicket for the forwarding, which can be obtained by app.onLaunch() or App.onShow when the forwarding card is opened by another user in shareTicket shareTicket obtained in both steps can obtain the same forwarding information through the wx.getShareInfo() interface.

Forwarding is initiated within the page

Base library 1.2.0 starts to support, and low versions need to be compatible

By setting button open-type="share" to the button component, you can trigger Page.onShareAppMessage() and if the current page does not define the event, it has no effect after clicking. Related component: button

Use guidelines

Forward buttons designed to help users share content and services with friends more smoothly. F orwarding should be user-spontaneous and within reach when needed. Developers will have a better user experience if they follow the guidelines below when using them.

  1. Clear meaning: Clear, clear graphical buttons will reduce the time for the user to understand. I n our resource download center, you can find such button footage and use it directly. O r you can flexibly design button styles that have a clear meaning, depending on the design style of your business. Of course, you can also use the text button directly, "forward to friends", it is also clear enough.
  2. Easy to click: button click hot area should not be too small, should not be too large. At the same time, the forward button is the same as other buttons, the hot zone should not be too dense, so as to avoid the user misoperation.
  3. Appears on demand: Not all pages are suitable for placing forward buttons, non-public content that involves user privacy, or scenarios that might disrupt the user's current operational experience, which is not recommended. At the same time, because during the forwarding process, we will take the user screen image as a drawing, therefore, we need to pay attention to help users block personal information.
  4. Respect the will: Of course, not all users like to share your little program with friends. T herefore, it should not become an induced or coercive behavior, such as forwarding before a feature can be unlocked. Please note that this practice is not only not recommended, but may also violate our Code of Business practices, and we strongly recommend that you read this section before using it.

Above, we have displayed the most important points, if you have time, you can fully browse the Design Guide, I believe there will be more gains.

Bug & Tip

  1. tip Forwarding a picture is not customizable;
  2. tip : Debugging support for forwarding Check out normal forwarding debugging support and forwarding with shareTicket
  3. tip shareTickets return value can only be obtained by forwarding to group chats, and there is shareTickets
  4. tip : shareTicket valid for the current gadget lifecycle
  5. tip Due to policy changes, the small program group-related capabilities are adjusted so that developers can first use the group ID in the wx.getShareInfo interface for functional development.