Today I will briefly talk about the forwarding function of the WeChat applet. Why should I explain it briefly? Because it mainly talks about forwarding to friends or groups. There is also another kind of sharing to the circle of friends, which is more complicated. Okay, let me reveal a little bit first. There are two main methods for sharing to Moments. One is to directly generate poster images in the background, and the other is to generate posters through canvas on the front end. I’ll talk about it in detail later when I have the opportunity. Well, let’s get back to business and continue talking about our forwarding friends.
First introduce the API of a WeChat applet: onShareAppMessage(options)
Define the onShareAppMessage function in Page to set the forwarding information of the page.
Only when this event handler is defined, the "Forward" button will be displayed in the upper right corner menu
It will be called when the user clicks the forward button
This event needs to return an Object for custom forwarding content
options Parameter description
Parameters | Type | Description | Minimum version |
---|---|---|---|
from | String | Forward event source. button: forwarding button in the page; menu: forwarding menu in the upper right corner | 1.2.4 |
target | Object | If from If the value is button, then the target is the button that triggered this forwarding event, otherwise it is undefined | 1.2.4 |
Custom forwarding field
Field | Description | Default value | Minimum version |
---|---|---|---|
title | Forward title | Current applet name | |
path | Forwarding path | Current page path must be the complete path starting with / | |
Customized The image path can be a local file path, a code package file path or a network image path. It supports PNG and JPG. If imageUrl is not passed in, the default screenshot will be used. The aspect ratio of the displayed image is 5:4 |
##1.5.0 |
##success | |
1.1.0 |
fail | ||
1.1.0 |
complete | ||
1.1.0 |
|
The API will talk about this first, and then the implementation of forwarding
First configure wx.showShareMenu in onLoad
onLoad: function (e) { wx.showShareMenu({ // 要求小程序返回分享目标信息 withShareTicket: true }); },
/* 转发*/ onShareAppMessage: function (ops) { if (ops.from === 'button') { // 来自页面内转发按钮 console.log(ops.target) } return { title: '转发dom', path: `pages/index/index`, success: function (res) { // 转发成功 console.log("转发成功:" + JSON.stringify(res)); var shareTickets = res.shareTickets; // if (shareTickets.length == 0) { // return false; // } // //可以获取群组信息 // wx.getShareInfo({ // shareTicket: shareTickets[0], // success: function (res) { // console.log(res) // } // }) }, fail: function (res) { // 转发失败 console.log("转发失败:" + JSON.stringify(res)); } } },
The complete js code is
//index.js //获取应用实例 const app = getApp() Page({ data: { motto: 'Hello World', }, onLoad: function (e) { wx.showShareMenu({ // 要求小程序返回分享目标信息 withShareTicket: true }); }, /* 转发*/ onShareAppMessage: function (ops) { if (ops.from === 'button') { // 来自页面内转发按钮 console.log(ops.target) } return { title: '转发dom', path: `pages/index/index`, success: function (res) { // 转发成功 console.log("转发成功:" + JSON.stringify(res)); var shareTickets = res.shareTickets; // if (shareTickets.length == 0) { // return false; // } // //可以获取群组信息 // wx.getShareInfo({ // shareTicket: shareTickets[0], // success: function (res) { // console.log(res) // } // }) }, fail: function (res) { // 转发失败 console.log("转发失败:" + JSON.stringify(res)); } } }, })
<view class="container"> <view class="userinfo"> <button open-type="share">分享好友</button> </view> <view class="usermotto"> <text class="user-motto">{{motto}}</text> </view> </view>
WeChat Mini Program
"The above is the detailed content of WeChat mini program forwarding function to friends. For more information, please follow other related articles on the PHP Chinese website!