wxml
<button bindtap='getopenid'>获取openID</button> //调用js方法
js方法
getopenid:function(){
wx.login({
//获取code
success: function (res) {
var code = res.code //返回code
console.log(code)
wx.request({
url: ' https://api.weixin.qq.com/sns/jscode2session?appid=appid&secret=secret&js_code= ' + code +
'&grant_type=authorization_code', //appid 与 secret为对应小程序项目后台获取
data: {},
header: {
'content-type': 'application/json'
},
success: function (res) {
var openid = res.data.openid //返回openid
console.log(openid);
}
})
}
})
<button bindtap='getopenid'>获取openID</button> //调用js方法
js方法
getopenid:function(){
wx.login({
//获取code
success: function (res) {
var code = res.code //返回code
console.log(code)
wx.request({
url: ' https://api.weixin.qq.com/sns/jscode2session?appid=appid&secret=secret&js_code= ' + code +
'&grant_type=authorization_code', //appid 与 secret为对应小程序项目后台获取
data: {},
header: {
'content-type': 'application/json'
},
success: function (res) {
var openid = res.data.openid //返回openid
console.log(openid);
}
})
}
})
}
注意:如果出现此现象
可在详情设置中勾选
即可获取openid