Redis.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. namespace App\Utils\Encrypt\Token\Driver;
  3. use App\Utils\Encrypt\Token\Driver;
  4. use Hyperf\Config\Annotation\Value;
  5. use Hyperf\Context\ApplicationContext;
  6. /**
  7. * Token操作类
  8. */
  9. class Redis extends Driver
  10. {
  11. /**
  12. * 注解
  13. * 设置redis前缀与框架保持一致
  14. *
  15. * @var string
  16. */
  17. #[Value("token.default")]
  18. protected array $config = [];
  19. protected array $options = [
  20. 'expire' => 2592000,
  21. 'userprefix' => 'up:',
  22. 'tokenprefix' => 'tp:',
  23. ];
  24. /**
  25. * 构造函数
  26. * @throws \Psr\Container\ContainerExceptionInterface
  27. * @throws \Psr\Container\NotFoundExceptionInterface
  28. */
  29. public function __construct()
  30. {
  31. $container = ApplicationContext::getContainer();
  32. $this->handler = $container->get(\Redis::class);
  33. }
  34. /**
  35. * 获取加密后的Token
  36. * @param string $token Token标识
  37. * @return string
  38. */
  39. protected function getEncryptedToken(string $token): string
  40. {
  41. $config = $this->config;
  42. return $this->options['tokenprefix'] . hash_hmac($config['hash_algo'], $token, $config['key']);
  43. }
  44. /**
  45. * 获取会员的key
  46. * @param $user_id
  47. * @return string
  48. */
  49. protected function getUserKey($user_id): string
  50. {
  51. return $this->options['userprefix'] . $user_id;
  52. }
  53. /**
  54. * 存储Token
  55. * @param string $token Token
  56. * @param int $user_id 会员ID
  57. * @param int $expire 过期时长,0表示无限,单位秒
  58. * @return bool
  59. */
  60. public function set(string $token, int $user_id, int $expire = 0): bool
  61. {
  62. if (is_null($expire)) {
  63. $expire = $this->options['expire'];
  64. }
  65. if ($expire instanceof \DateTime) {
  66. $expire = $expire->getTimestamp() - time();
  67. }
  68. $key = $this->getEncryptedToken($token);
  69. if ($expire) {
  70. $result = $this->handler->setex($key, $expire, $user_id);
  71. } else {
  72. $result = $this->handler->set($key, $user_id);
  73. }
  74. //写入会员关联的token
  75. $this->handler->sAdd($this->getUserKey($user_id), $key);
  76. return $result;
  77. }
  78. /**
  79. * 获取Token内的信息
  80. * @param string $token
  81. * @return array
  82. */
  83. public function get(string $token): array
  84. {
  85. $key = $this->getEncryptedToken($token);
  86. $value = $this->handler->get($key);
  87. if (is_null($value) || false === $value) {
  88. return [];
  89. }
  90. //获取有效期
  91. $expire = $this->handler->ttl($key);
  92. $expire = $expire < 0 ? 365 * 86400 : $expire;
  93. $expireTime = time() + $expire;
  94. //解决使用redis方式储存token时api接口Token刷新与检测因expires_in拼写错误报错的BUG
  95. return ['token' => $token, 'user_id' => $value, 'expiretime' => $expireTime, 'expires_in' => $expire];
  96. }
  97. /**
  98. * 判断Token是否可用
  99. * @param string $token Token
  100. * @param int $user_id 会员ID
  101. * @return boolean
  102. */
  103. public function check(string $token, int $user_id): bool
  104. {
  105. $data = self::get($token);
  106. return $data && $data['user_id'] == $user_id;
  107. }
  108. /**
  109. * 删除Token
  110. * @param string $token
  111. * @return boolean
  112. */
  113. public function delete(string $token): bool
  114. {
  115. $data = $this->get($token);
  116. if ($data) {
  117. $key = $this->getEncryptedToken($token);
  118. $user_id = $data['user_id'];
  119. $this->handler->del($key);
  120. $this->handler->sRem($this->getUserKey($user_id), $key);
  121. }
  122. return true;
  123. }
  124. /**
  125. * 删除指定用户的所有Token
  126. * @param int $user_id
  127. * @return boolean
  128. */
  129. public function clear(int $user_id): bool
  130. {
  131. $keys = $this->handler->sMembers($this->getUserKey($user_id));
  132. $this->handler->del($this->getUserKey($user_id));
  133. $this->handler->del($keys);
  134. return true;
  135. }
  136. }