微信ウィジェットログイン例の詳細

4816 ワード

WeChatウィジェットのログイン
一.ウィジェットはクッキーセッションをサポートしていません
  1. 伝達と検査による3 rd_セッションを維持する
  2. 3rd_sessionは‘head-n 80/dev/urandom|tr-dc A-Za-z 0-9|head-c 168’のコマンド生成を実行できます
  3. Redisまたはデータベースストレージセッションの使用
  4. 生成された3 rd_セッションはクライアントに送信しstorageに書き込む
  5. クライアントのリクエストごとに3 rd_を持参する必要があります.session
二、暗号化データ復号
  1. $iv,$codeは暗号化されたデータであり,要求中に符号化のため+番号がスペースになったため,次の方法で変換する必要がある.

 

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


三、例:
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);
      }
    })
  },

読書に感謝して、みんなを助けることができることを望んで、みんなの当駅に対する支持に感謝します!