onShareAppMessage(options)

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

  • The menu at the top right corner will display the "Forward" button only if this event processing function is defined
  • Will be called when the user taps the "Forward" button
  • This event needs to return one Object to define the forwarded content

"options" parameter description

Parameter Type Description Minimum Version
from String Source of forwarded event. "button": The forward button within the page; "menu": The forward menu at the top right corner 1.2.4
target Object If the "from" value is "button", then "target" is the "button" that triggers this forwarding event, otherwise it is "undefined" 1.2.4

Custom forward field

Field Description Default Value Minimum Version
title Forwarding title Name of current Mini Program
path Forwarding path Current page path, which must be a full path starting with "/"
imageUrl Custom image path. It can be a local file path, code package file path, or network image file path. It supports PNG and JPG. If "imageUrl" is not imported, the default screenshot will be used. Aspect ratio of displayed image is 5:4 1.5.0

Sample code:

Page({
  onShareAppMessage: function (res) {
    if (res.from === 'button') {
      // From the forward button within the page
      console.log(res.target)
    }
    return {
      title: "Custom forward title"
      path: '/page/user?id=123'
    }
  }
})

wx.showShareMenu(OBJECT)

Start from base library version 1.1.0. Please remaining backward compatible.

Display the forward button of the current page

OBJECT parameter description:

Parameter Type Required Description
withShareTicket Boolean No Whether to use forwarding that carries "shareTicket"Details
success Function No Callback function when interface call succeeded
fail Function No Callback function when interface call failed
complete Function No Callback function when interface call completed (always executed whether call succeeds or fails)

Sample code:

wx.showShareMenu({
  withShareTicket: true
})

wx.hideShareMenu(OBJECT)

Start from base library version 1.1.0. Please remaining backward compatible.

Hide the forward button

OBJECT parameter description:

Parameter Type Required Description
success Function No Callback function when interface call succeeded
fail Function No Callback function when interface call failed
complete Function No Callback function when interface call completed (always executed whether call succeeds or fails)

Sample code:

wx.hideShareMenu()

wx.updateShareMenu(OBJECT)

Start from base library version 1.2.0. Please remaining backward compatible.

Update forwarding attributes

OBJECT parameter description:

Parameter Type Required Description
withShareTicket Boolean No Whether to use forwarding that carries "shareTicket"Details
success Function No Callback function when interface call succeeded
fail Function No Callback function when interface call failed
complete Function No Callback function when interface call completed (always executed whether call succeeds or fails)

Sample code:

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

wx.getShareInfo(OBJECT)

Start from base library version 1.1.0. Please remaining backward compatible.

Retrieve forwarding details

OBJECT parameter description:

Parameter Type Required Description Minimum Version
shareTicket String Yes shareTicket
timeout Number No Timeout time (ms) 1.9.90
success Function No Callback function when interface call succeeded
fail Function No Callback function when interface call failed
complete Function No Callback function when interface call completed (always executed whether call succeeds or fails)

CALLBACK parameter description:

Parameter Type Description
errMsg String Error message
encryptedData String Encrypted data of the entire forwarded message that includes sensitive data. For details please see Decryption algorithm for encrypted data
iv String Initial vector of encryption algorithm. For details see the Decryption Algorithm for Encrypted Data

"encryptedData" will be a JSON structure after decryption, including the following fields:

Field Description
openGId Unique ID of group to current Mini Program

Tip: If group name has to be displayed, open data component can be used

Obtain more forwarded messages

Usually, when the Mini Program forwarded is opened the second time, the developer wants to obtain some information, e.g. tags of the group. Now, by calling wx.showShareMenu and setting withShareTicket as true , when the user forwards the Mini Program to any group chat and this forwarded card is opened by other users in this group chat, a shareTicket can be obtained at App.onLaunch() or App.onShow. By calling "wx.getShareInfo()" interface, importing this shareTicket can retrieve forwarded messages.

Initiate forwarding within the page

Start from base library version 1.2.0. Please remaining backward compatible.

By setting attribute open-type="share" for component button, Page.onShareAppMessage() event can be triggered after the user taps the button. If the current page did not define this event, tapping will have no effect. Related component: button

User Guide

The forward button helps the user share content and services with friends more easily. Forwarding is an action of the user's own accord and is available at the user's fingertips. The developer will achieve a better user experience if the following guide is adhered to.

  1. Concise meaning: Button image that can be clearly understood immediately. This reduces the time the user requires to understand its usage. You can find such button materials in our resource download center and directly use them, or you can design your own button sample that conveys meaning clearly according to the style of your own design. Of course, you can also directly use the ready-made button "Forward to friends", which is also concise enough.
  2. Convenient tapping: The tap area of the button should not be too small or too big. At the same time, the forward button is just like other buttons, their tap areas should not be too close together to prevent user incorrect operation.
  3. Appearing when needed: The forward button is not suitable for every page. This feature is not recommended for non-open content that involves user privacy, or scenarios where the user's ongoing operation may be interrupted. At the same time, as we will use the user's screen image as the accompanying image in the forwarding process, censoring of the user's personal information have to be taken into consideration.
  4. Respecting what the user wants: Of course, not every user would like to share your Mini Program with friends. Therefore, this should not be an induced or compulsory action, e.g. only allowing certain features to be unlocked after forwarding, etc. Take note that such practices are not recommended and may be in violation of our Operation Rules. We strongly suggest that you read this content before using.

We have explained the most important points above. If you have time, you can browse the complete Design Guide where there is much more information for you.

Bug & Tip

  1. tip: When forwarding of images is not defined, the current page will be taken by default starting from the top. The forwarded image will have a height 80% of the width of the screen.
  2. tip: For forwarding debugging support, please view Debugging Support for General Forwarding and Forwarding that contains "shareTicket"
  3. tip: shareTickets return value can only be retrieved by forwarding to group chats. Single chats doesn't have shareTickets
  4. tip: shareTicket is only effective during the current Mini Program's life cycle
  5. tip: Due to policy changes, capabilities of the Mini Programs are being adjusted. The developer can use the group ID of the wx.getShareInfo interface first to conduct feature development.

个结果 ""

    没有找到相关内容 ""