1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* 百度 |
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 Baidu |
13
|
|
|
* @package tinymeng\OAuth2\Gateways |
14
|
|
|
* @Author: TinyMeng <[email protected]> |
15
|
|
|
* @Created: 2023/07/09 |
16
|
|
|
*/ |
17
|
|
|
class Baidu extends Gateway |
18
|
|
|
{ |
19
|
|
|
const API_BASE = 'https://openapi.baidu.com/'; |
20
|
|
|
protected $AuthorizeURL = 'https://openapi.baidu.com/oauth/2.0/authorize'; |
21
|
|
|
protected $AccessTokenURL = 'https://openapi.baidu.com/oauth/2.0/token'; |
22
|
|
|
protected $UserInfoURL = 'https://openapi.baidu.com/rest/2.0/passport/users/getLoggedInUser'; |
23
|
|
|
|
24
|
|
|
public function __construct($config) |
25
|
|
|
{ |
26
|
|
|
parent::__construct($config); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Description: 得到跳转地址 |
31
|
|
|
* @author: JiaMeng <[email protected]> |
32
|
|
|
* Updater: |
33
|
|
|
* @return string |
34
|
|
|
*/ |
35
|
|
|
public function getRedirectUrl() |
36
|
|
|
{ |
37
|
|
|
//存储state |
38
|
|
|
$this->saveState(); |
39
|
|
|
//登录参数 |
40
|
|
|
$params = [ |
41
|
|
|
'response_type' => $this->config['response_type'], |
42
|
|
|
'client_id' => $this->config['app_id'], |
43
|
|
|
'redirect_uri' => $this->config['callback'], |
44
|
|
|
'state' => $this->config['state'], |
45
|
|
|
'scope' => $this->config['scope'], |
46
|
|
|
]; |
47
|
|
|
return $this->AuthorizeURL . '?' . http_build_query($params); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Description: 获取格式化后的用户信息 |
52
|
|
|
* @return array |
53
|
|
|
* @throws OAuthException |
54
|
|
|
* @author: JiaMeng <[email protected]> |
55
|
|
|
* Updater: |
56
|
|
|
*/ |
57
|
|
|
public function userInfo() |
58
|
|
|
{ |
59
|
|
|
$result = $this->getUserInfo(); |
60
|
|
|
$userInfo = [ |
61
|
|
|
'open_id' => isset($result['uid']) ? $result['uid'] : '', |
62
|
|
|
'union_id'=> isset($result['aid']) ? $result['aid'] : '', |
63
|
|
|
'channel' => ConstCode::TYPE_BAIDU, |
64
|
|
|
'nickname'=> $result['uname'] ?? $result['login_name'] ?? '', // 优先使用 uname |
65
|
|
|
'gender' => isset($result['sex']) ? (($result['sex'] == '男' ? 1 : ($result['sex'] == '女' ? 2 : 0))) : ConstCode::GENDER, |
66
|
|
|
'avatar' => $result['portrait'] ?? '', // 百度返回的头像字段 |
67
|
|
|
'birthday'=> '', |
68
|
|
|
'access_token'=> $this->token['access_token'] ?? '', |
69
|
|
|
'native'=> $result, |
70
|
|
|
]; |
71
|
|
|
return $userInfo; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Description: 获取原始接口返回的用户信息 |
76
|
|
|
* @return array |
77
|
|
|
* @throws OAuthException |
78
|
|
|
* @author: JiaMeng <[email protected]> |
79
|
|
|
* Updater: |
80
|
|
|
*/ |
81
|
|
|
public function getUserInfo() |
82
|
|
|
{ |
83
|
|
|
/** 获取用户信息 */ |
84
|
|
|
$this->openid(); |
85
|
|
|
|
86
|
|
|
$headers = ['Authorization: Bearer '.$this->token['access_token']]; |
87
|
|
|
$data = $this->get($this->UserInfoURL, [], $headers); |
88
|
|
|
$data = json_decode($data, true); |
89
|
|
|
|
90
|
|
|
if(!isset($data['uid'])) { |
91
|
|
|
throw new OAuthException("获取百度用户信息失败:" . ($data['error_description'] ?? '未知错误')); |
92
|
|
|
} |
93
|
|
|
return $data; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Description: 获取当前授权用户的openid标识 |
98
|
|
|
* @author: JiaMeng <[email protected]> |
99
|
|
|
* Updater: |
100
|
|
|
* @return string |
101
|
|
|
* @throws OAuthException |
102
|
|
|
*/ |
103
|
|
|
public function openid() |
104
|
|
|
{ |
105
|
|
|
$this->getToken(); |
106
|
|
|
return $this->token['uid'] ?? ''; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Description: 获取AccessToken |
112
|
|
|
* @author: JiaMeng <[email protected]> |
113
|
|
|
* Updater: |
114
|
|
|
*/ |
115
|
|
|
protected function getToken(){ |
116
|
|
|
if (empty($this->token)) { |
117
|
|
|
/** 验证state参数 */ |
118
|
|
|
$this->CheckState(); |
119
|
|
|
|
120
|
|
|
/** 获取参数 */ |
121
|
|
|
$params = $this->accessTokenParams(); |
122
|
|
|
|
123
|
|
|
/** 获取access_token */ |
124
|
|
|
$this->AccessTokenURL = $this->AccessTokenURL . '?' . http_build_query($params); |
125
|
|
|
$token = $this->post($this->AccessTokenURL); |
126
|
|
|
/** 解析token值(子类实现此方法) */ |
127
|
|
|
$this->token = $this->parseToken($token); |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Description: 解析access_token方法请求后的返回值 |
133
|
|
|
* @author: JiaMeng <[email protected]> |
134
|
|
|
* Updater: |
135
|
|
|
* @param $token |
136
|
|
|
* @return mixed |
137
|
|
|
* @throws OAuthException |
138
|
|
|
*/ |
139
|
|
|
protected function parseToken($token) |
140
|
|
|
{ |
141
|
|
|
$data = json_decode($token, true); |
142
|
|
|
if (isset($data['access_token'])) { |
143
|
|
|
return $data; |
144
|
|
|
} else { |
145
|
|
|
throw new OAuthException("获取Baidu ACCESS_TOKEN出错:{$data['error']}"); |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* 刷新AccessToken续期 |
152
|
|
|
* @param string $refreshToken |
153
|
|
|
* @return bool |
154
|
|
|
* @throws OAuthException |
155
|
|
|
*/ |
156
|
|
|
public function refreshToken($refreshToken) |
157
|
|
|
{ |
158
|
|
|
$params = [ |
159
|
|
|
'grant_type' => 'refresh_token', |
160
|
|
|
'refresh_token' => $refreshToken, |
161
|
|
|
'client_id' => $this->config['app_id'], |
162
|
|
|
'client_secret' => $this->config['app_secret'], |
163
|
|
|
]; |
164
|
|
|
|
165
|
|
|
$this->AccessTokenURL = static::API_BASE . 'oauth/2.0/token'; |
166
|
|
|
$token = $this->post($this->AccessTokenURL . '?' . http_build_query($params)); |
167
|
|
|
$token = $this->parseToken($token); |
168
|
|
|
|
169
|
|
|
if (isset($token['access_token'])) { |
170
|
|
|
$this->token = $token; |
171
|
|
|
return true; |
172
|
|
|
} |
173
|
|
|
return false; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* 检验授权凭证AccessToken是否有效 |
178
|
|
|
* @param string $accessToken |
179
|
|
|
* @return bool |
180
|
|
|
*/ |
181
|
|
|
public function validateAccessToken($accessToken = null) |
182
|
|
|
{ |
183
|
|
|
try { |
184
|
|
|
$accessToken = $accessToken ?? $this->token['access_token']; |
185
|
|
|
$headers = ['Authorization: Bearer ' . $accessToken]; |
186
|
|
|
$data = $this->get($this->UserInfoURL, [], $headers); |
187
|
|
|
$data = json_decode($data, true); |
188
|
|
|
return isset($data['uid']); |
189
|
|
|
} catch (OAuthException $e) { |
190
|
|
|
return false; |
191
|
|
|
} |
192
|
|
|
} |
193
|
|
|
} |
194
|
|
|
|