控制台应用,yzncms本身基于tp5.1框架,里面的队列用不了,bug,坑
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.

Token.php 4.4KB

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