You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

160 lines
3.7 KiB

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