May 19, 2021 WeChat Mini Program Development Document
Define the onShareAppMessage function in Page to set the forwarding information for the page.
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 |
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) {
// 转发失败
}
}
}
})
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
})
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()
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() {
}
})
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
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.
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
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.
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.
tip
Forwarding a picture is not customizable;
tip
: Debugging support for forwarding
Check out normal forwarding debugging
support and forwarding with shareTicket
tip
shareTickets
return value can only be obtained by forwarding to group chats, and there is
shareTickets
tip
:
shareTicket
valid for the current gadget lifecycle
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.