How to customize sharing card copy and pictures in WeChat mini program

The WeChat applet provides the onShareAppMessage method, which is specifically used to monitor the behavior of users clicking the forward button (button component open-type="share") or the "forward" button in the upper right corner menu, and customize the forwarded content.

> Note: Only when this event handler is defined, the "Forward" button will be displayed in the upper right corner menu

parameter type illustrate Minimum version
from String Forward event source. button: Forward button within the page;  menu: Forward menu in the upper right corner 1.2.4
target Object If  from the value is  buttontarget this forwarding event is triggered  button, otherwise it is undefined 1.2.4
webViewUrl String When the page contains the web-view component, the URL of the current web-view is returned. 1.6.4

This event handling function needs to return an Object for customizing the forwarding content. The return content is as follows:

Starting from version 2.8.1 of the custom forwarding content  base library   , shared images support cloud images.

Field illustrate default value Minimum version
title Forward title Current applet name
path Forwarding path Current page path, must be a complete path starting with /
imageUrl Customize the image path, which can be a local file path, a code package file path, or a network image path. Supports PNG and JPG. The display image aspect ratio is 5:4. Use default screenshot 1.5.0
promise If this parameter exists, the resolve result shall prevail. If it is not resolved within three seconds, the sharing will use the default parameters passed above. 2.12.0

Sample code

PS: The card image imageUrl can be local or the URL uploaded by the server.

onShareAppMessage() {
    return {
        title: '分享出去的卡片标题',
        path: '/index/index?id=123',
        imageUrl: '/img/bg.jpg'
    }
}

If you want to disable sharing of mini program cards on the current page, you can do the following in onLoad:

wx.hideShareMenu()

Guess you like

Origin blog.csdn.net/u010231454/article/details/132189637