1
|
|
|
<?php |
2
|
|
|
namespace Wechat\API; |
3
|
|
|
|
4
|
|
|
use Wechat\Utils\Url; |
5
|
|
|
use Wechat\Api; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* 微信Auth相关接口. |
9
|
|
|
* |
10
|
|
|
* @author Tian. |
11
|
|
|
*/ |
12
|
|
|
class AuthApi extends BaseApi |
13
|
|
|
{ |
14
|
|
|
const API_URL = 'https://open.weixin.qq.com/connect/oauth2/authorize'; |
15
|
|
|
|
16
|
|
|
protected static $authorizedUser; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* 生成outh URL |
20
|
|
|
* |
21
|
|
|
* @param string $to |
22
|
|
|
* @param string $scope |
23
|
|
|
* @param string $state |
24
|
|
|
* |
25
|
|
|
* @return string |
26
|
|
|
*/ |
27
|
|
|
public function url($to = null, $scope = 'snsapi_userinfo', $state = 'STATE') |
28
|
|
|
{ |
29
|
|
|
$to !== null || $to = Url::current(); |
30
|
|
|
|
31
|
|
|
$queryStr = [ |
32
|
|
|
'appid' => $this->getAppId(), |
33
|
|
|
'redirect_uri' => $to, |
34
|
|
|
'response_type' => 'code', |
35
|
|
|
'scope' => $scope, |
36
|
|
|
'state' => $state, |
37
|
|
|
]; |
38
|
|
|
|
39
|
|
|
return self::API_URL . '?' . http_build_query($queryStr) . '#wechat_redirect'; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* 直接跳转 |
44
|
|
|
* |
45
|
|
|
* @param string $to |
46
|
|
|
* @param string $scope |
47
|
|
|
* @param string $state |
48
|
|
|
*/ |
49
|
|
|
public function redirect($to = null, $scope = 'snsapi_userinfo', $state = 'STATE') |
50
|
|
|
{ |
51
|
|
|
header('Location:' . $this->url($to, $scope, $state)); |
52
|
|
|
|
53
|
|
|
exit; |
|
|
|
|
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* 获取用户信息 |
58
|
|
|
* |
59
|
|
|
* @param string $openId |
60
|
|
|
* @param string $accessToken |
61
|
|
|
* |
62
|
|
|
* @return array |
63
|
|
|
*/ |
64
|
|
View Code Duplication |
public function getUser($openId, $accessToken) |
|
|
|
|
65
|
|
|
{ |
66
|
|
|
$queryStr = [ |
67
|
|
|
'access_token' => $accessToken, |
68
|
|
|
'openid' => $openId, |
69
|
|
|
'lang' => 'zh_CN', |
70
|
|
|
]; |
71
|
|
|
|
72
|
|
|
$this->apitype = 'sns'; |
73
|
|
|
$this->module = 'userinfo'; |
74
|
|
|
$res = $this->_get('', $queryStr); |
75
|
|
|
|
76
|
|
|
return $res; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* 获取已授权用户 |
81
|
|
|
* |
82
|
|
|
* @return array $user |
83
|
|
|
*/ |
84
|
|
|
public function user() |
|
|
|
|
85
|
|
|
{ |
86
|
|
|
if (self::$authorizedUser || !$_GET['state'] || (!$code = $_GET['code']) && $_GET['state']) { |
87
|
|
|
return self::$authorizedUser; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
$permission = $this->getAccessPermission($code); |
91
|
|
|
|
92
|
|
|
if ($permission['scope'] !== 'snsapi_userinfo') { |
93
|
|
|
$user = ['openid' => $permission['openid']]; |
94
|
|
|
} else { |
95
|
|
|
$user = $this->getUser($permission['openid'], $permission['access_token']); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
return $this->authorizedUser = $user; |
|
|
|
|
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* 通过授权获取用户 |
103
|
|
|
* |
104
|
|
|
* @param null $to |
105
|
|
|
* @param string $scope |
106
|
|
|
* @param string $state |
107
|
|
|
* |
108
|
|
|
* @return array |
109
|
|
|
*/ |
110
|
|
|
public function authorize($to = null, $scope = 'snsapi_userinfo', $state = 'STATE') |
|
|
|
|
111
|
|
|
{ |
112
|
|
|
if (!$_GET['state'] && !$code = $_GET['code']) { |
113
|
|
|
$this->redirect($to, $scope, $state); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
return $this->user(); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* 检查 Access Token 是否有效 |
121
|
|
|
* |
122
|
|
|
* @param string $accessToken |
123
|
|
|
* @param string $openId |
124
|
|
|
* |
125
|
|
|
* @return boolean |
126
|
|
|
*/ |
127
|
|
View Code Duplication |
public function accessTokenIsValid($accessToken, $openId) |
|
|
|
|
128
|
|
|
{ |
129
|
|
|
$params = [ |
130
|
|
|
'openid' => $openId, |
131
|
|
|
'access_token' => $accessToken, |
132
|
|
|
]; |
133
|
|
|
|
134
|
|
|
$this->apitype = 'sns'; |
135
|
|
|
$this->module = 'auth'; |
136
|
|
|
|
137
|
|
|
$res = $this->_get('', $params); |
138
|
|
|
|
139
|
|
|
return $res; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* 刷新 access_token |
144
|
|
|
* |
145
|
|
|
* @param $refreshToken |
146
|
|
|
* |
147
|
|
|
* @return bool|array |
148
|
|
|
*/ |
149
|
|
View Code Duplication |
public function refresh($refreshToken) |
|
|
|
|
150
|
|
|
{ |
151
|
|
|
$queryStr = [ |
152
|
|
|
'appid' => $this->getAppId(), |
153
|
|
|
'grant_type' => 'refresh_token', |
154
|
|
|
'refresh_token' => $refreshToken, |
155
|
|
|
]; |
156
|
|
|
|
157
|
|
|
$this->apitype = 'sns'; |
158
|
|
|
$this->module = 'oauth2'; |
159
|
|
|
$res = $this->_get('refresh_token', $queryStr); |
160
|
|
|
|
161
|
|
|
return $res; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* 获取access token |
166
|
|
|
* |
167
|
|
|
* @param string $code |
168
|
|
|
* |
169
|
|
|
* @return string |
170
|
|
|
*/ |
171
|
|
View Code Duplication |
public function getAccessPermission($code) |
|
|
|
|
172
|
|
|
{ |
173
|
|
|
$queryStr = [ |
174
|
|
|
'appid' => $this->getAppId(), |
175
|
|
|
'secret' => $this->getAppSecret(), |
176
|
|
|
'code' => $code, |
177
|
|
|
'grant_type' => 'authorization_code', |
178
|
|
|
]; |
179
|
|
|
|
180
|
|
|
$this->apitype = 'sns'; |
181
|
|
|
$this->module = 'oauth2'; |
182
|
|
|
$res = $this->_get('access_token', $queryStr); |
183
|
|
|
|
184
|
|
|
return $res; |
185
|
|
|
} |
186
|
|
|
} |
187
|
|
|
|
An exit expression should only be used in rare cases. For example, if you write a short command line script.
In most cases however, using an
exit
expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.