入坑微信小程序必经之路(七)获取当前用户登录Openid
不废话,看代码
onLaunch: function() {
var _this = this;
var url=this.globalData.url+"WeiXinConfig";
wx.request({
method: 'POST',
url:url,
header:{
'content-type':'application/json'
},success(res) {
var Resdata=JSON.parse(res.data.d);
if(Resdata[0]!=undefined){
wx.login({
//获取code
success: function (res) {
var code = res.code; //返回code
var appId = Resdata[0].appid;
var secret = Resdata[0].appsecret;
wx.request({
url: 'https://api.weixin.qq.com/sns/jscode2session?appid=' + appId + '&secret=' + secret + '&js_code=' + code + '&grant_type=authorization_code',
data: {},
header: {
'content-type': 'json'
},
success: function (res) {
var openid = res.data.openid //返回openid
console.log('openid为' + openid);
console.log('appid为' + Resdata[0].appid);
console.log('appsecret为' + Resdata[0].appsecret);
if(openid!=undefined){
wx.setStorageSync("openid",openid);
// wx.setStorageSync("appid",Resdata[0].appid);
// wx.setStorageSync("appsecret",Resdata[0].appsecret);
}
}
})
}
})
}
}
});

浙公网安备 33010602011771号