TokenFast.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. namespace App\Utils\Encrypt;
  3. use App\Utils\Encrypt\Token\Driver;
  4. use Hyperf\Config\Annotation\Value;
  5. use function Hyperf\Config\config;
  6. /**
  7. * Token操作类
  8. */
  9. class TokenFast
  10. {
  11. /**
  12. * @var array Token的实例
  13. */
  14. public static array $instance = [];
  15. /**
  16. * @var object 操作句柄
  17. */
  18. public static $handler;
  19. public static array $options;
  20. /**
  21. * 连接Token驱动
  22. * @access public
  23. * @param array $options 配置数组
  24. * @param bool|string $name Token连接标识 true 强制重新连接
  25. * @return Driver
  26. */
  27. public static function connect(array $options = [], $name = false)
  28. {
  29. $type = !empty($options['type']) ? $options['type'] : 'File';
  30. if (false === $name) {
  31. $name = md5(serialize($options));
  32. }
  33. if (true === $name || !isset(self::$instance[$name])) {
  34. $class = false === strpos($type, '\\') ?
  35. '\\App\\Utils\\Encrypt\\Token\\Driver\\' . ucwords($type) :
  36. $type;
  37. if (true === $name) {
  38. return new $class($options);
  39. }
  40. self::$instance[$name] = new $class($options);
  41. }
  42. return self::$instance[$name];
  43. }
  44. /**
  45. * 自动初始化Token
  46. * @access public
  47. * @param array $options 配置数组
  48. * @return Driver
  49. */
  50. public static function init(array $options = [])
  51. {
  52. if (is_null(self::$handler)) {
  53. if (empty($options)) {
  54. $options = config('token.default');
  55. }
  56. self::$handler = self::connect($options);
  57. }
  58. return self::$handler;
  59. }
  60. /**
  61. * 判断Token是否可用(check别名)
  62. * @access public
  63. * @param string $token Token标识
  64. * @param int $user_id
  65. * @return bool
  66. */
  67. public static function has(string $token, int $user_id): bool
  68. {
  69. return self::check($token, $user_id);
  70. }
  71. /**
  72. * 判断Token是否可用
  73. * @param string $token Token标识
  74. * @param int $user_id
  75. * @return bool
  76. */
  77. public static function check(string $token, int $user_id): bool
  78. {
  79. return self::init()->check($token, $user_id);
  80. }
  81. /**
  82. * 读取Token
  83. * @access public
  84. * @param string $token Token标识
  85. * @param array $default 默认值
  86. * @return array
  87. */
  88. public static function get(string $token, array $default = []): array
  89. {
  90. return self::init()->get($token) ?: $default;
  91. }
  92. /**
  93. * 写入Token
  94. * @access public
  95. * @param string $token Token标识
  96. * @param int $user_id 存储数据
  97. * @param int $expire 有效时间 0为永久
  98. * @return boolean
  99. */
  100. public static function set(string $token, int $user_id, int $expire = 0): bool
  101. {
  102. return self::init()->set($token, $user_id, $expire);
  103. }
  104. /**
  105. * 删除Token(delete别名)
  106. * @access public
  107. * @param string $token Token标识
  108. * @return boolean
  109. */
  110. public static function rm(string $token): bool
  111. {
  112. return self::delete($token);
  113. }
  114. /**
  115. * 删除Token
  116. * @param string $token 标签名
  117. * @return bool
  118. */
  119. public static function delete(string $token): bool
  120. {
  121. return self::init()->delete($token);
  122. }
  123. /**
  124. * 清除Token
  125. * @access public
  126. * @param int $user_id 用户编号
  127. * @return boolean
  128. */
  129. public static function clear(int $user_id): bool
  130. {
  131. return self::init()->clear($user_id);
  132. }
  133. }