-
Notifications
You must be signed in to change notification settings - Fork 3
/
ServiceWork.php
286 lines (235 loc) · 7.88 KB
/
ServiceWork.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
<?php
namespace dovechen\yii2\weWork;
use dovechen\yii2\weWork\components\Utils;
use dovechen\yii2\weWork\src\dataStructure\UserDetailByUserTicket;
use dovechen\yii2\weWork\src\dataStructure\UserInfoByCode;
use yii\base\InvalidParamException;
class ServiceWork extends Work
{
/**
* 为应用的唯一身份标识
* @var string
*/
public $suite_id;
/**
* 为对应的调用身份密钥。
* @var string
*/
public $suite_secret;
/**
* 企业微信服务器会定时(每十分钟)推送ticket。ticket会实时变更,并用于后续接口的调用。
* @var string
*/
public $suite_ticket;
/**
* 第三方应用的token
* @var string
*/
public $suite_access_token;
/**
* 凭证的有效时间(秒)
* @var string
*/
public $suite_access_token_expire;
/**
* 授权方corpid
* @var string
*/
public $auth_corpid;
/**
* 预授权码
* @var string
*/
public $pre_auth_code;
/**
* 预授权码有效期
* @var string
*/
public $pre_auth_code_expires;
/**
* 永久授权码,通过get_permanent_code获取
* @var string
*/
public $permanent_code;
/**
* 数据缓存前缀
* @var string
*/
protected $cachePrefix = 'cache_work_suite';
/**
* @throws \ParameterError
*/
public function init ()
{
Utils::checkNotEmptyStr($this->suite_id, 'suite_id');
Utils::checkNotEmptyStr($this->suite_secret, 'suite_secret');
Utils::checkNotEmptyStr($this->suite_ticket, 'suite_ticket');
}
/**
* 获取缓存键值
*
* @param $name
*
* @return string
*/
protected function getCacheKey ($name)
{
return $this->cachePrefix . '_' . $this->suite_id . '_' . $this->auth_corpid . '_' . $name;
}
protected function RefreshAccessToken ()
{
if (!Utils::notEmptyStr($this->auth_corpid) || !Utils::notEmptyStr($this->permanent_code)) {
throw new \ParameterError("invalid auth_corpid or permanent_code");
}
$time = time();
$this->_HttpCall(self::SERVICE_GET_CORP_TOKEN, 'POST', ['auth_corpid' => $this->auth_corpid, 'permanent_code' => $this->permanent_code]);
$this->repJson['expire'] = $time + $this->repJson["expires_in"];
$this->setCache('access_token', $this->repJson, $this->repJson['expires_in']);
return $this->repJson;
}
public function GetSuiteAccessToken ($force = false)
{
$time = time();
if (!Utils::notEmptyStr($this->suite_access_token) || $this->suite_access_token_expire < $time || $force) {
$result = !Utils::notEmptyStr($this->suite_access_token) && !$force ? $this->getCache("suite_access_token", false) : false;
if ($result === false) {
$result = $this->RefreshSuiteAccessToken();
} else {
if ($result['expire'] < $time) {
$result = $this->RefreshSuiteAccessToken();
}
}
$this->SetSuiteAccessToken($result);
}
return $this->suite_access_token;
}
protected function RefreshSuiteAccessToken ()
{
if (!Utils::notEmptyStr($this->suite_id) || !Utils::notEmptyStr($this->suite_secret) || !Utils::notEmptyStr($this->suite_ticket)) {
throw new \ParameterError("invalid suite_id or suite_secret or suite_ticket");
}
$time = time();
$this->_HttpCall(self::SERVICE_GET_SUITE_TOKEN, 'POST', ['suite_id' => $this->suite_id, 'suite_secret' => $this->suite_secret, 'suite_ticket' => $this->suite_ticket]);
$this->repJson['expire'] = $time + $this->repJson["expires_in"];
$this->setCache('suite_access_token', $this->repJson, $this->repJson['expires_in']);
return $this->repJson;
}
public function SetSuiteAccessToken (array $suiteAccessToken)
{
if (!isset($suiteAccessToken['suite_access_token'])) {
throw new InvalidParamException('The suite suite_access_token must be set.');
} elseif (!isset($suiteAccessToken['expire'])) {
throw new InvalidParamException('Suite suite_access_token expire time must be set.');
}
$this->suite_access_token = $suiteAccessToken['suite_access_token'];
$this->suite_access_token_expire = $suiteAccessToken['expire'];
}
protected function GetAgentOauth2Url ($appid, $redirectUri, $state, $scope = self::SNSAPI_BASE, $agentId = '')
{
if ($scope == self::SNSAPI_BASE) {
return $this->GetOauth2Url($appid, $redirectUri, $state, $scope);
} else {
return "https://open.weixin.qq.com/connect/oauth2/authorize?appid={$appid}&redirect_uri={$redirectUri}&response_type=code&scope={$scope}&agentid={$agentId}&state={$state}#wechat_redirect";
}
}
public function getAuthUrl ($redirectUri, $state, $authType = 0, $force = false)
{
$time = time();
if (!Utils::notEmptyStr($this->pre_auth_code) || $this->pre_auth_code_expires < $time || $force) {
$result = !Utils::notEmptyStr($this->pre_auth_code) && !$force ? $this->getCache("pre_auth_code", false) : false;
if ($result === false) {
$result = $this->getPreAuthCode();
} else {
if ($result['expire'] < $time) {
$result = $this->getPreAuthCode();
}
}
$this->setPreAuthCode($result);
}
if ($authType != 0) {
$this->setSessionInfo($this->pre_auth_code, ['auth_type' => 1]);
}
return "https://open.work.weixin.qq.com/3rdapp/install?suite_id={$this->suite_id}&pre_auth_code={$this->pre_auth_code}&redirect_uri={$redirectUri}&state={$state}";
}
public function getPreAuthCode ()
{
$time = time();
self::_HttpCall(self::SERVICE_GET_PRE_AUTH_CODE);
$this->repJson['expire'] = $time + $this->repJson["expires_in"];
$this->setCache('pre_auth_code', $this->repJson, $this->repJson['expires_in']);
return $this->repJson;
}
public function setPreAuthCode (array $preAuthCode)
{
if (!isset($preAuthCode['pre_auth_code'])) {
throw new InvalidParamException('The suite pre_auth_code must be set.');
} elseif (!isset($preAuthCode['expire'])) {
throw new InvalidParamException('Suite pre_auth_code expire time must be set.');
}
$this->pre_auth_code = $preAuthCode['pre_auth_code'];
$this->pre_auth_code_expires = $preAuthCode['expire'];
}
public function setSessionInfo ($preAuthCode, $sessionInfo)
{
$args = [
'pre_auth_code' => $preAuthCode,
'session_info' => $sessionInfo,
];
self::_HttpCall(self::SERVICE_SET_SESSION_INFO, 'POST', $args);
return $this->repJson;
}
public function getPermanentCode ($authCode)
{
self::_HttpCall(self::SERVICE_GET_PERMANENT_CODE, 'POST', ['auth_code' => $authCode]);
return $this->repJson;
}
public function getAuthInfo ($authCorpId, $permanentCode)
{
$args = [
'auth_corpid' => $authCorpId,
'permanent_code' => $permanentCode,
];
self::_HttpCall(self::SERVICE_GET_AUTH_INFO, 'POST', $args);
return $this->repJson;
}
public function getAdminList ($authCorpId, $agentId)
{
$args = [
'auth_corpid' => $authCorpId,
'agentid' => $agentId,
];
self::_HttpCall(self::SERVICE_GET_ADMIN_LIST, 'POST', $args);
return $this->repJson;
}
public function getUserInfo3rd ($code)
{
Utils::checkNotEmptyStr($code, "code");
self::_HttpCall(self::SERVICE_GET_USER_INFO, 'GET', ['code' => $code]);
return UserInfoByCode::parseFromArray($this->repJson);
}
public function getUserDetail3rd ($userTicket)
{
Utils::checkNotEmptyStr($userTicket, "user_ticket");
self::_HttpCall(self::SERVICE_GET_USER_DETIAL, 'GET', ['user_ticket' => $userTicket]);
return UserDetailByUserTicket::parseFromArray($this->repJson);
}
public function unionidToExternalUserid3rd ($unionid, $openid, $corpid = '')
{
Utils::checkNotEmptyStr($unionid, "unionid");
Utils::checkNotEmptyStr($openid, "openid");
$args = [
'unionid' => $unionid,
'openid' => $openid,
];
if (!empty($corpid)) {
$args['corpid'] = $corpid;
}
self::_HttpCall(self::UNIONID_TO_EXTERNAL_USERID_3RD, 'POST', $args);
return $this->repJson;
}
public function GetSuiteJsapiTicket ()
{
self::_HttpCall(self::GET_SUITE_JSAPI_TICKET);
return $this->repJson;
}
}