Details on logon instances of WeChat applets (with code)

Source: Internet
Author: User
Tags openid
This article describes how to log on to an instance (with code) by using applets. For more information, see this article, for more information, see

Applet logon

1. applets do not support cookie sessions

1. maintain the session by passing and verifying 3rd_session

2. 3rd_session can execute ''head-n 80/dev/urandom | tr-dc A-Za-z0-9 | head-c 168 'this command is generated

3. use Redis or database to store Sessions

4. the generated 3rd_session is sent to the client and written to storage

5. each client request must contain 3rd_session

II. encryption data decoding

1. $ iv, $ code is the encrypted data. because the number + is converted to a space during the request, we need to use the following method to convert it back.

 function define_str_replace($data){    return str_replace(' ','+',$data);  }

III. example:

Php

// Log on to the public function weixin_login () {$ session_db = D ('session '); $ session_id = I ('Get. sessionid ', ''); $ session = $ session_db-> getSession ($ session_id); if (! Empty ($ session) {$ this-> ajaxReturn (['error _ code' => 0, 'sessionid' => $ session_id]);} else {$ iv = define_str_replace (I ('Get. iv '); // convert the space into + $ encryptedData = urldecode (I ('Get. encryptedData '); // decodes $ code = define_str_replace (I ('Get. code '); // convert the space to + $ msg = D ('weixin')-> getUserInfo ($ code, $ encryptedData, $ iv ); // obtain user information (openid) if ($ msg ['errcode'] = 0) {$ open_id = $ msg ['data']-> openId; $ users_db = D ('users'); $ info = $ u Sers_db-> getUserInfo ($ open_id); if (! $ Info | empty ($ info) {$ users_db-> addUser (['open _ id' => $ open_id, 'Last _ time' => ['Exp ', 'Now () ']); // puts user information into the database $ info = $ users_db-> getUserInfo ($ open_id ); // get user information $ session_id = 'head-n 80/dev/urandom | tr-dc A-Za-z0-9 | head-c 168 '; // Generate 3rd_session $ session_db-> addSession (['uid' => $ info ['id'], 'id' => $ session_id]); // save session} if ($ session_id) {$ this-> ajaxReturn (['error _ code' => 0, 'sessionid' => $ session_id]); // Return 3rd_session to the client} else {$ this-> ajaxReturn (['error _ code' => 0, 'sessionid' => $ session_db-> getSid ($ info ['id']);} else {$ this-> ajaxReturn (['error _ code' => 'user information acquisition failed! ']) ;}}

Get information model (including information decryption, official example Click to download)

Require_once ABS_APP_PATH. '/Addon/Aes/wxBizDataCrypt. php'; class WeixinModel {// User information obtained (openid) public function getUserInfo ($ code, $ encryptedData, $ iv) {$ appid = C ('appid '); $ secret = C ('secret'); $ grant_type = 'authorization _ code'; $ url = 'https: // api.weixin.qq.com/sns/jscode2session'; $ url = sprintf ("% s? Appid = % s & secret = % s & js_code = % s & grant_type = % ", $ url, $ appid, $ secret, $ code, $ grant_type ); $ user_data = json_decode (file_get_contents ($ url); $ session_key = define_str_replace ($ user_data-> session_key); $ data = ""; $ response = new \ response ($ appid, $ session_key); $ errCode = $ wxBizDataCrypt-> decryptData ($ encryptedData, $ iv, $ data); return ['errcode' => $ errCode, 'data' => json_decode ($ data), 'session _ key' => $ session_key];}

Javascript

GetUserInfo: function (cb) {var that = this if (this. globalData. userInfo) {typeof cb = "function" & cb (this. globalData. userInfo)} else {// call the logon interface wx. login ({success: function (r) {wx. getUserInfo ({success: function (res) {that. login ({code: r. code, iv: res. iv, encryptedData: encodeURIComponent (res. encryptedData),}) that. globalData. userInfo = res. userInfo typeof cb = "function" & cb (that. globalData. userInfo) }}) }}, login: function (param) {wx. request ({url: this. requestUrl ('index/weixin_login '), data: param, header: {'content-type': "application/json" ,}, success: function (res) {var data = JSON. parse (res. data. trim (); wx. setStorageSync ('sessionid', data. sessionid );}})},

Thank you for reading this article. I hope it will help you. thank you for your support for this site!

The above is a detailed description of the logon instance of the applet (with code). 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.