123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151 |
- <?php
- namespace App\Utils\Encrypt\Token\Driver;
- use App\Utils\Encrypt\Token\Driver;
- use Hyperf\Config\Annotation\Value;
- use Hyperf\Context\ApplicationContext;
- /**
- * Token操作类
- */
- class Redis extends Driver
- {
- /**
- * 注解
- * 设置redis前缀与框架保持一致
- *
- * @var string
- */
- #[Value("token.default")]
- protected array $config = [];
- protected array $options = [
- 'expire' => 2592000,
- 'userprefix' => 'up:',
- 'tokenprefix' => 'tp:',
- ];
- /**
- * 构造函数
- * @throws \Psr\Container\ContainerExceptionInterface
- * @throws \Psr\Container\NotFoundExceptionInterface
- */
- public function __construct()
- {
- $container = ApplicationContext::getContainer();
- $this->handler = $container->get(\Redis::class);
- }
- /**
- * 获取加密后的Token
- * @param string $token Token标识
- * @return string
- */
- protected function getEncryptedToken(string $token): string
- {
- $config = $this->config;
- return $this->options['tokenprefix'] . hash_hmac($config['hash_algo'], $token, $config['key']);
- }
- /**
- * 获取会员的key
- * @param $user_id
- * @return string
- */
- protected function getUserKey($user_id): string
- {
- return $this->options['userprefix'] . $user_id;
- }
- /**
- * 存储Token
- * @param string $token Token
- * @param int $user_id 会员ID
- * @param int $expire 过期时长,0表示无限,单位秒
- * @return bool
- */
- public function set(string $token, int $user_id, int $expire = 0): bool
- {
- if (is_null($expire)) {
- $expire = $this->options['expire'];
- }
- if ($expire instanceof \DateTime) {
- $expire = $expire->getTimestamp() - time();
- }
- $key = $this->getEncryptedToken($token);
- if ($expire) {
- $result = $this->handler->setex($key, $expire, $user_id);
- } else {
- $result = $this->handler->set($key, $user_id);
- }
- //写入会员关联的token
- $this->handler->sAdd($this->getUserKey($user_id), $key);
- return $result;
- }
- /**
- * 获取Token内的信息
- * @param string $token
- * @return array
- */
- public function get(string $token): array
- {
- $key = $this->getEncryptedToken($token);
- $value = $this->handler->get($key);
- if (is_null($value) || false === $value) {
- return [];
- }
- //获取有效期
- $expire = $this->handler->ttl($key);
- $expire = $expire < 0 ? 365 * 86400 : $expire;
- $expireTime = time() + $expire;
- //解决使用redis方式储存token时api接口Token刷新与检测因expires_in拼写错误报错的BUG
- return ['token' => $token, 'user_id' => $value, 'expiretime' => $expireTime, 'expires_in' => $expire];
- }
- /**
- * 判断Token是否可用
- * @param string $token Token
- * @param int $user_id 会员ID
- * @return boolean
- */
- public function check(string $token, int $user_id): bool
- {
- $data = self::get($token);
- return $data && $data['user_id'] == $user_id;
- }
- /**
- * 删除Token
- * @param string $token
- * @return boolean
- */
- public function delete(string $token): bool
- {
- $data = $this->get($token);
- if ($data) {
- $key = $this->getEncryptedToken($token);
- $user_id = $data['user_id'];
- $this->handler->del($key);
- $this->handler->sRem($this->getUserKey($user_id), $key);
- }
- return true;
- }
- /**
- * 删除指定用户的所有Token
- * @param int $user_id
- * @return boolean
- */
- public function clear(int $user_id): bool
- {
- $keys = $this->handler->sMembers($this->getUserKey($user_id));
- $this->handler->del($this->getUserKey($user_id));
- $this->handler->del($keys);
- return true;
- }
- }
|