wx.login({ //wx.login 会返回code
success: res => {
if (res.code) {//如果res.code包含参数则为true 执行下一步
wx.request({//发送 res.code 到后台换取 openId, sessionKey, unionId
url: '接口地址url', //仅为示例,并非真实的接口地址
method: "GET",//方法get / post
data: {
"code": res.code
},
header: {//设置请求头
'content-type': 'application/json' // 默认值
},
// 小程序包含多种接收方式 sueccess(成功) / fail(失败)/ complent(成功/失败)
success: function (res) {//请求成功接收res
if (res.data.openid) {
wx.setStorage({
key: "tokenId",
data: res.data.openid,
})
}
}
})
}
}
})