Redis.php 4.4 KB

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