Redis.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <?php
  2. namespace app\common\library\token\driver;
  3. use app\common\library\token\Driver;
  4. /**
  5. * Token操作类
  6. */
  7. class Redis extends Driver
  8. {
  9. protected $options = [
  10. 'host' => '127.0.0.1',
  11. 'port' => 6379,
  12. 'password' => '',
  13. 'select' => 0,
  14. 'timeout' => 0,
  15. 'expire' => 0,
  16. 'persistent' => false,
  17. 'userprefix' => 'up:',
  18. 'tokenprefix' => 'tp:',
  19. ];
  20. /**
  21. * 构造函数
  22. * @param array $options 缓存参数
  23. * @throws \BadFunctionCallException
  24. * @access public
  25. */
  26. public function __construct($options = [])
  27. {
  28. if (!extension_loaded('redis')) {
  29. throw new \BadFunctionCallException('not support: redis');
  30. }
  31. if (!empty($options)) {
  32. $this->options = array_merge($this->options, $options);
  33. }
  34. $this->handler = new \Redis;
  35. if ($this->options['persistent']) {
  36. $this->handler->pconnect($this->options['host'], $this->options['port'], $this->options['timeout'], 'persistent_id_' . $this->options['select']);
  37. } else {
  38. $this->handler->connect($this->options['host'], $this->options['port'], $this->options['timeout']);
  39. }
  40. if ('' != $this->options['password']) {
  41. $this->handler->auth($this->options['password']);
  42. }
  43. if (0 != $this->options['select']) {
  44. $this->handler->select($this->options['select']);
  45. }
  46. }
  47. /**
  48. * 获取加密后的Token
  49. * @param string $token Token标识
  50. * @return string
  51. */
  52. protected function getEncryptedToken($token)
  53. {
  54. $config = \think\Config::get('token');
  55. $token = $token ?? ''; // 为兼容 php8
  56. return $this->options['tokenprefix'] . hash_hmac($config['hashalgo'], $token, $config['key']);
  57. }
  58. /**
  59. * 获取会员的key
  60. * @param $user_id
  61. * @return string
  62. */
  63. protected function getUserKey($user_id)
  64. {
  65. return $this->options['userprefix'] . $user_id;
  66. }
  67. /**
  68. * 存储Token
  69. * @param string $token Token
  70. * @param int $user_id 会员ID
  71. * @param int $expire 过期时长,0表示无限,单位秒
  72. * @return bool
  73. */
  74. public function set($token, $user_id, $expire = 0)
  75. {
  76. if (is_null($expire)) {
  77. $expire = $this->options['expire'];
  78. }
  79. if ($expire instanceof \DateTime) {
  80. $expire = $expire->getTimestamp() - time();
  81. }
  82. $key = $this->getEncryptedToken($token);
  83. if ($expire) {
  84. $result = $this->handler->setex($key, $expire, $user_id);
  85. } else {
  86. $result = $this->handler->set($key, $user_id);
  87. }
  88. //写入会员关联的token
  89. $this->handler->sAdd($this->getUserKey($user_id), $key);
  90. return $result;
  91. }
  92. /**
  93. * 获取Token内的信息
  94. * @param string $token
  95. * @return array
  96. */
  97. public function get($token)
  98. {
  99. $key = $this->getEncryptedToken($token);
  100. $value = $this->handler->get($key);
  101. if (is_null($value) || false === $value) {
  102. return [];
  103. }
  104. //获取有效期
  105. $expire = $this->handler->ttl($key);
  106. $expire = $expire < 0 ? 365 * 86400 : $expire;
  107. $expiretime = time() + $expire;
  108. //解决使用redis方式储存token时api接口Token刷新与检测因expires_in拼写错误报错的BUG
  109. $result = ['token' => $token, 'user_id' => $value, 'expiretime' => $expiretime, 'expires_in' => $expire];
  110. return $result;
  111. }
  112. /**
  113. * 判断Token是否可用
  114. * @param string $token Token
  115. * @param int $user_id 会员ID
  116. * @return boolean
  117. */
  118. public function check($token, $user_id)
  119. {
  120. $data = self::get($token);
  121. return $data && $data['user_id'] == $user_id ? true : false;
  122. }
  123. /**
  124. * 删除Token
  125. * @param string $token
  126. * @return boolean
  127. */
  128. public function delete($token)
  129. {
  130. $data = $this->get($token);
  131. if ($data) {
  132. $key = $this->getEncryptedToken($token);
  133. $user_id = $data['user_id'];
  134. $this->handler->del($key);
  135. $this->handler->sRem($this->getUserKey($user_id), $key);
  136. }
  137. return true;
  138. }
  139. /**
  140. * 删除指定用户的所有Token
  141. * @param int $user_id
  142. * @return boolean
  143. */
  144. public function clear($user_id)
  145. {
  146. $keys = $this->handler->sMembers($this->getUserKey($user_id));
  147. $this->handler->del($this->getUserKey($user_id));
  148. $this->handler->del($keys);
  149. return true;
  150. }
  151. }