123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154 |
- <?php
- namespace App\Utils\Encrypt;
- use App\Utils\Encrypt\Token\Driver;
- use Hyperf\Config\Annotation\Value;
- use function Hyperf\Config\config;
- /**
- * Token操作类
- */
- class TokenFast
- {
- /**
- * @var array Token的实例
- */
- public static array $instance = [];
- /**
- * @var object 操作句柄
- */
- public static $handler;
- public static array $options;
- /**
- * 连接Token驱动
- * @access public
- * @param array $options 配置数组
- * @param bool|string $name Token连接标识 true 强制重新连接
- * @return Driver
- */
- public static function connect(array $options = [], $name = false)
- {
- $type = !empty($options['type']) ? $options['type'] : 'File';
- if (false === $name) {
- $name = md5(serialize($options));
- }
- if (true === $name || !isset(self::$instance[$name])) {
- $class = false === strpos($type, '\\') ?
- '\\App\\Utils\\Encrypt\\Token\\Driver\\' . ucwords($type) :
- $type;
- if (true === $name) {
- return new $class($options);
- }
- self::$instance[$name] = new $class($options);
- }
- return self::$instance[$name];
- }
- /**
- * 自动初始化Token
- * @access public
- * @param array $options 配置数组
- * @return Driver
- */
- public static function init(array $options = [])
- {
- if (is_null(self::$handler)) {
- if (empty($options)) {
- $options = config('token.default');
- }
- self::$handler = self::connect($options);
- }
- return self::$handler;
- }
- /**
- * 判断Token是否可用(check别名)
- * @access public
- * @param string $token Token标识
- * @param int $user_id
- * @return bool
- */
- public static function has(string $token, int $user_id): bool
- {
- return self::check($token, $user_id);
- }
- /**
- * 判断Token是否可用
- * @param string $token Token标识
- * @param int $user_id
- * @return bool
- */
- public static function check(string $token, int $user_id): bool
- {
- return self::init()->check($token, $user_id);
- }
- /**
- * 读取Token
- * @access public
- * @param string $token Token标识
- * @param array $default 默认值
- * @return array
- */
- public static function get(string $token, array $default = []): array
- {
- return self::init()->get($token) ?: $default;
- }
- /**
- * 写入Token
- * @access public
- * @param string $token Token标识
- * @param int $user_id 存储数据
- * @param int $expire 有效时间 0为永久
- * @return boolean
- */
- public static function set(string $token, int $user_id, int $expire = 0): bool
- {
- return self::init()->set($token, $user_id, $expire);
- }
- /**
- * 删除Token(delete别名)
- * @access public
- * @param string $token Token标识
- * @return boolean
- */
- public static function rm(string $token): bool
- {
- return self::delete($token);
- }
- /**
- * 删除Token
- * @param string $token 标签名
- * @return bool
- */
- public static function delete(string $token): bool
- {
- return self::init()->delete($token);
- }
- /**
- * 清除Token
- * @access public
- * @param int $user_id 用户编号
- * @return boolean
- */
- public static function clear(int $user_id): bool
- {
- return self::init()->clear($user_id);
- }
- }
|