Redis.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. return $this->options['tokenprefix'] . hash_hmac($config['hashalgo'], $token, $config['key']);
  56. }
  57. /**
  58. * 获取会员的key
  59. * @param $user_id
  60. * @return string
  61. */
  62. protected function getUserKey($user_id)
  63. {
  64. return $this->options['userprefix'] . $user_id;
  65. }
  66. /**
  67. * 存储Token
  68. * @param string $token Token
  69. * @param int $user_id 会员ID
  70. * @param int $expire 过期时长,0表示无限,单位秒
  71. * @return bool
  72. */
  73. public function set($token, $user_id, $expire = 0)
  74. {
  75. if (is_null($expire)) {
  76. $expire = $this->options['expire'];
  77. }
  78. if ($expire instanceof \DateTime) {
  79. $expire = $expire->getTimestamp() - time();
  80. }
  81. $key = $this->getEncryptedToken($token);
  82. if ($expire) {
  83. $result = $this->handler->setex($key, $expire, $user_id);
  84. } else {
  85. $result = $this->handler->set($key, $user_id);
  86. }
  87. //写入会员关联的token
  88. $this->handler->sAdd($this->getUserKey($user_id), $key);
  89. return $result;
  90. }
  91. /**
  92. * 获取Token内的信息
  93. * @param string $token
  94. * @return array
  95. */
  96. public function get($token)
  97. {
  98. $key = $this->getEncryptedToken($token);
  99. $value = $this->handler->get($key);
  100. if (is_null($value) || false === $value) {
  101. return [];
  102. }
  103. //获取有效期
  104. $expire = $this->handler->ttl($key);
  105. $expire = $expire < 0 ? 365 * 86400 : $expire;
  106. $expiretime = time() + $expire;
  107. //解决使用redis方式储存token时api接口Token刷新与检测因expires_in拼写错误报错的BUG
  108. $result = ['token' => $token, 'user_id' => $value, 'expiretime' => $expiretime, 'expires_in' => $expire];
  109. return $result;
  110. }
  111. /**
  112. * 判断Token是否可用
  113. * @param string $token Token
  114. * @param int $user_id 会员ID
  115. * @return boolean
  116. */
  117. public function check($token, $user_id)
  118. {
  119. $data = self::get($token);
  120. return $data && $data['user_id'] == $user_id ? true : false;
  121. }
  122. /**
  123. * 删除Token
  124. * @param string $token
  125. * @return boolean
  126. */
  127. public function delete($token)
  128. {
  129. $data = $this->get($token);
  130. if ($data) {
  131. $key = $this->getEncryptedToken($token);
  132. $user_id = $data['user_id'];
  133. $this->handler->del($key);
  134. $this->handler->sRem($this->getUserKey($user_id), $key);
  135. }
  136. return true;
  137. }
  138. /**
  139. * 删除指定用户的所有Token
  140. * @param int $user_id
  141. * @return boolean
  142. */
  143. public function clear($user_id)
  144. {
  145. $keys = $this->handler->sMembers($this->getUserKey($user_id));
  146. $this->handler->del($this->getUserKey($user_id));
  147. $this->handler->del($keys);
  148. return true;
  149. }
  150. }