tp5 微信授權登錄

 因爲工作需要自己用tp5封裝了微信授權登錄功能,參考書籍爲方倍的微信開發書。
本文章作爲一個記錄方式,防止自己忘記,如果能對哪個小夥伴兒一點幫助那就更好了。
授權登錄在數據庫有一張微信用戶表,需要提前自建。
開始上代碼:
//授權登錄發起方式爲:
$url= 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
$this->User($url);
    /*
 +---------------------------------------------------------------
 |授權登錄
 +---------------------------------------------------------------
 */
 public function User($callback){//傳入參數爲需要授權登錄地址
    if(session('openid'))//判斷用戶是否登錄過如果登陸過
    {
        $user=model('ld_member')->where('openid',session('openid'))->find();
        if(!$user){//判斷用戶在數據庫中是否有記錄如果沒有記錄就清空登錄狀態重新發起userinfo方式用戶授權
            session(null);
            $this->redirect("https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx207fa66c97a2db5c&redirect_uri=$callback&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect");
        }else{//如果用戶登錄過並且在數據庫中找到了用戶的信息就把用戶信息寫入到模板並設置全局變量備用
        //因爲用戶暱稱有特殊字符數據庫不能存儲所以進行了格式轉換需要使用時重新轉換回來
            $mistdata=json_decode($user['nickname']);//將數據庫中json數據還原
            $user['namep']=urldecode($mistdata);//將十六進制暱稱還原成中文
            $this->user=$user;
            $this->assign('user',$user);
            return true;
        }
    }else{//如果沒有登錄執行這部分代碼
        $get = input('param.');
        if($get){//這裏判斷是否有get數據 如果有說明是登錄授權回調 用code獲取用戶openid
            $appid = config('weixin.appID');//此處加載APPid可以直接寫上
            $appsecret = config('weixin.appsecret');
            if(isset($get['code'])){//判斷是否有code參數
                $code=$get['code'];
                $urlp="https://api.weixin.qq.com/sns/oauth2/access_token?appid=$appid&secret=$appsecret&code=$code&grant_type=authorization_code";
                $res = $this->https_request($urlp);//通過code獲取openid
                $userinfo= json_decode($res, JSON_UNESCAPED_UNICODE);
                 session('openid',$userinfo['openid']);
                if($userinfo['scope']=='snsapi_base'){//判斷獲取方式是BASE還是userinfo
                    $user=model('ld_member')->where('openid',session('openid'))->find();
                    if(!isset($user['is_auth'])){//如果是base方式發起數據庫沒有用戶信息就用userinfo方式重新發起
                        session(null);
                        $this->redirect("https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx207fa66c97a2db5c&redirect_uri=$callback&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect");
                    }else{
                        $mistdata=json_decode($user['nickname']);//將數據庫中json數據還原
                        $user['namep']=urldecode($mistdata);//將十六進制暱稱還原成中文
                        $this->is_auth=$user['is_auth'];
                        $this->openid=$user['openid'];
                        $this->user=$user;
                        $this->assign('user',$user);
                        return true;
                    }       
                }else{//如果是userinfo 發起就接着調用接口獲取用戶詳細信息
                    $token=$userinfo['access_token'];
                    $apenid=$userinfo['openid'];
                    $urln="https://api.weixin.qq.com/sns/userinfo?access_token=$token&openid=$apenid&lang=zh_CN";
                    $ress = $this->https_request($urln);//用opendi獲取用戶資料
                    $username= json_decode($ress, JSON_UNESCAPED_UNICODE);
                    $dataname=urlencode($username['nickname']);//將暱稱中文轉成16進制數字
                    $namedata=json_encode($dataname);
                    $userdata=array(
                       'openid'=>$username['openid'],
                       'nickname'=>$namedata,
                       'gender'=>$username['sex'],
                       'language'=>$username['language'],
                       'city'=>$username['city'],
                       'province'=>$username['province'],
                       'country'=>$username['country'],
                       'avatar'=>$username['headimgurl'],
                       'created_at'=>time()
                       );
                    $userstatus=model('ld_member')->allowField(true)->insert($userdata);//將數據寫入到數據庫
                    $user=model('ld_member')->where('openid',session('openid'))->find();//將用戶信息從數據庫查出來寫入到模板和全局備用
                    $mistdata=json_decode($user['nickname']);//將數據庫中json數據還原
                    $user['namep']=urldecode($mistdata);//將十六進制暱稱還原成中文
                    $this->is_auth=$user['is_auth'];
                    $this->openid=$user['openid'];
                    $this->user=$user;
                    $this->assign('user',$user);
                    return true;
                }
            }else{//如果get數據沒有code參數 發起base方式授權登錄獲取CODE
                $this->redirect("https://open.weixin.qq.com/connect/oauth2/authorize?appid='你的appid'&redirect_uri=$callback&response_type=code&scope=snsapi_base&state=2#wechat_redirect"); 
            }
        }
        //如果沒有get數據說明是後臺調用方法 發起base方式授權登錄獲取CODE
        $this->redirect("https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx207fa66c97a2db5c&redirect_uri=$callback&response_type=code&scope=snsapi_base&state=2#wechat_redirect");
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章