Home  >  Article  >  WeChat Applet  >  Example analysis of WeChat applet implementing forwarding function

Example analysis of WeChat applet implementing forwarding function

黄舟
黄舟Original
2017-09-13 09:06:583462browse

This article mainly introduces relevant information about the implementation of the forwarding function of WeChat Mini Program. Here we provide implementation methods and examples to help everyone learn and understand. Friends in need can refer to

Forwarding of WeChat Mini Program Implementation of the function

1. When the user forwards the mini program to any group chat, the forwarded shareTicket can be obtained

2. The forwarded card is in the group chat When the chat is opened by other users, you can get another shareTicket in App.onLaunch() or App.onShow

3. The shareTicket obtained in the two steps can be obtained through the wx.getShareInfo() interface The same forwarding information can be obtained.

onShareAppMessage(options) function sets the forwarding information of the page.

options parameter description:

from: Forward event source. button: the forwarding button in the page; menu: the forwarding menu in the upper right corner;
target: if the from value is button, then the target is the button that triggered the forwarding event, otherwise it is undefined

Custom field:


return {
  title: '转发', // 转发标题(默认:当前小程序名称)
  path: '/pages/index/index', // 转发路径(当前页面 path ),必须是以 / 开头的完整路径
  success(e) {
   // shareAppMessage: ok,
   // shareTickets 数组,每一项是一个 shareTicket ,对应一个转发对象
     // 需要在页面onLoad()事件中实现接口
     wx.showShareMenu({
      // 要求小程序返回分享目标信息
      withShareTicket: true 
     });
  },
  fail(e) {
   // shareAppMessage:fail cancel
   // shareAppMessage:fail(detail message) 
  },
  complete() { }
}

wx.showShareMenu(OBJECT) Forward with shareTicket.

1. During the debugging process of both the SDK and the real machine, you need to set withShareTicket to true.

2. Otherwise, during the debugging process of the real machine, even if If a forwarded group chat is selected, shareTicket


onLoad(e) {
  wx.showShareMenu({
   withShareTicket: true
  })
 }, onShow(e) {
  wx.showShareMenu({
   withShareTicket: true
  })
 },

wx.getShareInfo(OBJECT) will not be returned to obtain forwarding details


if (res.shareTickets) {
 // 获取转发详细信息
 wx.getShareInfo({
  shareTicket: res.shareTickets[0],
  success(res) {
   res.errMsg; // 错误信息
   res.encryptedData; // 解密后为一个 JSON 结构(openGId  群对当前小程序的唯一 ID)
   res.iv; // 加密算法的初始向量
  },
  fail() {},
  complete() {}
 });
}

After the applet is opened in the group, get the situation value and shareTicket


//app.js
App({
 onLaunch: function (ops) {
  if (ops.scene == 1044) {
   console.log(ops.shareTicket)
  }
 }
})

Bug & Tip

1. The parameters passed between pages need to be received in the onLoad() function and cannot be received in the onShow() function.

2. To obtain the shareTicket of the applet opened in the group chat, you need to use App.onLaunch() or App.onShow(). It cannot be obtained in the life cycle function of the page.

3. When forwarding content to a single user, the shareTicket cannot be obtained.

The above is the detailed content of Example analysis of WeChat applet implementing forwarding function. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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