1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace anerg\OAuth2\Gateways; |
4
|
|
|
|
5
|
|
|
use anerg\OAuth2\Connector\Gateway; |
6
|
|
|
use anerg\OAuth2\Helper\Str; |
7
|
|
|
|
8
|
|
|
class Line extends Gateway |
9
|
|
|
{ |
10
|
|
|
const API_BASE = 'https://api.line.me/v2/'; |
11
|
|
|
protected $AuthorizeURL = 'https://access.line.me/oauth2/v2.1/authorize'; |
12
|
|
|
protected $AccessTokenURL = 'https://api.line.me/oauth2/v2.1/token'; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* 构造函数 |
16
|
|
|
* |
17
|
|
|
* @param array $config |
18
|
|
|
*/ |
19
|
|
|
public function __construct($config = null) |
20
|
|
|
{ |
21
|
|
|
parent::__construct($config); |
22
|
|
|
$this->clientParams(); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* 设置客户端请求的参数 |
27
|
|
|
* |
28
|
|
|
* @return void |
29
|
|
|
*/ |
30
|
|
|
private function clientParams() |
31
|
|
|
{ |
32
|
|
|
if (isset($this->config['access_token']) && !empty($this->config['access_token'])) { |
33
|
|
|
$this->token['access_token'] = $this->config['access_token']; |
34
|
|
|
} |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* 得到跳转地址 |
39
|
|
|
*/ |
40
|
|
|
public function getRedirectUrl() |
41
|
|
|
{ |
42
|
|
|
$params = [ |
43
|
|
|
'response_type' => $this->config['response_type'], |
44
|
|
|
'client_id' => $this->config['app_id'], |
45
|
|
|
'redirect_uri' => $this->config['callback'], |
46
|
|
|
'scope' => $this->config['scope'], |
47
|
|
|
'state' => $this->config['state'] ?: Str::random(), |
48
|
|
|
]; |
49
|
|
|
return $this->AuthorizeURL . '?' . http_build_query($params); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* 获取当前授权用户的openid标识 |
54
|
|
|
*/ |
55
|
|
|
public function openid() |
56
|
|
|
{ |
57
|
|
|
$rsp = $this->userinfoRaw(); |
58
|
|
|
return $rsp['userId']; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* 获取格式化后的用户信息 |
63
|
|
|
*/ |
64
|
|
|
public function userinfo() |
65
|
|
|
{ |
66
|
|
|
$rsp = $this->userinfoRaw(); |
67
|
|
|
|
68
|
|
|
$userinfo = [ |
69
|
|
|
'openid' => $rsp['userId'], |
70
|
|
|
'channel' => 'line', |
71
|
|
|
'nick' => $rsp['displayName'], |
72
|
|
|
'gender' => 'n', //line不返回性别信息 |
73
|
|
|
'avatar' => isset($rsp['pictureUrl']) ? $rsp['pictureUrl'] . '/large' : '', |
74
|
|
|
]; |
75
|
|
|
return $userinfo; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* 获取原始接口返回的用户信息 |
80
|
|
|
*/ |
81
|
|
|
public function userinfoRaw() |
82
|
|
|
{ |
83
|
|
|
$this->getToken(); |
84
|
|
|
|
85
|
|
|
$data = $this->call('profile', $this->token, 'GET'); |
86
|
|
|
|
87
|
|
|
if (isset($data['error'])) { |
88
|
|
|
throw new \Exception($data['error_description']); |
89
|
|
|
} |
90
|
|
|
return $data; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* 发起请求 |
95
|
|
|
* |
96
|
|
|
* @param string $api |
97
|
|
|
* @param array $params |
98
|
|
|
* @param string $method |
99
|
|
|
* @return array |
100
|
|
|
*/ |
101
|
|
|
private function call($api, $params = [], $method = 'GET') |
102
|
|
|
{ |
103
|
|
|
$method = strtoupper($method); |
104
|
|
|
$request = [ |
105
|
|
|
'method' => $method, |
106
|
|
|
'uri' => self::API_BASE . $api, |
107
|
|
|
]; |
108
|
|
|
|
109
|
|
|
$headers = ['Authorization' => (isset($this->token['token_type']) ? $this->token['token_type'] : 'Bearer') . ' ' . $this->token['access_token']]; |
110
|
|
|
|
111
|
|
|
$data = $this->$method($request['uri'], $params, $headers); |
112
|
|
|
|
113
|
|
|
return json_decode($data, true); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* 解析access_token方法请求后的返回值 |
118
|
|
|
* @param string $token 获取access_token的方法的返回值 |
119
|
|
|
*/ |
120
|
|
|
protected function parseToken($token) |
121
|
|
|
{ |
122
|
|
|
$token = json_decode($token, true); |
123
|
|
|
if (isset($token['error'])) { |
124
|
|
|
throw new \Exception($token['error_description']); |
125
|
|
|
} |
126
|
|
|
return $token; |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|