微信小程序登陸

 

一. 小程序不支持cookie會話javascript

  1. 經過傳遞與檢驗3rd_session來保持會話php

  2. 3rd_session能夠執行‘`head -n 80 /dev/urandom | tr -dc A-Za-z0-9 | head -c 168`該命令生成html

  3. 使用Redis或者數據庫存儲sessionjava

  4. 生成的3rd_session發送給客戶端,寫入storageajax

  5. 客戶端的每次請求必須帶上3rd_session數據庫

2、加密數據解碼json

  1. $iv,$code是被加密過的數據,因爲請求過程當中由於編碼緣由+號變成了空格,因此咱們須要用下面的方法轉換回來小程序

  

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

 

3、例子:api

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'=>'用戶信息獲取失敗!']);
            }
            
        }
    }

 

獲取微信信息模型(包括信息解密,官方例子點擊下載

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);
            }
        })
    },
相關文章
相關標籤/搜索