Token.php 4.3 KB

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