Skip to content

Commit

Permalink
Merge pull request #6 from yejiuxing/master
Browse files Browse the repository at this point in the history
Add files via upload,修复gitee登录,感谢您的贡献
  • Loading branch information
majiameng authored May 5, 2024
2 parents 5e7dd6f + e6cbdac commit 051933f
Showing 1 changed file with 126 additions and 137 deletions.
263 changes: 126 additions & 137 deletions src/Gateways/Gitee.php
Original file line number Diff line number Diff line change
@@ -1,137 +1,126 @@
<?php
/**
* Gitee
* api接口文档
*/
namespace tinymeng\OAuth2\Gateways;
use tinymeng\OAuth2\Connector\Gateway;
use tinymeng\OAuth2\Helper\ConstCode;

/**
* Class Gitee
* @package tinymeng\OAuth2\Gateways
* @Author: TinyMeng <666@majiameng.com>
* @Created: 2023/07/09
*/
class Gitee extends Gateway
{
const API_BASE = 'https://openapi.baidu.com/';
protected $AuthorizeURL = 'https://openapi.baidu.com/oauth/2.0/authorize';
protected $AccessTokenURL = 'https://openapi.baidu.com/';
protected $UserInfoURL = 'https://openapi.baidu.com/';

/**
* Description: 得到跳转地址
* @author: JiaMeng <666@majiameng.com>
* Updater:
* @return string
*/
public function getRedirectUrl()
{
//存储state
$this->saveState();
//登录参数
$params = [
'response_type' => $this->config['response_type'],
'client_id' => $this->config['app_id'],
'redirect_uri' => $this->config['callback'],
'state' => $this->config['state'],
'scope' => $this->config['scope'],
];
return $this->AuthorizeURL . '?' . http_build_query($params);
}

/**
* Description: 获取格式化后的用户信息
* @return array
* @throws \Exception
* @author: JiaMeng <666@majiameng.com>
* Updater:
*/
public function userInfo()
{
$result = $this->getUserInfo();
$userInfo = [
'open_id' => isset($result['uid']) ? $result['uid'] : '',
'union_id'=> isset($result['aid']) ? $result['aid'] : '',
'channel' => ConstCode::TYPE_GITEE,
'nickname'=> $result['login_name'],
'gender' => ConstCode::GENDER,
'avatar' => '',
'birthday'=> '',
'access_token'=> $this->token['access_token'] ?? '',
'native'=> $result,
];
return $userInfo;
}

/**
* Description: 获取原始接口返回的用户信息
* @return array
* @throws \Exception
* @author: JiaMeng <666@majiameng.com>
* Updater:
*/
public function getUserInfo()
{
/** 获取用户信息 */
$this->openid();

$headers = ['Authorization: Bearer '.$this->token['access_token']];
$data = $this->get($this->UserInfoURL, [],$headers);
return json_decode($data, true);
}

/**
* Description: 获取当前授权用户的openid标识
* @author: JiaMeng <666@majiameng.com>
* Updater:
* @return mixed
* @throws \Exception
*/
public function openid()
{
$this->getToken();
}


/**
* Description: 获取AccessToken
* @author: JiaMeng <666@majiameng.com>
* Updater:
*/
protected function getToken(){
if (empty($this->token)) {
/** 验证state参数 */
$this->CheckState();

/** 获取参数 */
$params = $this->accessTokenParams();

/** 获取access_token */
$this->AccessTokenURL = $this->AccessTokenURL . '?' . http_build_query($params);
$token = $this->post($this->AccessTokenURL);
/** 解析token值(子类实现此方法) */
$this->token = $this->parseToken($token);
}
}

/**
* Description: 解析access_token方法请求后的返回值
* @author: JiaMeng <666@majiameng.com>
* Updater:
* @param $token
* @return mixed
* @throws \Exception
*/
protected function parseToken($token)
{
$data = json_decode($token, true);
if (isset($data['access_token'])) {
return $data;
} else {
throw new \Exception("获取Gitee ACCESS_TOKEN出错:{$data['error']}");
}
}

}
<?php
/**
* Gitee
* api接口文档
*/
namespace ZhuoSheGuaMo\YxOAuth\Gateways;

use ZhuoSheGuaMo\YxOAuth\Connector\Gateway;
use ZhuoSheGuaMo\YxOAuth\Plug\ConstCode;

/**
* Class Gitee
*/
class Gitee extends Gateway
{
protected $AuthorizeURL = 'https://gitee.com/oauth/authorize';
protected $AccessTokenURL = 'https://gitee.com/oauth/token/';
protected $UserInfoURL = 'https://gitee.com/api/v5/user';

/**
* Description: 得到跳转地址
* Updater:
* @return string
*/
public function getRedirectUrl()
{
//存储state
$this->saveState();
//登录参数
$params = [
'response_type' => $this->config['response_type'],
'client_id' => $this->config['app_id'],
'redirect_uri' => $this->config['callback'],
'state' => $this->config['state'],
'scope' => $this->config['scope'],
];
return $this->AuthorizeURL . '?' . http_build_query($params);
}

/**
* Description: 获取格式化后的用户信息
* @return array
* @throws \Exception
*/
public function userInfo()
{
$result = $this->getUserInfo();
$userInfo = [
'open_id' => isset($result['id']) ? $result['id'] : '',
'union_id'=> isset($result['login']) ? $result['login'] : '',
'channel' => ConstCode::TYPE_GITEE,
'nickname'=> $result['name'],
'gender' => ConstCode::GENDER,
'avatar' => $result['avatar_url'],
'birthday'=> '',
'access_token'=> $this->token['access_token'] ?? '',
'native'=> $result,
];
return $userInfo;
}

/**
* Description: 获取原始接口返回的用户信息
* @return array
* @throws \Exception
*/
public function getUserInfo()
{
/** 获取用户信息 */
$this->openid();

// $headers = ['Authorization: Bearer '.$this->token['access_token']];
$params = [
'access_token'=>$this->token['access_token'],
];
$data = $this->get($this->UserInfoURL,$params);
return json_decode($data, true);
}

/**
* Description: 获取当前授权用户的openid标识
* @return mixed
* @throws \Exception
*/
public function openid()
{
$this->getToken();
}


/**
* Description: 获取AccessToken
*/
protected function getToken(){
if (empty($this->token)) {
/** 验证state参数 */
$this->CheckState();

/** 获取参数 */
$params = $this->accessTokenParams();

/** 获取access_token */
$this->AccessTokenURL = $this->AccessTokenURL . '?' . http_build_query($params);
$token = $this->post($this->AccessTokenURL);
/** 解析token值(子类实现此方法) */
$this->token = $this->parseToken($token);
}
}

/**
* Description: 解析access_token方法请求后的返回值
* @param $token
* @return mixed
* @throws \Exception
*/
protected function parseToken($token)
{
$data = json_decode($token, true);
if (isset($data['access_token'])) {
return $data;
} else {
throw new \Exception("获取Gitee ACCESS_TOKEN出错:{$data['error']}");
}
}

}

0 comments on commit 051933f

Please sign in to comment.