1. 程式人生 > >基於ThinkPHP3.2.3的微信OAuth2.0微信網頁授權 微信公眾號網頁登入 改裝

基於ThinkPHP3.2.3的微信OAuth2.0微信網頁授權 微信公眾號網頁登入 改裝

首先我參考的這位仁兄的,感謝他地址 http://blog.csdn.net/a5435431941/article/details/61615851

仁兄沒有給sql,我的弄好了上傳出來 地址: http://download.csdn.net/detail/qq_27229113/9914285

程式碼也傳上來,地址:http://download.csdn.net/detail/qq_27229113/9914291

貼出我改動的部分

 //獲取使用者openid
    function getOpenid(){
        if(!$_GET['code']){
            //獲取當前的url地址
            // $rUrl=_URL_.__ACTION__.'.html';
            $rUrl="www.ypyunedu.com/Home/Demo/index"; //改成當前的url
            $url="https://open.weixin.qq.com/connect/oauth2/authorize?appid="._APPID_."&redirect_uri=".$rUrl."&response_type=code&scope=snsapi_base&state=12345#wechat_redirect";
            //跳轉頁面
            redirect($url,0);
        }else{
            $aUrl="https://api.weixin.qq.com/sns/oauth2/access_token?appid="._APPID_."&secret="._APPSECRET_."&code=".$_GET['code']."&grant_type=authorization_code";
            //獲取網頁授權access_token和openid等
            $data=getHttp($aUrl);
            return $data['openid'];
        }
    }


    //獲取使用者詳細資訊
    function getUserInfo(){
        if(!$_GET['code']){
            //獲取當前的url地址
            // $rUrl=_URL_.__ACTION__.'.html';
             $rUrl="http://www.ypyunedu.com/Home/Demo/index";//改成當前的url
            $url="https://open.weixin.qq.com/connect/oauth2/authorize?appid="._APPID_."&redirect_uri=".$rUrl."&response_type=code&scope=snsapi_userinfo&state=12345#wechat_redirect";
            //跳轉頁面
            redirect($url,0);
        }else{
            $getOpenidUrl="https://api.weixin.qq.com/sns/oauth2/access_token?appid="._APPID_."&secret="._APPSECRET_."&code=".$_GET['code']."&grant_type=authorization_code";
            //獲取網頁授權access_token和openid等
            $data=getHttp($getOpenidUrl);
            $getUserInfoUrl="https://api.weixin.qq.com/sns/userinfo?access_token=".$data['access_token']."&openid=".$data['openid']."&lang=zh_CN";
            //獲取使用者資料
            $userInfo=getHttp($getUserInfoUrl);
            //預設設定頭像是132*132的
            $userInfo['headimgurl']=substr($userInfo['headimgurl'],0,strlen($userInfo['headimgurl'])-1);
            $userInfo['headimgurl']=$userInfo['headimgurl'].'132';
            $userInfo['openid']=$userInfo['openid'];
            $userInfo['city']=$userInfo['city'];
            $userInfo['province']=$userInfo['province'];
            $userInfo['country']=$userInfo['country'];
            $userInfo['userInfo']=$userInfo['userInfo'];
            $userInfo['nickname']=$userInfo['nickname'];
            $userInfo['sex']=$userInfo['sex'];
            // 將資訊插入資料庫
            $userInfo['addtime']=date("Y-m-d H:i:s");
            //刪除language元素
            unset($userInfo['language']);
            $model=M("");
            if($model->table('wUserInfo')->data($userInfo)->add()){
                setSession($userInfo);
                session("status",null);
            }else{
                echo "驗證錯誤";
            }
        }
    }

把仁兄的方法貼上過來

第一步

將全部檔案放在網站目錄下

第二步

配置你的公眾號資訊

  • 進入檔案./Application/Common/Conf/config.php
  • 將你的公眾號相關資訊寫入
  1. <span style="font-size:14px;"><?php  
  2. //這裡填入的你域名
  3. define("_URL_","www.baidu.com");  
  4. //這裡填入你公眾號的APPID
  5. define("_APPID_","你公眾號的APPID");  
  6. //這裡填入你公眾號的APPSECRET
  7. define('_APPSECRET_','你公眾號的APPSECRET'
    );  
  8. returnarray(  
  9.     //'配置項'=>'配置值'
  10.     'DB_TYPE'               =>  'mysql',     // 資料庫型別
  11.     'DB_HOST'               =>  '127.0.0.1'// 伺服器地址
  12.     'DB_NAME'               =>  '',          // 資料庫名
  13.     'DB_USER'               =>  '',      // 使用者名稱
  14.     'DB_PWD'                =>  '',          
    // 密碼
  15.     'DB_PORT'               =>  '3306',        // 埠
  16.     'DB_PARAMS' => array(\PDO::ATTR_CASE => \PDO::CASE_NATURAL) ,  
  17.     'MODULE_ALLOW_LIST'    =>    array('Home','Admin'),//模組
  18.     'DEFAULT_MODULE'       =>    'Home',  
  19.     'URL_ROUTER_ON'   => true,   
  20.     'URL_ROUTE_RULES'=>array(  
  21.         'index$' => 'Home/Index/index'//定義路由
  22.     )  
  23. );</span>  

第三步

新建的控制器繼承OAuth2Controller類即可實現微信網頁授權認證功能
OAuth2Controller類的相關程式碼

  1. <span style="font-size:14px;"><?php  
  2. namespace Home\Controller;  
  3. use Think\Controller;  
  4. class OAuth2Controller extends Controller {  
  5.     publicfunction _initialize(){  
  6.         //判斷是否驗證過
  7.         if((session("?userOpenid")&&session("?userSex"))||(session("?userOpenid")&&session("?userNickname"))){  
  8.             //已驗證過
  9.             //如果你不需要使用者繫結的話,可以跳過下面這步
  10.             if(!session("?userID")){  
  11.                 $this->redirect('這裡填入要跳轉的繫結頁面','',2,"<h1>請先繫結賬號再使用,將自動跳轉到繫結頁面</h1>");  
  12.             }  
  13.         }else{  
  14.         //進入驗證
  15.         //方法放在./Application/Home/Common/function.php下
  16.             Check();  
  17.         }  
  18.     }  
  19. }</span>  

Demo

  1. <span style="font-size:14px;"><?php   
  2. namespace Home\Controller;  
  3. class DemoController extends OAuth2Controller {  
  4.     publicfunction index(){  
  5.         $this->show("這個是測試案例");  
  6.     }  
  7. }</span>  


P.S.

相關認證程式碼放在目錄./Application/Home/Common/function.php下(不是用TP寫的小夥伴可以在這裡參考下認證程式碼)
注意要建立相應的資料庫和表。具體更改在function目錄下更改getUserInfo()方法就好