Home > WeChat Applet > Mini Program Development > WeChat mini program forwarding function to friends

WeChat mini program forwarding function to friends

hzc
Release: 2020-06-28 10:03:03
forward
3721 people have browsed it

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

##imageUrlCustomized 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##successCallback for successful forwarding FunctionfailCallback function for forwarding failurecompleteThe callback function at the end of forwarding (will be executed if the forwarding is successful or failedThere is another value, shareTickets, which is returned successfully by forwarding, and is an array. One item is a shareTicket, corresponding to a forwarding object
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 /

##1.5.0
1.1.0
1.1.0
1.1.0


The API will talk about this first, and then the implementation of forwarding

Look at the picture first:

First configure wx.showShareMenu in onLoad

  onLoad: function (e) {
    wx.showShareMenu({
      // 要求小程序返回分享目标信息
      withShareTicket: true
    }); 
  },
Copy after login
Then configure onShareAppMessage

/* 转发*/
  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));
      }
    }
  },
Copy after login
Let me explain wx.getShareInfo to get forwarding details

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));
      }
    }
  },
})
Copy after login
Smart students should know that the next step is the wxml code

<view class="container">
  <view class="userinfo">
   <button open-type="share">分享好友</button>
  </view>
  <view class="usermotto">
    <text class="user-motto">{{motto}}</text>
  </view>
</view>
Copy after login
A friendly reminder that if you click the button to share, the button must be set to open-type= "share" will not work otherwise.

If you think the article is good and helpful to you, please share it with your friends and like it. If you don't understand anything, you can leave a message below.

Recommended tutorial: "

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!

Related labels:
source:juejin.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template