Redis.php 5.1 KB

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