Token.php 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use fast\Random;
  5. /**
  6. * Token接口
  7. */
  8. class Token extends Api
  9. {
  10. protected $noNeedLogin = [];
  11. protected $noNeedRight = '*';
  12. public function _initialize()
  13. {
  14. parent::_initialize();
  15. }
  16. /**
  17. * 检测Token是否过期
  18. *
  19. */
  20. public function check()
  21. {
  22. $token = $this->auth->getToken();
  23. $tokenInfo = \app\common\library\Token::get($token);
  24. $this->success('', ['token' => $tokenInfo['token'], 'expires_in' => $tokenInfo['expires_in']]);
  25. }
  26. /**
  27. * 刷新Token
  28. *
  29. */
  30. public function refresh()
  31. {
  32. //删除源Token
  33. $token = $this->auth->getToken();
  34. \app\common\library\Token::delete($token);
  35. //创建新Token
  36. $token = Random::uuid();
  37. \app\common\library\Token::set($token, $this->auth->id, 2592000);
  38. $tokenInfo = \app\common\library\Token::get($token);
  39. $this->success('', ['token' => $tokenInfo['token'], 'expires_in' => $tokenInfo['expires_in']]);
  40. }
  41. }