Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the login function in Mini Program development

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

The knowledge points of this article "how to achieve login function in Mini Program development" are not quite understood by most people, so the editor summarizes the following contents for you, with detailed contents and clear steps, which can be used for reference. I hope you can get something after reading this article. Let's take a look at this "how to achieve login function in Mini Program development" article.

WeChat Mini Programs logged in

one。 Mini Program does not support cookie sessions

1. Maintain a session by passing and verifying 3rd_session

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

3. Use Redis or database to store session

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

5. The client must bring 3rd_session with each request

2. Decoding of encrypted data

1. $iv,$code is encrypted data, because the + sign becomes a space during the request due to coding reasons, so we need to convert it back with the following method

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

3. Examples:

Php

/ / Wechat login to 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'= > 0training session = > $session_id]);} else {$iv=define_str_replace (I (' get.iv')) / / convert spaces to + $encryptedData=urldecode (I ('get.encryptedData')); / decode $code=define_str_replace (I (' get.code')); / / convert spaces to + $msg=D ('Weixin')-> getUserInfo ($code,$encryptedData,$iv); / / get Wechat user information (openid) if ($msg [' errCode'] = = 0) {$open_id=$msg ['data']-> openId; $users_db=D (' Users') $info=$users_db- > getUserInfo ($open_id); if (! $info | | empty ($info)) {$users_db- > addUser (['open_id'= > $open_id,'last_time'= > [' exp','now ()']]); / / user information is stored in $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 training session = > $session_id]) / / return the 3rd_session to the client} else {$this- > ajaxReturn (['error_code'= > 0 recording session session = > $session_db- > getSid ($info [' id'])]);}} else {$this- > ajaxReturn (['error_code'= >' user information acquisition failed!']) ;}

Get Wechat information model (including information decryption, click download for official examples)

Require_once ABS_APP_PATH.'/Addon/Aes/wxBizDataCrypt.php';class WeixinModel {/ / get the user information of Wechat (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 ("% swords appendages% swords secretaries% swords jsails codewords% swatches grantless typeballs%", $url,$appid,$secret,$code,$grant_type); $user_data=json_decode (file_get_contents ($url)); $session_key= define_str_replace ($user_data- > session_key); $data= "; $wxBizDataCrypt=new\ WXBizDataCrypt ($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 login 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);}})}, the above is about "how to achieve login function in Mini Program development" this article, I believe you all have a certain understanding, I hope the content shared by the editor will be helpful to you, if you want to know more related knowledge content, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report