Commit fc16e61e1fcdf4347f4a9883232bbcf1cacb5214
1 parent
b75885cd
完善 token 过期认证
Showing
2 changed files
with
45 additions
and
11 deletions
src/Api/User.php
@@ -43,7 +43,8 @@ class User | @@ -43,7 +43,8 @@ class User | ||
43 | 'email' => '' | 43 | 'email' => '' |
44 | ], $params); | 44 | ], $params); |
45 | 45 | ||
46 | - return $this->service->apiRequest('get', 'employeeList', $params); | 46 | + $result = $this->service->apiRequest('get', 'employeeList', $params); |
47 | + return isset($result['employeeList']) ? $result['employeeList'] : (isset($result['data']) ? $result['data'] : []); | ||
47 | } | 48 | } |
48 | 49 | ||
49 | /** | 50 | /** |
@@ -101,6 +102,7 @@ class User | @@ -101,6 +102,7 @@ class User | ||
101 | private function _fieldCheck(array $params) : bool | 102 | private function _fieldCheck(array $params) : bool |
102 | { | 103 | { |
103 | // 必须提供的参数 | 104 | // 必须提供的参数 |
105 | + /* | ||
104 | $requires = [ | 106 | $requires = [ |
105 | 'nameZH', // *员工姓名(中文) | 107 | 'nameZH', // *员工姓名(中文) |
106 | 'nameEN', // *员工姓名(英文) | 108 | 'nameEN', // *员工姓名(英文) |
@@ -131,6 +133,7 @@ class User | @@ -131,6 +133,7 @@ class User | ||
131 | throw new \Exception('Add user lose params "'.$_key.'"', 91001); | 133 | throw new \Exception('Add user lose params "'.$_key.'"', 91001); |
132 | } | 134 | } |
133 | } | 135 | } |
136 | + */ | ||
134 | 137 | ||
135 | return true; | 138 | return true; |
136 | } | 139 | } |
src/Hris.php
@@ -114,7 +114,7 @@ class Hris | @@ -114,7 +114,7 @@ class Hris | ||
114 | public function getTokenInfo(bool $force = false) : array | 114 | public function getTokenInfo(bool $force = false) : array |
115 | { | 115 | { |
116 | $token = $this->_cache->getItem('token'); | 116 | $token = $this->_cache->getItem('token'); |
117 | - if ($token->isHit() || $force !== false) { | 117 | + if ($token->isHit() && $force === false) { |
118 | return $token->get(); | 118 | return $token->get(); |
119 | } | 119 | } |
120 | 120 | ||
@@ -126,13 +126,17 @@ class Hris | @@ -126,13 +126,17 @@ class Hris | ||
126 | 'token' => $this->_originToken | 126 | 'token' => $this->_originToken |
127 | ]; | 127 | ]; |
128 | // 请求接口 | 128 | // 请求接口 |
129 | - $this->_http->headers([ | 129 | + $headers = [ |
130 | 'Authorization' => $this->_originToken, | 130 | 'Authorization' => $this->_originToken, |
131 | 'Content-Type' => 'application/json;charset=utf-8' | 131 | 'Content-Type' => 'application/json;charset=utf-8' |
132 | - ]); | 132 | + ]; |
133 | + $this->_http->headers($headers); | ||
134 | + $this->log($url, 'Get Token'); | ||
135 | + $this->log($params, 'Request'); | ||
136 | + $this->log($headers, 'Header'); | ||
133 | $response = $this->_http->post($url, $params, 'json'); | 137 | $response = $this->_http->post($url, $params, 'json'); |
134 | $newTokenInfo = $response->json(true); | 138 | $newTokenInfo = $response->json(true); |
135 | - | 139 | + $this->log($newTokenInfo, 'Result'); |
136 | if (empty($newTokenInfo) || !isset($newTokenInfo['code'])) { | 140 | if (empty($newTokenInfo) || !isset($newTokenInfo['code'])) { |
137 | throw new \Exception('request hris token error' . var_export($newTokenInfo, true) . '|' . $url . var_export($params, true), 9001); | 141 | throw new \Exception('request hris token error' . var_export($newTokenInfo, true) . '|' . $url . var_export($params, true), 9001); |
138 | } | 142 | } |
@@ -148,7 +152,8 @@ class Hris | @@ -148,7 +152,8 @@ class Hris | ||
148 | } else { | 152 | } else { |
149 | $expire = $this->_tokenExpire; | 153 | $expire = $this->_tokenExpire; |
150 | } | 154 | } |
151 | - $token->expiresAfter($expire - 60 * 5); | 155 | + $token->expiresAfter($expire - 10); |
156 | + $this->log($token->get(), 'Save Token'); | ||
152 | $this->_cache->save($token); | 157 | $this->_cache->save($token); |
153 | 158 | ||
154 | return $newTokenInfo; | 159 | return $newTokenInfo; |
@@ -173,12 +178,18 @@ class Hris | @@ -173,12 +178,18 @@ class Hris | ||
173 | if ($tryCount > 3) { | 178 | if ($tryCount > 3) { |
174 | throw new \Exception('hris SDK Error: Get token error, retry maximum number.'); | 179 | throw new \Exception('hris SDK Error: Get token error, retry maximum number.'); |
175 | } | 180 | } |
176 | - $this->_http->headers([ | 181 | + $headers = [ |
177 | 'Content-Type' => 'application/json;charset=utf-8', | 182 | 'Content-Type' => 'application/json;charset=utf-8', |
178 | 'Authorization' => $this->getTokenInfo($flushToken)['data']['token'] | 183 | 'Authorization' => $this->getTokenInfo($flushToken)['data']['token'] |
179 | - ]); | 184 | + ]; |
185 | + $this->_http->headers($headers); | ||
180 | $url = $this->_baseUrl . $path; | 186 | $url = $this->_baseUrl . $path; |
181 | $method = strtolower($method); | 187 | $method = strtolower($method); |
188 | + | ||
189 | + $this->log($url, 'Url'); | ||
190 | + $this->log($method, 'Method'); | ||
191 | + $this->log($headers, 'Header'); | ||
192 | + $this->log($data, 'Request'); | ||
182 | if ($method == 'post') { | 193 | if ($method == 'post') { |
183 | $dataString = json_encode($data, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE); | 194 | $dataString = json_encode($data, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE); |
184 | $response = $this->_http->post($url, $dataString, 'json'); | 195 | $response = $this->_http->post($url, $dataString, 'json'); |
@@ -186,14 +197,34 @@ class Hris | @@ -186,14 +197,34 @@ class Hris | ||
186 | $response = $this->_http->get($url, $data); | 197 | $response = $this->_http->get($url, $data); |
187 | } | 198 | } |
188 | $result = $response->json(true); | 199 | $result = $response->json(true); |
189 | - if (!isset($result['code']) || $result['code'] != 0) { | ||
190 | - if ($result['code'] == 3001) { | 200 | + $this->log($result, 'Result'); |
201 | + if (!isset($result['code']) || $result['code'] != 200) { | ||
202 | + if (isset($result['code']) && ($result['code'] == '3001' || $result['code'] == '30010')) { | ||
191 | // token 无效,尝试重新刷新 token | 203 | // token 无效,尝试重新刷新 token |
204 | + $this->log($tryCount, 'Try count'); | ||
192 | return $this->apiRequest($method, $path, $data, true); | 205 | return $this->apiRequest($method, $path, $data, true); |
193 | } | 206 | } |
194 | - throw new \Exception('hris SDK Error: ' . $result['msg'] . ':' . $result['code'], $result['code']); | 207 | + if (isset($result['msg']) && isset($result['code'])) { |
208 | + throw new \Exception('hris SDK Error: ' . $result['msg'] . ':' . $result['code'] . '='. print_r($result, true), $result['code']); | ||
209 | + } else { | ||
210 | + throw new \Exception('hris SDK Error: Result NULL.' .var_export($result, true), 9999); | ||
211 | + } | ||
195 | } | 212 | } |
196 | 213 | ||
214 | + | ||
197 | return isset($result['data']) ? $result['data'] : []; | 215 | return isset($result['data']) ? $result['data'] : []; |
198 | } | 216 | } |
217 | + | ||
218 | + /** | ||
219 | + * 打印调试日志 | ||
220 | + * @param mixed $info | ||
221 | + */ | ||
222 | + public function log($info, $title = '', $level = 'info') | ||
223 | + { | ||
224 | + if ($this->_debug) { | ||
225 | + $info = is_scalar($info) ? $info : json_encode($info, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE); | ||
226 | + $title = $title ? "[{$title}]" : ''; | ||
227 | + \think\facade\Log::write($title . $info, 'info'); | ||
228 | + } | ||
229 | + } | ||
199 | } | 230 | } |