美文网首页
微信小程序全局自定义分享

微信小程序全局自定义分享

作者: 大白520 | 来源:发表于2020-07-05 10:20 被阅读0次

1、开启分享(放在onLoad中开启分享按钮,放在app.js的onLaunch中对全局有效)

//开启转发按钮
wx.showShareMenu({
    withShareTicket: true
})

2、设置全局分享代码,分享代码放在 app.js 第一行

!function(){
  var PageTmp = Page;
  Page =function (pageConfig) {
    // 设置全局默认分享
    pageConfig = Object.assign({
            //右上角分享功能
            onShareAppMessage () {
                var shareImage = ['../../image/share1.png','../../image/share2.png','../../image/share3.png'];//数组
                var shareTitle = ['标题1','标题2'];//数组和图片分开随机不对应
                var image = shareImage[Math.floor(Math.random() * shareImage.length)];
                var title = shareTitle[Math.floor(Math.random() * shareTitle.length)];
                
                return {
                    title: title,//分享标题
                    path: '/pages/index/index',//分享用户点开后页面
                    imageUrl: image,//分享图片
                    success (res) {
                        console.log('分享成功!')
                    }
                }
            }
    },pageConfig);
    PageTmp(pageConfig);
  };
}();

完成

备注:实现环境基础库版本2.11.0,其他版本没有测试

相关文章

网友评论

      本文标题:微信小程序全局自定义分享

      本文链接:https://www.haomeiwen.com/subject/uyomqktx.html