微信小程序获取openId
//首先获取到code
loginMode(){
let that = this
wx.login({
success(res){
if(res.code){
that.getOpenId(res.code)
}
},
fail(err){}
})
},
getOpenId(code){
let that = this
wx.request({
//全局变量
url: that.globalData.barseUrl + “接口名称”,
methos:'POST',
//将code作为参数传入
data: {code},
timeout: 3000,
header:{
'content-type':'application/x-www-form-urlencoded',
'platform':'3',
'version': '1.0.1'
},
success(){
if(res.statusCode!==200){
return
}else{
let val = res.data
if(val.code==200){
let openId = val.openId
wx.setStorageSync('openId', openId);
}
}
}
})
}