


Detailed explanation of login example of WeChat applet (with code)
This article mainly introduces relevant information about the detailed explanation of WeChat Mini Program login examples. Friends in need can refer to
WeChat Mini Program Login
## 1. The applet does not support cookiesession
1. Maintain the session by passing and checking 3rd_session 2. 3rd_session can Execute '`head
-n 80 /dev/urandom | tr -dc A-Za-z0-9 | head -c 168`This command generates3. Use Redis
Or database storage session4. The generated 3rd_session is sent to the client and written into storage
5. Each request of the client must bring 3rd_session2. Decoding encrypted data
1. $iv, $code is encrypted data. Since the + sign becomes a space due to encoding during the request process, we need to use the following Method to convert back
function define_str_replace($data){ return str_replace(' ','+',$data); }
3. Example:
php
// 微信登录 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')); //把空格转成+ $encryptedData=urldecode(I('get.encryptedData')); //解码 $code=define_str_replace(I('get.code')); //把空格转成+ $msg=D('Weixin')->getUserInfo($code,$encryptedData,$iv); //获取微信用户信息(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()']]); //用户信息入库 $info=$users_db->getUserInfo($open_id); //获取用户信息 $session_id=`head -n 80 /dev/urandom | tr -dc A-Za-z0-9 | head -c 168`; //生成3rd_session $session_db->addSession(['uid'=>$info['id'],'id'=>$session_id]); //保存session } if($session_id){ $this->ajaxReturn(['error_code'=>0,'sessionid'=>$session_id]); //把3rd_session返回给客户端 }else{ $this->ajaxReturn(['error_code'=>0,'sessionid'=>$session_db->getSid($info['id'])]); } }else{ $this->ajaxReturn(['error_code'=>'用户信息获取失败!']); } } }Get WeChat information
Model
(including information decryption, official Click to download the example)require_once ABS_APP_PATH.'/Addon/Aes/wxBizDataCrypt.php'; class WeixinModel{ // 获取微信的用户信息(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=""; $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 { //调用登录接口 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, I hope it can help everyone, thank you for your support of this site!
The above is the detailed content of Detailed explanation of login example of WeChat applet (with code). For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 English version
Recommended: Win version, supports code prompts!

Zend Studio 13.0.1
Powerful PHP integrated development environment

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft
