Tokencoach.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. namespace app\common\library;
  3. use app\common\library\tokencoach\Driver;
  4. use think\App;
  5. use think\Config;
  6. use think\Log;
  7. /**
  8. * Token操作类
  9. */
  10. class Tokencoach
  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\\tokencoach\\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. * @param int $user_id 会员ID
  71. * @return bool
  72. */
  73. public static function has($token, $user_id)
  74. {
  75. return self::check($token, $user_id);
  76. }
  77. /**
  78. * 判断Token是否可用
  79. * @param string $token Token标识
  80. * @param int $user_id 会员ID
  81. * @return bool
  82. */
  83. public static function check($token, $user_id)
  84. {
  85. return self::init()->check($token, $user_id);
  86. }
  87. /**
  88. * 读取Token
  89. * @access public
  90. * @param string $token Token标识
  91. * @param mixed $default 默认值
  92. * @return mixed
  93. */
  94. public static function get($token, $default = false)
  95. {
  96. return self::init()->get($token) ?: $default;
  97. }
  98. /**
  99. * 写入Token
  100. * @access public
  101. * @param string $token Token标识
  102. * @param mixed $user_id 会员ID
  103. * @param int|null $expire 有效时间 0为永久
  104. * @return boolean
  105. */
  106. public static function set($token, $user_id, $expire = null)
  107. {
  108. return self::init()->set($token, $user_id, $expire);
  109. }
  110. /**
  111. * 删除Token(delete别名)
  112. * @access public
  113. * @param string $token Token标识
  114. * @return boolean
  115. */
  116. public static function rm($token)
  117. {
  118. return self::delete($token);
  119. }
  120. /**
  121. * 删除Token
  122. * @param string $token 标签名
  123. * @return bool
  124. */
  125. public static function delete($token)
  126. {
  127. return self::init()->delete($token);
  128. }
  129. /**
  130. * 清除Token
  131. * @access public
  132. * @param int user_id 会员ID
  133. * @return boolean
  134. */
  135. public static function clear($user_id = null)
  136. {
  137. return self::init()->clear($user_id);
  138. }
  139. }