微信小程序--获取openid
wx.login({
complete: (res) => {
// 发送 res.code 到后台换取 openId, sessionKey, unionId
const code = res.code;
// 设置appid
const appId = app.globalData.appID;
// const appid = res.appid;
//设置secret
const secret = app.globalData.secretKey;
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) {
console.log(res)
// const openId = res.data.openid; //返回openid
}
})
}
})
结果