Skip to content

Commit

Permalink
Merge pull request #17 from anerg2046/v2
Browse files Browse the repository at this point in the history
QQ支持unionid
  • Loading branch information
anerg2046 authored Oct 18, 2018
2 parents 8793964 + 5e466b1 commit eece559
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 8 deletions.
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -199,10 +199,12 @@ $snsInfo = OAuth::$name($this->config)->mustCheckState()->userinfo();
#### 2.QQ

```
'app_id' => '1013****',
'app_secret' => '67c52bc284b32e7**********',
'scope' => 'get_user_info',
'app_id' => '1013****',
'app_secret' => '67c52bc284b32e7**********',
'scope' => 'get_user_info',
```
QQ现在可以获取`unionid`了,详见: http://wiki.connect.qq.com/unionid%E4%BB%8B%E7%BB%8D
只需要配置参数`$config['withUnionid'] = true`,默认不会请求获取Unionid

#### 3.微博

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "anerg2046/sns_auth",
"description": "weixin weibo qq sns oauth",
"description": "weixin weibo qq alipay twitter google line facebook sns oauth",
"type": "library",
"license": "MIT",
"authors": [
Expand Down
18 changes: 14 additions & 4 deletions src/Gateways/Qq.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ public function openid()
$this->getToken();

if (!isset($this->token['openid']) || !$this->token['openid']) {
$this->token['openid'] = $this->getOpenID();
$userID = $this->getOpenID();
$this->token['openid'] = $userID['openid'];
$this->token['unionid'] = isset($userID['unionid']) ?: '';
}

return $this->token['openid'];
Expand All @@ -49,6 +51,7 @@ public function userinfo()

$userinfo = [
'openid' => $this->openid(),
'unionid' => isset($this->token['unionid']) ? $this->token['unionid'] : '',
'channel' => 'qq',
'nick' => $rsp['nickname'],
'gender' => $rsp['gender'] == "" ? 'm' : 'f',
Expand Down Expand Up @@ -108,12 +111,19 @@ protected function parseToken($token)
*/
private function getOpenID()
{
$client = new \GuzzleHttp\Client();
$response = $client->request('GET', self::API_BASE . 'oauth2.0/me', ['query' => ['access_token' => $this->token['access_token']]]);
$client = new \GuzzleHttp\Client();

$query = ['access_token' => $this->token['access_token']];
// 如果要获取unionid,先去申请:http://wiki.connect.qq.com/%E5%BC%80%E5%8F%91%E8%80%85%E5%8F%8D%E9%A6%88
if (isset($this->config['withUnionid']) && $this->config['withUnionid'] === true) {
$query['unionid'] = 1;
}

$response = $client->request('GET', self::API_BASE . 'oauth2.0/me', ['query' => $query]);
$data = $response->getBody()->getContents();
$data = json_decode(trim(substr($data, 9), " );\n"), true);
if (isset($data['openid'])) {
return $data['openid'];
return $data;
} else {
throw new \Exception("获取用户openid出错:" . $data['error_description']);
}
Expand Down

0 comments on commit eece559

Please sign in to comment.