代码如下
wx.login({
//获取code
success: function (res) {
let code = res.code //返回code
let APPID = “wx07de8bea337407f2”;
let SECRET = “f88ba62fbfa5b647487a3ddc91c3f151”;
wx.request({
url: ‘https://api.weixin.qq.com/sns/jscode2session?appid=’ + APPID + ‘&secret=’ + SECRET + ‘&js_code=’ + res.code+ ‘&grant_type=authorization_code’,
data: {},
header: {
‘content-type’: ‘application/json’
},
success: function (res) {
let openid = res.data.openid //返回openid
console.log(openid)
getApp().globalData.openid = openid;
wx.setStorage({
key: ‘openid’,
data: openid,
success: function(res) {},
fail: function(res) {},
complete: function(res) {},
})
wx.getUserInfo({
success: function(res) {
getApp().globalData.userInfo = res.rawData;
},
fail: function(res) {},
complete: function(res) {},
})
},
fail(res) {
// console.log(“q”)
}
})
}
});