Skip to content

Commit

Permalink
Token 失效刷新问题处理
Browse files Browse the repository at this point in the history
  • Loading branch information
itxiao6 committed Jan 19, 2021
1 parent 69eb855 commit 00f8f73
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 14 deletions.
20 changes: 20 additions & 0 deletions src/Authorization.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,24 @@ public function getTokenAsString(): string
}
throw new \Exception('个推Token 获取失败');
}

/**
* 刷新Token
* @return mixed
* @throws \GuzzleHttp\Exception\GuzzleException
*/
public function refurbishToken()
{
$key = 'cache:getui:token';
$res = (new HttpRequest())->withConfig($this->config)->withData([
])->withApi('/auth')->withMethod('POST')->send();
if(isset($res['code']) && $res['code'] === 0){
$expire_time = intval($res['data']['expire_time']/1000);
$token = $res['data']['token'];
if($this->cache instanceof Cache){
$this->cache->save($key,$token,$expire_time-time());
}
return $token;
}
}
}
37 changes: 23 additions & 14 deletions src/HttpRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ class HttpRequest
* @var string
*/
protected $api;
/**
* @var Authorization
*/
protected $authorization;

/**
* HttpRequest constructor.
Expand Down Expand Up @@ -78,10 +82,11 @@ public function withData(array $data): self
* 设置Token
* @param Authorization $authorization
* @return $this
* @throws \GuzzleHttp\Exception\GuzzleException
*/
public function withToken(Authorization $authorization)
{
$this->headers['token'] = $authorization->getTokenAsString();
$this->authorization = $authorization;
return $this;
}

Expand Down Expand Up @@ -113,19 +118,23 @@ public function withApi(string $api)
*/
public function send()
{
$this->data['appkey'] = $this->config->getAppKey();
$this->data['timestamp'] = $this->micro_time();
$this->data['sign'] = hash("sha256", "{$this->config->getAppKey()}{$this->data['timestamp']}{$this->config->getMasterSecret()}");
// var_dump($this->data);exit();
// var_dump($this->method, $this->gateway . $this->config->getAppId() . $this->api);
// var_dump([
// 'headers' => $this->headers,
// 'body' => json_encode($this->data)
// ]);
return json_decode($this->client->request($this->method, $this->gateway . $this->config->getAppId() . $this->api, [
'headers' => $this->headers,
'body' => json_encode($this->data)
])->getBody()->getContents(), 1);
try{
$this->data['appkey'] = $this->config->getAppKey();
$this->data['timestamp'] = $this->micro_time();
$this->data['sign'] = hash("sha256", "{$this->config->getAppKey()}{$this->data['timestamp']}{$this->config->getMasterSecret()}");
$this->headers['token'] = $this->authorization->getTokenAsString();
return json_decode($this->client->request($this->method, $this->gateway . $this->config->getAppId() . $this->api, [
'headers' => $this->headers,
'body' => json_encode($this->data)
])->getBody()->getContents(), 1);
}catch (\GuzzleHttp\Exception\ClientException $exception){
$data = json_encode($exception->getResponse()->getBody()->getContents(),1);
if($exception->getResponse()->getStatusCode() == 400 && $data['code'] == 20001){
$this->authorization->refurbishToken();
$this->send();
}
throw $exception;
}
}

private function micro_time()
Expand Down

0 comments on commit 00f8f73

Please sign in to comment.