search
HomeWeChat AppletMini Program DevelopmentWeChat mini program forwarding function to friends

WeChat mini program forwarding function to friends

Jun 28, 2020 am 10:03 AM
javascriptfront endWeChat applet

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

<view>
  <view>
   <button>分享好友</button>
  </view>
  <view>
    <text>{{motto}}</text>
  </view>
</view>
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!

Statement
This article is reproduced at:掘金社区. If there is any infringement, please contact admin@php.cn delete

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function