Files
im/support/Jwt/RedisHandler.php
T
commie c153975eed 7
2026-01-08 05:42:44 +08:00

91 lines
2.5 KiB
PHP
Executable File

<?php
/**
* @desc RedisHanle.php 描述信息
* @author Tinywan(ShaoBo Wan)
* @date 2022/3/18 17:13
*/
declare(strict_types=1);
namespace support\Jwt;
use support\Redis;
use support\Jwt\Exception\JwtCacheTokenException;
class RedisHandler
{
/**
* @desc: 生成缓存令牌
* (1)登录时,判断该账号是否在其它设备登录,如果有,就请空之前key清除,
* (2)重新设置key,然后存储用户信息在redis当中
* @param string $pre
* @param string $client
* @param string $uid
* @param int $ttl
* @param string $token
* @author Tinywan(ShaoBo Wan)
*/
public static function generateToken(string $pre, string $client, string $uid, int $ttl, string $token): void
{
$cacheKey = $pre . $client. ':'. $uid;
Redis::del($cacheKey);
Redis::setex($cacheKey, $ttl, $token);
}
/**
* @desc: 刷新存储的缓存令牌
* @param string $pre
* @param string $client
* @param string $uid
* @param int $ttl
* @param string $token
* @return void
*/
public static function refreshToken(string $pre, string $client, string $uid, int $ttl, string $token): void
{
$cacheKey = $pre . $client . ':' . $uid;
$isExists = Redis::exists($cacheKey);
if ($isExists) {
$ttl = Redis::ttl($cacheKey);
}
Redis::setex($cacheKey, $ttl, $token);
}
/**
* @desc: 检查设备缓存令牌
* @param string $pre
* @param string $client
* @param string $uid
* @param string $token
* @return bool
* @author Tinywan(ShaoBo Wan)
*/
public static function verifyToken(string $pre, string $client, string $uid, string $token): bool
{
$cacheKey = $pre . $client. ':'. $uid;
if (!Redis::exists($cacheKey)) {
throw new JwtCacheTokenException('The account has been logged in on another device and forced to log out.');
}
if (Redis::get($cacheKey) != $token) {
throw new JwtCacheTokenException('Identity verification session has expired, please log in again!');
}
return true;
}
/**
* @desc: 清理缓存令牌
* @param string $pre
* @param string $client
* @param string $uid
* @return bool
* @author Tinywan(ShaoBo Wan)
*/
public static function clearToken(string $pre, string $client, string $uid): bool
{
Redis::del($pre . $client. ':'. $uid);
return true;
}
}