1
|
|
|
<?php
|
2
|
|
|
/**
|
3
|
|
|
* Gitee
|
4
|
|
|
* api接口文档
|
5
|
|
|
*/
|
6
|
|
|
namespace tinymeng\OAuth2\Gateways;
|
7
|
|
|
use tinymeng\OAuth2\Connector\Gateway;
|
8
|
|
|
use tinymeng\OAuth2\Exception\OAuthException;
|
9
|
|
|
use tinymeng\OAuth2\Helper\ConstCode;
|
10
|
|
|
|
11
|
|
|
/**
|
12
|
|
|
* Class Gitee
|
13
|
|
|
*/
|
14
|
|
|
class Gitee extends Gateway
|
15
|
|
|
{
|
16
|
|
|
protected $AuthorizeURL = 'https://gitee.com/oauth/authorize';
|
17
|
|
|
protected $AccessTokenURL = 'https://gitee.com/oauth/token/';
|
18
|
|
|
protected $UserInfoURL = 'https://gitee.com/api/v5/user';
|
19
|
|
|
|
20
|
|
|
/**
|
21
|
|
|
* Description: 得到跳转地址
|
22
|
|
|
* Updater:
|
23
|
|
|
* @return string
|
24
|
|
|
*/
|
25
|
|
|
public function getRedirectUrl()
|
26
|
|
|
{
|
27
|
|
|
//存储state
|
28
|
|
|
$this->saveState();
|
29
|
|
|
//登录参数
|
30
|
|
|
$params = [
|
31
|
|
|
'response_type' => $this->config['response_type'],
|
32
|
|
|
'client_id' => $this->config['app_id'],
|
33
|
|
|
'redirect_uri' => $this->config['callback'],
|
34
|
|
|
'state' => $this->config['state'],
|
35
|
|
|
'scope' => $this->config['scope'],
|
36
|
|
|
];
|
37
|
|
|
return $this->AuthorizeURL . '?' . http_build_query($params);
|
38
|
|
|
}
|
39
|
|
|
|
40
|
|
|
/**
|
41
|
|
|
* Description: 获取格式化后的用户信息
|
42
|
|
|
* @return array
|
43
|
|
|
* @throws OAuthException
|
44
|
|
|
*/
|
45
|
|
|
public function userInfo()
|
46
|
|
|
{
|
47
|
|
|
$result = $this->getUserInfo();
|
48
|
|
|
$userInfo = [
|
49
|
|
|
'open_id' => isset($result['id']) ? $result['id'] : '',
|
50
|
|
|
'union_id'=> isset($result['login']) ? $result['login'] : '',
|
51
|
|
|
'channel' => ConstCode::TYPE_GITEE,
|
52
|
|
|
'nickname'=> $result['name'],
|
53
|
|
|
'gender' => ConstCode::GENDER,
|
54
|
|
|
'avatar' => $result['avatar_url'],
|
55
|
|
|
'birthday'=> '',
|
56
|
|
|
'access_token'=> $this->token['access_token'] ?? '',
|
57
|
|
|
'native'=> $result,
|
58
|
|
|
];
|
59
|
|
|
return $userInfo;
|
60
|
|
|
}
|
61
|
|
|
|
62
|
|
|
/**
|
63
|
|
|
* Description: 获取原始接口返回的用户信息
|
64
|
|
|
* @return array
|
65
|
|
|
* @throws OAuthException
|
66
|
|
|
*/
|
67
|
|
|
public function getUserInfo()
|
68
|
|
|
{
|
69
|
|
|
/** 获取用户信息 */
|
70
|
|
|
$this->openid();
|
71
|
|
|
|
72
|
|
|
$params = [
|
73
|
|
|
'access_token'=>$this->token['access_token'],
|
74
|
|
|
];
|
75
|
|
|
$data = $this->get($this->UserInfoURL,$params);
|
76
|
|
|
return json_decode($data, true);
|
77
|
|
|
}
|
78
|
|
|
|
79
|
|
|
/**
|
80
|
|
|
* Description: 获取当前授权用户的openid标识
|
81
|
|
|
* @return mixed
|
82
|
|
|
* @throws OAuthException
|
83
|
|
|
*/
|
84
|
|
|
public function openid()
|
85
|
|
|
{
|
86
|
|
|
$this->getToken();
|
87
|
|
|
}
|
88
|
|
|
|
89
|
|
|
|
90
|
|
|
/**
|
91
|
|
|
* Description: 获取AccessToken
|
92
|
|
|
*/
|
93
|
|
|
protected function getToken(){
|
94
|
|
|
if (empty($this->token)) {
|
95
|
|
|
/** 验证state参数 */
|
96
|
|
|
$this->CheckState();
|
97
|
|
|
|
98
|
|
|
/** 获取参数 */
|
99
|
|
|
$params = $this->accessTokenParams();
|
100
|
|
|
|
101
|
|
|
/** 获取access_token */
|
102
|
|
|
$this->AccessTokenURL = $this->AccessTokenURL . '?' . http_build_query($params);
|
103
|
|
|
$token = $this->post($this->AccessTokenURL);
|
104
|
|
|
/** 解析token值(子类实现此方法) */
|
105
|
|
|
$this->token = $this->parseToken($token);
|
106
|
|
|
}
|
107
|
|
|
}
|
108
|
|
|
|
109
|
|
|
/**
|
110
|
|
|
* Description: 解析access_token方法请求后的返回值
|
111
|
|
|
* @param $token
|
112
|
|
|
* @return mixed
|
113
|
|
|
* @throws OAuthException
|
114
|
|
|
*/
|
115
|
|
|
protected function parseToken($token)
|
116
|
|
|
{
|
117
|
|
|
$data = json_decode($token, true);
|
118
|
|
|
if (isset($data['access_token'])) {
|
119
|
|
|
return $data;
|
120
|
|
|
} else {
|
121
|
|
|
throw new OAuthException("获取Gitee ACCESS_TOKEN出错:{$data['error']}");
|
122
|
|
|
}
|
123
|
|
|
}
|
124
|
|
|
|
125
|
|
|
} |