| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162 |
- <?php
- namespace app\common\library;
- use app\common\library\token\Driver;
- use fast\Random;
- use think\App;
- use think\Config;
- use think\Log;
- /**
- * Token操作类
- */
- class Token
- {
- /**
- * @var array Token的实例
- */
- public static array $instance = [];
- /**
- * @var Driver 操作句柄
- */
- public static $handler;
- /**
- * 连接Token驱动
- * @access public
- * @param array $options 配置数组
- * @param bool|string $name Token连接标识 true 强制重新连接
- * @return Driver
- */
- public static function connect(array $options = [], $name = false): Driver
- {
- $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\\common\\library\\token\\driver\\' . ucwords($type) :
- $type;
- // 记录初始化信息
- App::$debug && Log::record('[ TOKEN ] INIT ' . $type, 'info');
- 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 = []): Driver
- {
- if (is_null(self::$handler)) {
- if (empty($options) && 'complex' == Config::get('token.type')) {
- $default = Config::get('token.default');
- // 获取默认Token配置,并连接
- $options = Config::get('token.' . $default['type']) ?: $default;
- } elseif (empty($options)) {
- $options = Config::get('token');
- }
- self::$handler = self::connect($options);
- }
- return self::$handler;
- }
- /**
- * 写入Token
- * @access public
- * @param int $userID 用户ID
- * @param string $address 凭证
- * @return bool
- */
- public static function marshal(int $userID): bool
- {
- // 最近有生成时直接返回
- $tokenArr = self::get($userID);
- if (!empty($tokenArr) && isset($tokenArr['create_time']) && $tokenArr['create_time'] + Config::get('token.reuse_duration') > time()) {
- return true;
- }
- // 不存在或超过一定时间则重新生成
- $tokenData["user_id"] = $userID;
- $tokenData["token"] = Random::uuid(); // 使用随机字符串
- $tokenData["create_time"] = time();
- return self::init()->set($userID, json_encode($tokenData, JSON_UNESCAPED_UNICODE));
- }
- /**
- * 解码已加密的Token信息
- * @param string $token
- * @return array
- */
- public static function unmarshal(string $token): array
- {
- $decode = json_decode(base64_decode($token), true);
- $tokenArr = [];
- if (!empty($decode) && !empty($decode["user_id"]) && is_int($decode["user_id"]) && !empty($decode["token"]) && is_string($decode["token"])) {
- $tokenArr["user_id"] = $decode["user_id"];
- $tokenArr["token"] = $decode["token"];
- }
- return $tokenArr;
- }
- /**
- * 判断Token是否可用
- * @param int $userID 会员ID
- * @param string $token Token标识
- * @return bool
- */
- public static function check(int $userID, string $token): bool
- {
- return self::init()->check($userID, $token);
- }
- /**
- * 读取Token
- * @access public
- * @param int $userID 用户ID
- * @return array
- */
- public static function get(int $userID): array
- {
- return self::init()->get($userID);
- }
- /**
- * 获取加密后的Token
- * @access public
- * @param int $userID 用户ID
- * @return array
- */
- public static function getEncryptedToken(int $userID): string
- {
- return self::init()->getEncryptedToken($userID);
- }
- /**
- * 删除Token
- * @param int $userID 用户ID
- * @return bool
- */
- public static function delete(int $userID): bool
- {
- return self::init()->delete($userID);
- }
- }
|