下面我就直接进入主题。
定义一个button
buttonopen-type='share'type='primary'获取群id/button
在对应的js文件里面定义onShareAppMessage监听。
onShareAppMessage:function(){wx.showShareMenu({withShareTicket:true})return{title:"test",path:"/pages/index/index",success:function(res){varshareTickets=res.shareTickets;if(shareTickets.length==0){returnfalse;}wx.getShareInfo({shareTicket:shareTickets[0],success:function(res){varencryptedData=res.encryptedData;variv=res.iv;wx.login({success:function(res){varcode=res.code;console.log(code)wx.request({url:'xx',//写上你后台服务器路径(用途:解析出Gopenid)//解析出Gopenid需要session_key,encryptedData,iv。具体后台怎么写,//可以参照https://www.jianshu.com/p/856fe2195ffedata:{'code':code,'encryptedData':encryptedData,"iv":iv},header:{'content-type':'application/json'//默认值},success:function(res){wx.showModal({title:'GopenId',content:res.data.openGId})}})}})console.log(encryptedData)}})},fail:function(){}}}这样就能得到Gopenid了。
第二个问题,用户点击群里面,分享了的小程序后,怎么获取这个群的Gopenid呢?
在App.js的onLaunch里面定义如下代码就可以了
//1044代码是从群进入的代码if(ops.scene==1044){wx.showModal({title:'test',content:ops.shareTicket+""})wx.getShareInfo({shareTicket:ops.shareTicket,success:function(res){varencryptedData=res.encryptedData;variv=res.iv;wx.login({success:function(res){varcode=res.code;console.log(code)wx.request({url:'xxx',//跟前面一样,是服务器地址data:{'code':code,'encryptedData':encryptedData,"iv":iv},header:{'content-type':'application/json'//默认值},success:function(res){wx.showModal({title:'GopenId',content:res.data.openGId})}})}})}})}













