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 | 43 | 'email' => '' |
44 | 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 | 102 | private function _fieldCheck(array $params) : bool |
102 | 103 | { |
103 | 104 | // 必须提供的参数 |
105 | + /* | |
104 | 106 | $requires = [ |
105 | 107 | 'nameZH', // *员工姓名(中文) |
106 | 108 | 'nameEN', // *员工姓名(英文) |
... | ... | @@ -131,6 +133,7 @@ class User |
131 | 133 | throw new \Exception('Add user lose params "'.$_key.'"', 91001); |
132 | 134 | } |
133 | 135 | } |
136 | + */ | |
134 | 137 | |
135 | 138 | return true; |
136 | 139 | } | ... | ... |
src/Hris.php
... | ... | @@ -114,7 +114,7 @@ class Hris |
114 | 114 | public function getTokenInfo(bool $force = false) : array |
115 | 115 | { |
116 | 116 | $token = $this->_cache->getItem('token'); |
117 | - if ($token->isHit() || $force !== false) { | |
117 | + if ($token->isHit() && $force === false) { | |
118 | 118 | return $token->get(); |
119 | 119 | } |
120 | 120 | |
... | ... | @@ -126,13 +126,17 @@ class Hris |
126 | 126 | 'token' => $this->_originToken |
127 | 127 | ]; |
128 | 128 | // 请求接口 |
129 | - $this->_http->headers([ | |
129 | + $headers = [ | |
130 | 130 | 'Authorization' => $this->_originToken, |
131 | 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 | 137 | $response = $this->_http->post($url, $params, 'json'); |
134 | 138 | $newTokenInfo = $response->json(true); |
135 | - | |
139 | + $this->log($newTokenInfo, 'Result'); | |
136 | 140 | if (empty($newTokenInfo) || !isset($newTokenInfo['code'])) { |
137 | 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 | 152 | } else { |
149 | 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 | 157 | $this->_cache->save($token); |
153 | 158 | |
154 | 159 | return $newTokenInfo; |
... | ... | @@ -173,12 +178,18 @@ class Hris |
173 | 178 | if ($tryCount > 3) { |
174 | 179 | throw new \Exception('hris SDK Error: Get token error, retry maximum number.'); |
175 | 180 | } |
176 | - $this->_http->headers([ | |
181 | + $headers = [ | |
177 | 182 | 'Content-Type' => 'application/json;charset=utf-8', |
178 | 183 | 'Authorization' => $this->getTokenInfo($flushToken)['data']['token'] |
179 | - ]); | |
184 | + ]; | |
185 | + $this->_http->headers($headers); | |
180 | 186 | $url = $this->_baseUrl . $path; |
181 | 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 | 193 | if ($method == 'post') { |
183 | 194 | $dataString = json_encode($data, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE); |
184 | 195 | $response = $this->_http->post($url, $dataString, 'json'); |
... | ... | @@ -186,14 +197,34 @@ class Hris |
186 | 197 | $response = $this->_http->get($url, $data); |
187 | 198 | } |
188 | 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 | 203 | // token 无效,尝试重新刷新 token |
204 | + $this->log($tryCount, 'Try count'); | |
192 | 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 | 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 | } | ... | ... |