主要用到的文档有
1.自定義異常處理類 HttpException 位於application/lib/exception/HttpException .php
2.配置文档 config/qq.php
3.自己寫的核心文档 Qq.php
TP版本V5.1.5
PHP 版本 7.2
對於自定義異常處理類做了很多處理 所以這裡就不發了,大家用的時候可以自己擴展一下,也可以參考官方文档自定義一個異常處理
對於配置文档中的重要輸入 會用*代替
沒有對用戶注銷做處理,大家可以自己寫一下,在用戶退出的時候將調用qq登錄接口的Session全部刪掉就可以了!
Qq.php
<?php /** * Email: 1614369925@qq.com * User: 李昊天 * Date: 2018/3/4 * Time: 10:36 */ namespace app\user\controller; use app\lib\exception\HttpException; use think\facade\Session; class Qq { private $get_code_url = ''; //獲取code的地址 private $appId = ''; //appid private $appKey = ''; //appkey private $callback = ''; //回調地址 public function __construct() { $this->appId = config('qq.appId'); $this->appKey = config('qq.appKey'); $this->callback = config('qq.callback'); } public function qqLogin() { Session::set('qAuthData.state', md5(uniqid(rand(), TRUE))); $this->get_code_url = sprintf(config('qq.qq_login.get_code_url'), $this->appId, $this->callback, Session::get('qAuthData.state'), 'all'); //獲取Code 地址 return $this->get_code_url; } public function qq_callback() { if (Session::get('qAuthData.state') != input('get.state')) { throw new HttpException([ 'msg' => '參數錯誤,請重新發起登錄請求!' ]); } $code = input('get.code'); Session::set('qAuthData.code', $code); $url = sprintf(config('qq.get_account_token.get_account_token_url'), $this->appId, $this->callback, $this->appKey, $code); $Result = curl_get($url); //$response ='callback( {"error":100020,"error_description":"code is reused error"} )'; //請求失敗示例 //$response = 'access_token=************&expires_in=7776000&refresh_token=************'; //請求成功示例 if (strpos($Result, "callback") !== false) { $lpos = strpos($Result, "("); $rpos = strrpos($Result, ")"); $response = substr($Result, $lpos + 1, $rpos - $lpos - 1); $error = json_decode($response ); throw new HttpException([ 'msg' => $error->error_description, 'errorCode' => $error->error ]); } $data = []; parse_str($Result, $data); if (!array_key_exists('access_token', $data)) { throw new HttpException([ 'msg' => '獲取access_token失敗,服務器錯誤!', 'errorCode' => '500' ]); } $access_token = $data['access_token']; Session::set('qAuthData.access_token', $access_token); return $data; } public function get_openid() { $get_openId_url = config('qq.get_openId.get_openId_url') . '?access_token=' . Session::get('qAuthData.access_token'); $result = curl_get($get_openId_url); //$result = 'callback( {"client_id":"******","openid":"*************"} )'; //請求成功示例 //$result = callback( {"error":100007,"error_description":"param access token is wrong or lost "} ) //請求失敗示例 #檢測錯誤是否發生 if (strpos($result, "callback") !== false) { $l = strpos($result, "("); $r = strrpos($result, ")"); $Result = substr($result, $l + 1, $r - $l - 1); } $user = json_decode($Result); Session::set('qAuthData.openId', $user->openid); return $user->openid; } public function userinfo() { $UserData = Session::get('qAuthData'); if (!array_key_exists('access_token', $UserData)) { throw new HttpException([ 'msg' => 'access_token 不存在 無法獲取用戶信息!' ]); } if (!array_key_exists('openId', $UserData)) { throw new HttpException([ 'msg' => '用戶openID不存在,請重新登錄!' ]); } $access_token = $UserData['access_token']; $openId = $UserData['openId']; $get_user_info = sprintf(config('qq.get_user_info.get_user_info_url'), $access_token, $openId, $this->appId); $UserResult = curl_get($get_user_info); $res = json_decode($UserResult); if(!property_exists($res,'ret')){ throw new HttpException([ 'msg' => '獲取用戶信息失敗,未知錯誤' ]); } if($res->ret != 0){ throw new HttpException([ 'msg' => $res->msg ]); } return json($res); } }
config/qq.php 文档
<?php /** * User: 李昊天 * Email: 1614369925@qq.com * Date: 2018/2/26 * Time: 13:57 */ return [ 'appId' => '*********', 'appKey' => '*********', 'callback' => '*********', 'qq_login' => [ 'get_code_url' => 'https://graph.qq.com/oauth2.0/authorize?response_type=code&client_id=%s&redirect_uri=%s&state=%s&scope=%s' ], 'get_account_token'=>[ 'grant_type' => 'authorization_code', 'get_account_token_url' => 'https://graph.qq.com/oauth2.0/token?grant_type=authorization_code&client_id=%s&redirect_uri=%s&client_secret=%s&code=%s' ], 'get_openId' => [ 'get_openId_url'=>'https://graph.qq.com/oauth2.0/me' ], 'get_user_info' => [ 'get_user_info_url' => 'https://graph.qq.com/user/get_user_info?format=json&access_token=%s&openid=%s&oauth_consumer_key=%s' ], ];