Token.php 1007 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  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. /**
  13. * 检测Token是否过期
  14. *
  15. */
  16. public function check()
  17. {
  18. $token = $this->auth->getToken();
  19. $tokenInfo = \app\common\library\Token::get($token);
  20. $this->success('', ['token' => $tokenInfo['token'], 'expires_in' => $tokenInfo['expires_in']]);
  21. }
  22. /**
  23. * 刷新Token
  24. *
  25. */
  26. public function refresh()
  27. {
  28. //删除源Token
  29. $token = $this->auth->getToken();
  30. \app\common\library\Token::delete($token);
  31. //创建新Token
  32. $token = Random::uuid();
  33. \app\common\library\Token::set($token, $this->auth->id, 2592000);
  34. $tokenInfo = \app\common\library\Token::get($token);
  35. $this->success('', ['token' => $tokenInfo['token'], 'expires_in' => $tokenInfo['expires_in']]);
  36. }
  37. }