在小程序app.js
App({
onLaunch: function () {
//调用API从本地缓存中获取数据
var that = this
wx.login({
success: function (loginCode) {
var appid = ''; //填写微信小程序appid
var secret = ''; //填写微信小程序secret
//调用request请求api转换登录凭证
wx.request({
url: 'https://api.weixin.qq.com/sns/jscode2session?appid=' + appid + '&secret=' + secret +'&grant_type=authorization_code&js_code=' + loginCode.code,
header: {
'content-type': 'application/json'
},
success: function (res) {
console.log(res.data.openid) //获取openid
}
})
}
})
}
})
appid和secret 在小程序平台 设置-开发设置里面找