1. Get openid
1.1 Get code
First we need to call the interface to get the login credentials, which is the code, to get the user login status information , there is a unique identifier, which is openid, and the key (session_key) we use to log in. We all need to use the key to obtain the user's basic data.
wx.login({
//Get code
success: function(res) {
code = res.code / /Return code
}
})
1.2 Get openid
Get the code obtained in the previous step, combine the applet appid and secret request interface api.weixin.qq.com/sns/jscode2session?appid=APPID&secret=SECRET&js_code=JSCODE&grant_type=authorization_code in exchange for openid, which is returned together with openid, also includes session_key, where session_key is the key for encrypting and signing user data . For the sake of application security, session_key should not be transmitted over the network.
wx.request({
url: 'api.weixin.qq.com/sns/jscode2session?appid=APPID&secret=SECRET&js_code='+ code +'&grant_type=authorization_code',
data: {},
header: {
'content-type': 'application/json'
},< /p>
success: function(res) {
openid = res.data.openid //return openid
}
}) p>
2. Get user information
2.1 Create the global method in app.js
//app.js
getUserInfo:function( cb){
var that = this
if(this.globalData.personInfo){
typeof cb == "function" && cb(this.globalData .personInfo)
}else{
//Call the login interface
wx.login({
success: function () {
wx.getUserInfo({
success: function (res) {
that.globalData.personInfo = res.userInfo
typeof cb == "function" && cb(that.globalData.personInfo)
}
})
}
})< /p>
}
}
2.2 Instantiate global methods to obtain user information
var that = this;
/ /Call the method of the application instance to obtain global data
app.getUserInfo(function (personInfo) {
//Update data
that.setData({
p>personInfo: personInfo
})
})