小程序获取openid

var openId = (wx.getStorageSync('openId'))
if (openId) {
wx.getUserInfo({
success: function (res) {
that.setData({
nickName: res.userInfo.nickName,
avatarUrl: res.userInfo.avatarUrl,
})
},
fail: function () {
// fail
console.log("获取失败!")
},
complete: function () {
// complete
console.log("获取用户信息完成!")
}
})
} else {
wx.login({
success: function (res) {
if (res.code) {
wx.getUserInfo({
withCredentials: true,
success: function (res_user) {
wx.login({
success: function (res) {
wx.request({
url: 'https://api.weixin.qq.com/sns/jscode2session',
data: {
appid: 'wx6286d06fe538aed9',
secret: '63ca18846d28001d334fecb84bd9bafb',
js_code: res.code,
grant_type: 'authorization_code'
},
method: 'GET',
success: function (res) {
var openid = res.data.openid;//获取到的openid
console.log(openid);
}
})
}
})
}, fail: function () {

}, complete: function (res) {

}
})
}
}
})

}



posted @ 2018-01-24 19:37  浅夏、  阅读(722)  评论(0编辑  收藏  举报