微信小程序转发好友的功能怎么实现(微信小程序,开发技术)

时间:2024-05-09 07:29:54 作者 : 石家庄SEO 分类 : 开发技术
  • TAG :

微信小程序转发好友的功能怎么实现

首先介绍一个微信小程序的API:onShareAppMessage(options)

在 Page 中定义 onShareAppMessage 函数,设置该页面的转发信息。

  • 只有定义了此事件处理函数,右上角菜单才会显示 “转发” 按钮

  • 用户点击转发按钮的时候会调用

  • 此事件需要 return 一个 Object,用于自定义转发内容

还有一个值那就是shareTickets他是转发成功返回的,并且是个数组,每一项是一个 shareTicket ,对应一个转发对象

API先说到这,接下来就是转发的实现:

首先要在onLoad中配置wx.showShareMenu

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

然后再配置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));varshareTickets=res.shareTickets;//if(shareTickets.length==0){//returnfalse;//}////可以获取群组信息//wx.getShareInfo({//shareTicket:shareTickets[0],//success:function(res){//console.log(res)//}//})},fail:function(res){//转发失败console.log("转发失败:"+JSON.stringify(res));}}},

我解释一下wx.getShareInfo这个可以获取到获取转发详细信息

完整js代码就是

//index.js//获取应用实例constapp=getApp()Page({data:{motto:'HelloWorld',},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));varshareTickets=res.shareTickets;//if(shareTickets.length==0){//returnfalse;//}////可以获取群组信息//wx.getShareInfo({//shareTicket:shareTickets[0],//success:function(res){//console.log(res)//}//})},fail:function(res){//转发失败console.log("转发失败:"+JSON.stringify(res));}}},})

聪明的同学就该知道接下来该是wxml代码

<viewclass="container"><viewclass="userinfo"><buttonopen-type="share">分享好友</button></view><viewclass="usermotto"><textclass="user-motto">{{motto}}</text></view></view>

友情提示一下如果点击按钮分享的话,button一定要设置open-type="share"否则不起作用。

 </div> <div class="zixun-tj-product adv-bottom"></div> </div> </div> <div class="prve-next-news">
本文:微信小程序转发好友的功能怎么实现的详细内容,希望对您有所帮助,信息来源于网络。
上一篇:外卖小程序对商家运营有什么用途下一篇:

14 人围观 / 0 条评论 ↓快速评论↓

(必须)

(必须,保密)

阿狸1 阿狸2 阿狸3 阿狸4 阿狸5 阿狸6 阿狸7 阿狸8 阿狸9 阿狸10 阿狸11 阿狸12 阿狸13 阿狸14 阿狸15 阿狸16 阿狸17 阿狸18