How can I obtain the openid and user information of WeChat applets?

Source: Internet
Author: User
Tags openid

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.