Home  >  Article  >  WeChat Applet  >  How to implement sharing function in mini program (code example)

How to implement sharing function in mini program (code example)

不言
不言Original
2018-09-01 10:58:3511358browse

The content of this article is about how to implement the sharing function (code example) in the mini program. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

Because multiple pages in the mini program use sharing, we need to write the sharing function separately in a common file. util.js file.
//Sharing function

const shareEvent = (option, obj) => {
  let shareObj = {
    title: obj.title,
    path: obj.path,
    imgUrl: obj.imgUrl,
    success(res){
      // 转发成功之后的回调
    if (res.errMsg == 'shareAppMessage:ok') {}
    }, 
    fail(res){
       // 转发失败之后的回调
    if (res.errMsg == 'shareAppMessage:fail cancel') {
    // 用户取消转发
     } else if (res.errMsg == 'shareAppMessage:fail') {
     // 转发失败,其中 detail message 为详细失败信息
    }
    },
    complete(){
        // 转发结束之后的回调(转发成不成功都会执行)
    }
  };
  if (option.from === 'button') {
    // 来自页面内转发按钮
    console.log(option.target)
  }
  return shareObj;
}

Introduce util.js into the page that uses sharing

const util = require('./utils/util.js');
/**
用户点击右上角分享
*/
onShareAppMessage: function(option){
    console.log(option);
    let obj = {
      title: '我的老窝',
      path: 'pages/index/index',
      imageUrl: ''
    };
    return util.shareEvent(option, obj);
  }

Note: Remember to use return when calling.

Related recommendations:

Example sharing of WeChat applet implementation of process progress function

Example of sharing code for friends circle in applet development

How to get the return value of sharing success or failure from the sharing function

The above is the detailed content of How to implement sharing function in mini program (code example). 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