1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* 企业微信开放平台 https://work.weixin.qq.com/api/doc/ |
4
|
|
|
* api接口文档: https://work.weixin.qq.com/api/doc/90000/90135/91022 |
5
|
|
|
*/ |
6
|
|
|
namespace tinymeng\OAuth2\Gateways; |
7
|
|
|
|
8
|
|
|
use tinymeng\OAuth2\Connector\Gateway; |
9
|
|
|
use tinymeng\OAuth2\Exception\OAuthException; |
10
|
|
|
use tinymeng\OAuth2\Helper\ConstCode; |
11
|
|
|
|
12
|
|
|
class Wecom extends Gateway |
13
|
|
|
{ |
14
|
|
|
const API_BASE = 'https://qyapi.weixin.qq.com/'; |
15
|
|
|
protected $AuthorizeURL = 'https://open.work.weixin.qq.com/wwopen/sso/qrConnect'; |
16
|
|
|
protected $AccessTokenURL = 'https://qyapi.weixin.qq.com/cgi-bin/gettoken'; |
17
|
|
|
protected $UserInfoURL = 'https://qyapi.weixin.qq.com/cgi-bin/user/getuserinfo'; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* 得到跳转地址 |
21
|
|
|
*/ |
22
|
|
|
public function getRedirectUrl() |
23
|
|
|
{ |
24
|
|
|
$params = [ |
25
|
|
|
'appid' => $this->config['app_id'], |
26
|
|
|
'agentid' => $this->config['agent_id'], |
27
|
|
|
'redirect_uri' => $this->config['callback'], |
28
|
|
|
'state' => $this->config['state'] ?: '', |
29
|
|
|
]; |
30
|
|
|
return $this->AuthorizeURL . '?' . http_build_query($params); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* 获取当前授权用户的openid标识 |
35
|
|
|
*/ |
36
|
|
|
public function openid() |
37
|
|
|
{ |
38
|
|
|
$result = $this->getUserInfo(); |
39
|
|
|
return $result['userid'] ?? ''; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* 获取格式化后的用户信息 |
44
|
|
|
*/ |
45
|
|
|
public function userInfo() |
46
|
|
|
{ |
47
|
|
|
$result = $this->getUserInfo(); |
48
|
|
|
|
49
|
|
|
$userInfo = [ |
50
|
|
|
'open_id' => $result['userid'] ?? '', |
51
|
|
|
'union_id' => '', |
52
|
|
|
'channel' => ConstCode::TYPE_WECOM, |
53
|
|
|
'nickname' => $result['userid'] ?? '', |
54
|
|
|
'gender' => ConstCode::GENDER, |
55
|
|
|
'avatar' => '', |
56
|
|
|
'type' => ConstCode::getTypeConst(ConstCode::TYPE_WECOM, $this->type), |
57
|
|
|
'access_token' => $this->token['access_token'] ?? '', |
58
|
|
|
'native' => $result |
59
|
|
|
]; |
60
|
|
|
return $userInfo; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* 获取原始接口返回的用户信息 |
65
|
|
|
*/ |
66
|
|
|
public function getUserInfo() |
67
|
|
|
{ |
68
|
|
|
$this->getToken(); |
69
|
|
|
|
70
|
|
|
$params = [ |
71
|
|
|
'access_token' => $this->token['access_token'], |
72
|
|
|
'code' => isset($_REQUEST['code']) ? $_REQUEST['code'] : '', |
73
|
|
|
]; |
74
|
|
|
|
75
|
|
|
$data = $this->get($this->UserInfoURL, $params); |
76
|
|
|
$data = json_decode($data, true); |
77
|
|
|
|
78
|
|
|
if (!isset($data['userid'])) { |
79
|
|
|
throw new OAuthException('获取企业微信用户信息失败:' . json_encode($data)); |
80
|
|
|
} |
81
|
|
|
return $data; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* 获取access_token |
86
|
|
|
*/ |
87
|
|
|
protected function getAccessToken() |
88
|
|
|
{ |
89
|
|
|
$params = [ |
90
|
|
|
'corpid' => $this->config['app_id'], |
91
|
|
|
'corpsecret' => $this->config['app_secret'], |
92
|
|
|
]; |
93
|
|
|
|
94
|
|
|
$response = $this->get($this->AccessTokenURL, $params); |
95
|
|
|
$response = json_decode($response, true); |
96
|
|
|
|
97
|
|
|
if (!isset($response['access_token'])) { |
98
|
|
|
throw new OAuthException('获取企业微信 access_token 出错:' . json_encode($response)); |
99
|
|
|
} |
100
|
|
|
return $response; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* 检验授权凭证AccessToken是否有效 |
105
|
|
|
*/ |
106
|
|
|
public function validateAccessToken($accessToken = null) |
107
|
|
|
{ |
108
|
|
|
try { |
109
|
|
|
$accessToken = $accessToken ?? $this->token['access_token']; |
110
|
|
|
$params = [ |
111
|
|
|
'access_token' => $accessToken, |
112
|
|
|
'code' => 'fake_code' |
113
|
|
|
]; |
114
|
|
|
$data = $this->get($this->UserInfoURL, $params); |
115
|
|
|
$data = json_decode($data, true); |
116
|
|
|
return $data['errcode'] === 40029; // 无效的code但token有效 |
117
|
|
|
} catch (\Exception $e) { |
118
|
|
|
return false; |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* 刷新AccessToken续期 |
124
|
|
|
*/ |
125
|
|
|
public function refreshToken($refreshToken) |
126
|
|
|
{ |
127
|
|
|
return $this->getAccessToken(); |
128
|
|
|
} |
129
|
|
|
} |