Redis.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. namespace app\common\library\token\driver;
  3. use app\common\library\token\Driver;
  4. use think\Config;
  5. /**
  6. * Token操作类
  7. */
  8. class Redis extends Driver
  9. {
  10. protected array $options = [
  11. 'host' => '127.0.0.1',
  12. 'port' => 6379,
  13. 'password' => '',
  14. 'select' => 0,
  15. 'timeout' => 0,
  16. 'persistent' => false,
  17. ];
  18. /**
  19. * 构造函数
  20. * @throws \BadFunctionCallException
  21. * @access public
  22. */
  23. public function __construct()
  24. {
  25. if (!extension_loaded('redis')) {
  26. throw new \BadFunctionCallException('not support: redis');
  27. }
  28. $this->options = array_merge($this->options, \think\Config::get('redis')); // 读取redis的配置信息
  29. $this->handler = new \Redis;
  30. if ($this->options['persistent']) {
  31. $this->handler->pconnect($this->options['host'], $this->options['port'], $this->options['timeout'], 'persistent_id_' . $this->options['select']);
  32. } else {
  33. $this->handler->connect($this->options['host'], $this->options['port'], $this->options['timeout']);
  34. }
  35. if ('' != $this->options['password']) {
  36. $this->handler->auth($this->options['password']);
  37. }
  38. if (0 != $this->options['select']) {
  39. $this->handler->select($this->options['select']);
  40. }
  41. }
  42. /**
  43. * 存储Token
  44. * @param int $userID 用户ID
  45. * @param string $token 凭证
  46. * @return bool
  47. */
  48. public function set(int $userID, string $token): bool
  49. {
  50. return $this->handler->hSet($this->userTokenKey, $userID, $token);
  51. }
  52. /**
  53. * 获取Token内的信息
  54. * @param string $token
  55. * @return array
  56. */
  57. public function get(int $userID): array
  58. {
  59. // 判断redis中是否存在
  60. $value = $this->handler->hGet($this->userTokenKey, $userID);
  61. if (is_null($value) || false === $value) {
  62. return [];
  63. }
  64. // 解码
  65. $tokenArr = json_decode($value,true);
  66. if (is_null($tokenArr) || false === $tokenArr) {
  67. return [];
  68. }
  69. return $tokenArr;
  70. }
  71. /**
  72. * 获取加密后的Token
  73. * @param int $userID 用户ID
  74. * @return string
  75. */
  76. public function getEncryptedToken(int $userID): string
  77. {
  78. // 对json数据进行base64编码
  79. return base64_encode(json_encode(self::get($userID), JSON_UNESCAPED_UNICODE));
  80. }
  81. /**
  82. * 判断Token是否可用
  83. * @param string $token Token
  84. * @param int $userID 会员ID
  85. * @return boolean
  86. */
  87. public function check(int $userID, string $token): bool
  88. {
  89. $data = self::get($userID);
  90. return $data && $data['user_id'] == $userID && $data['token'] == $token && $data['create_time'] + Config::get('token.duration') > time();
  91. }
  92. /**
  93. * 删除用户的Token
  94. * @param int $userID 会员ID
  95. * @return boolean
  96. */
  97. public function delete(int $userID): bool
  98. {
  99. $data = $this->get($userID);
  100. if ($data) {
  101. $this->handler->hDel($this->userTokenKey, $userID);
  102. }
  103. return true;
  104. }
  105. }