How does a applet obtain openid and user information?
How does a applet obtain openid and user information?
1. Obtain the openid
1.1 get code
Call the interface to obtain the login credential (code) in exchange for the user's login status information, including the user's unique identity (openid) and the session key (session_key) of the current login ). The encryption and decryption of user data depends on the session key.
Wx. login ({// get code success: function (res) {code = res. code // return code }})
1.2 obtain openid
Get the code obtained in the previous step, combined with the applet appid and secret request interface https://api.weixin.qq.com/sns/jscode2session? Appid = APPID & secret = SECRET & js_code = JSCODE & grant_type = authorization_code in exchange for openid. It is returned together with openid and also includes session_key. session_key is the key that encrypts and signs user data. For the sake of application security, session_key should not be transmitted over the network.
Wx. request ({url: 'https: // api.weixin.qq.com/sns/jscode2session? Appid = APPID & secret = SECRET & js_code = '+ code +' & grant_type = authorization_code ', data :{}, header: {'content-type ': 'application/json'}, success: function (res) {openid = res. data. openid // return openid }})
2. Get user information
2.1 create this global method in app. js.
// App. jsgetUserInfo: function (cb) {var that = this if (this. globalData. personInfo) {typeof cb = "function" & cb (this. globalData. personInfo)} else {// call the logon interface wx. login ({success: function () {wx. getUserInfo ({success: function (res) {that. globalData. personInfo = res. userInfo typeof cb = "function" & cb (that. globalData. personInfo )}})}})}}
2.2 instantiate the global method 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 ({personInfo: personInfo })})
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.