How to create a logon process for WeChat applet development

Source: Internet
Author: User
Once logged on, we all know that we need an identifier to record the uniqueness of the user's identity. in unionId, it is the unique ID of the record we need, so how to obtain the unionId becomes the key, the project is divided into two parts: applet and backend PHP code. Once logged on, we all know that we need an identifier to record the uniqueness of the user's identity. in unionId, it is the unique ID of the record we need, so how to obtain the unionId becomes the key, the project is divided into two parts: applet and backend PHP code.

Starting with our small program code

Let's briefly talk about the js code logon process of our applet.

Login-> get code-> getUserInfo get iv and encryptedData-> send it to your server for processing-> return the result to the applet

Var API_URL = "your server address"; Page ({onLoad: function () {console. log ("iv"); wx. login ({// login process success: function (res) {// login successful if (res. code) {var code = res. code; wx. getUserInfo ({// getUserInfo process success: function (res2) {// console for obtaining userinfo successfully. log (res2); var encryptedData = encodeURIComponent (res2.encryptedData); // you must convert the encrypted string to URI encoded var iv = res2.iv; // request your server Login (code, encryptedData, iv) ;}}} else {Console. log ('failed to get user logon status! '+ Res. errMsg )}}});}})

Code: Required parameter for the server to obtain the sessionKey.

IV: The initial vector of the encryption algorithm. encryptedData: the encrypted string.

Pass code iv encryptedData to our server

Function Login (code, encryptedData, iv) {console. log ('code = '+ code +' & encryptedData = '+ encryptedData +' & iv = '+ iv); // Create a dialog wx. showToast ({title: 'logging on... ', icon: 'Loading', duration: 10000}); // request server wx. request ({url: API_URL, data: {code: code, encryptedData: encryptedData, iv: iv}, method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT header: {'content-type': 'application/json'}, // Set the request header success: function (res) {// success wx. hideToast (); console. log ('server return '+ res. data) ;}, fail: function () {// fail // wx. hideToast () ;}, complete: function () {// complete }})}

If you read this document, you should know that the unionId we need is in encryptedData, so the server needs this information to parse the unionId.

Server processing logic

Decryption demo downloaded first

The PHP code is selected here, and three class files except demo are put into our own project, which will be called later.

Here we will explain the server processing process:

Obtain the seesionKey through the interface, and then decrypt encryptedData through sessionKey and iv to obtain the UnionID.

/*** Log on ** @ return Response */public function weixinlogin ($ user_id = null) {global $ App_Error_Conf, $ Gift_Ids, $ Server_Http_Path, $ Is_Local, $ Test_User, $ Good_Vcode, $ WeiXin_Xd_Conf; $ validator_result = input_validator (array ('code', 'IV ', 'encrypteddat'); if (! Empty ($ validator_result) {return response ($ validator_result);} $ js_code = $ _ REQUEST ['code']; $ encryptedData = $ _ REQUEST ['encrypteddata']; $ iv = $ _ REQUEST ['IV']; $ appid = $ WeiXin_Xd_Conf ['appid ']; $ secret = $ WeiXin_Xd_Conf ['secret']; $ grant_type = $ WeiXin_Xd_Conf ['grant _ type']; // Obtain session_key from $ user_info_url = $ WeiXin_Xd_Conf ['code2session _ url']; $ user_info_url = sprintf ("% s? Appid = % s & secret = % s & js_code = % s & grant_type = % ", $ user_info_url, $ appid, $ secret, $ js_code, $ grant_type ); $ weixin_user_data = json_decode (get_url ($ user_info_url); $ session_key = $ weixin_user_data-> session_key; // decrypt data $ data = ''; $ response = new response ($ appid, $ session_key); $ errCode = $ wxBizDataCrypt> decryptData ($ appid, $ session_key, $ encryptedData, $ iv, $ data );

The last data we get is the decrypted encryptedData that contains the unionId.

The above describes how to create a logon process for applet development. For more information, see other related articles in the first PHP community!

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.