1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace EntWeChat\Auth; |
4
|
|
|
|
5
|
|
|
use EntWeChat\Core\Exceptions\InvalidStateException; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class App. |
9
|
|
|
*/ |
10
|
|
|
class App extends AbstractAuthentication |
11
|
|
|
{ |
12
|
|
|
const AUTH_URL = 'https://open.weixin.qq.com/connect/oauth2/authorize'; |
13
|
|
|
const API_GET_USER_INFO = 'https://qyapi.weixin.qq.com/cgi-bin/user/getuserinfo'; |
14
|
|
|
const API_GET_USER_DETAIL = 'https://qyapi.weixin.qq.com/cgi-bin/user/getuserdetail'; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Indicates if the session state should be utilized. |
18
|
|
|
* |
19
|
|
|
* @var bool |
20
|
|
|
*/ |
21
|
|
|
protected $stateless = true; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* {@inheritdoc}. |
25
|
|
|
*/ |
26
|
|
|
protected function getAuthUrl($state) |
27
|
|
|
{ |
28
|
|
|
return $this->buildAuthUrlFromBase(self::AUTH_URL, $state); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* {@inheritdoc}. |
33
|
|
|
*/ |
34
|
|
|
protected function buildAuthUrlFromBase($url, $state) |
35
|
|
|
{ |
36
|
|
|
$query = http_build_query($this->getCodeFields($state), '', '&', $this->encodingType); |
37
|
|
|
|
38
|
|
|
return $url.'?'.$query.'#wechat_redirect'; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* {@inheritdoc}. |
43
|
|
|
*/ |
44
|
|
|
protected function getCodeFields($state = null) |
45
|
|
|
{ |
46
|
|
|
return array_merge([ |
47
|
|
|
'appid' => $this->clientId, |
48
|
|
|
'redirect_uri' => $this->redirectUrl, |
49
|
|
|
'response_type' => 'code', |
50
|
|
|
'scope' => $this->formatScopes($this->scopes, $this->scopeSeparator), |
51
|
|
|
'state' => $state ?: md5(time()), |
52
|
|
|
], $this->parameters); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param null $code |
57
|
|
|
* |
58
|
|
|
* @throws InvalidStateException |
59
|
|
|
* |
60
|
|
|
* @return \EntWeChat\Support\Collection |
61
|
|
|
*/ |
62
|
|
View Code Duplication |
public function user($code = null) |
|
|
|
|
63
|
|
|
{ |
64
|
|
|
if ($this->hasInvalidState()) { |
65
|
|
|
throw new InvalidStateException(); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
$params = [ |
69
|
|
|
'code' => $code ?: $this->request->get('code'), |
70
|
|
|
]; |
71
|
|
|
|
72
|
|
|
return $this->parseJSON('get', [self::API_GET_USER_INFO, $params]); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param $user_ticket |
77
|
|
|
* |
78
|
|
|
* @return \EntWeChat\Support\Collection |
79
|
|
|
*/ |
80
|
|
|
public function detail($userTicket) |
81
|
|
|
{ |
82
|
|
|
$params = [ |
83
|
|
|
'user_ticket' => $userTicket, |
84
|
|
|
]; |
85
|
|
|
|
86
|
|
|
return $this->parseJSON('json', [self::API_GET_USER_DETAIL, $params]); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.