不多废话 直接上代码
//登录时 获取用户的openid
wx.login({
success: function (loginCode) {
var appid = ' 微信公众平台中开发设置里的 appid(小程序ID) ';
var secret = '微信公众平台中开发设置里的 secret(小程序密钥) ';
var code = loginCode.code;
// 通过 code 进行网络请求 openid
wx.request({
url: 'https://api.weixin.qq.com/sns/jscode2session?'
+ 'appId=' + appid
+ '&secret=' + secret
+ '&js_code=' + code
+ '&grant_type=authorization_code',
header: {
'content-type': 'application/json'
},
success: function (res) {
// 获取请求成功之后的 openid
openid = res.data.openid;
// 请求后台接口 进行 openid 保存
wx.request({
url: ' 后台接口 ?openid=' + openid,
header: {
'content-type': 'application/json'
},
success: function (res) {
}
})
}
});
}
})