Redis.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. namespace app\common\library\token\driver;
  3. use Throwable;
  4. use think\Response;
  5. use BadFunctionCallException;
  6. use app\common\library\token\Driver;
  7. use think\exception\HttpResponseException;
  8. /**
  9. * @see Driver
  10. */
  11. class Redis extends Driver
  12. {
  13. /**
  14. * 默认配置
  15. * @var array
  16. */
  17. protected array $options = [];
  18. /**
  19. * 构造函数
  20. * @access public
  21. * @param array $options 参数
  22. * @throws Throwable
  23. */
  24. public function __construct(array $options = [])
  25. {
  26. if (!extension_loaded('redis')) {
  27. throw new BadFunctionCallException('未安装redis扩展');
  28. }
  29. if (!empty($options)) {
  30. $this->options = array_merge($this->options, $options);
  31. }
  32. $this->handler = new \Redis;
  33. if ($this->options['persistent']) {
  34. $this->handler->pconnect($this->options['host'], $this->options['port'], $this->options['timeout'], 'persistent_id_' . $this->options['select']);
  35. } else {
  36. $this->handler->connect($this->options['host'], $this->options['port'], $this->options['timeout']);
  37. }
  38. if ('' != $this->options['password']) {
  39. $this->handler->auth($this->options['password']);
  40. }
  41. if (false !== $this->options['select']) {
  42. $this->handler->select($this->options['select']);
  43. }
  44. }
  45. /**
  46. * @throws Throwable
  47. */
  48. public function set(string $token, string $type, int $user_id, int $expire = null): bool
  49. {
  50. if (is_null($expire)) {
  51. $expire = $this->options['expire'];
  52. }
  53. $expireTime = $expire !== 0 ? time() + $expire : 0;
  54. $token = $this->getEncryptedToken($token);
  55. $tokenInfo = [
  56. 'token' => $token,
  57. 'type' => $type,
  58. 'user_id' => $user_id,
  59. 'create_time' => time(),
  60. 'expire_time' => $expireTime,
  61. ];
  62. $tokenInfo = json_encode($tokenInfo, JSON_UNESCAPED_UNICODE);
  63. if ($expire) {
  64. if ($type == 'admin' || $type == 'user') {
  65. // 增加 redis中的 token 过期时间,以免 token 过期自动刷新永远无法触发
  66. $expire *= 2;
  67. }
  68. $result = $this->handler->setex($token, $expire, $tokenInfo);
  69. } else {
  70. $result = $this->handler->set($token, $tokenInfo);
  71. }
  72. $this->handler->sAdd($this->getUserKey($user_id), $token);
  73. return $result;
  74. }
  75. /**
  76. * @throws Throwable
  77. */
  78. public function get(string $token, bool $expirationException = true): array
  79. {
  80. $key = $this->getEncryptedToken($token);
  81. $data = $this->handler->get($key);
  82. if (is_null($data) || false === $data) {
  83. return [];
  84. }
  85. $data = json_decode($data, true);
  86. // 返回未加密的token给客户端使用
  87. $data['token'] = $token;
  88. // 过期时间
  89. $data['expires_in'] = $this->getExpiredIn($data['expire_time'] ?? 0);
  90. if ($data['expire_time'] && $data['expire_time'] <= time() && $expirationException) {
  91. // token过期-触发前端刷新token
  92. $response = Response::create(['code' => 409, 'msg' => __('Token expiration'), 'data' => $data], 'json');
  93. throw new HttpResponseException($response);
  94. }
  95. return $data;
  96. }
  97. /**
  98. * @throws Throwable
  99. */
  100. public function check(string $token, string $type, int $user_id, bool $expirationException = true): bool
  101. {
  102. $data = $this->get($token, $expirationException);
  103. if (!$data || (!$expirationException && $data['expire_time'] && $data['expire_time'] <= time())) return false;
  104. return $data['type'] == $type && $data['user_id'] == $user_id;
  105. }
  106. /**
  107. * @throws Throwable
  108. */
  109. public function delete(string $token): bool
  110. {
  111. $data = $this->get($token, false);
  112. if ($data) {
  113. $key = $this->getEncryptedToken($token);
  114. $user_id = $data['user_id'];
  115. $this->handler->del($key);
  116. $this->handler->sRem($this->getUserKey($user_id), $key);
  117. }
  118. return true;
  119. }
  120. /**
  121. * @throws Throwable
  122. */
  123. public function clear(string $type, int $user_id): bool
  124. {
  125. $keys = $this->handler->sMembers($this->getUserKey($user_id));
  126. $this->handler->del($this->getUserKey($user_id));
  127. $this->handler->del($keys);
  128. return true;
  129. }
  130. /**
  131. * 获取会员的key
  132. * @param $user_id
  133. * @return string
  134. */
  135. protected function getUserKey($user_id): string
  136. {
  137. return $this->options['userprefix'] . $user_id;
  138. }
  139. }