158 lines
3.9 KiB
PHP
158 lines
3.9 KiB
PHP
<?php
|
|
|
|
namespace support\OpenImSdk\Core;
|
|
|
|
use Exception;
|
|
|
|
class TokenManager
|
|
{
|
|
/**
|
|
* 默认Token过期时间(秒)
|
|
* 仅在API未返回过期时间时使用
|
|
* @var int
|
|
*/
|
|
private $defaultTokenExpire = 86400; // 默认24小时
|
|
|
|
/**
|
|
* 构造函数
|
|
*/
|
|
public function __construct()
|
|
{
|
|
}
|
|
|
|
/**
|
|
* 设置默认Token过期时间
|
|
* @param int $seconds 过期时间(秒)
|
|
* @return $this
|
|
*/
|
|
public function setDefaultTokenExpire(int $seconds)
|
|
{
|
|
$this->defaultTokenExpire = $seconds;
|
|
return $this;
|
|
}
|
|
|
|
/**
|
|
* 获取管理员Token
|
|
* @param string $userID 管理员ID
|
|
* @return string|null
|
|
*/
|
|
public function getAdminToken(string $userID = 'imAdmin'): ?string
|
|
{
|
|
$key = "admin_token_{$userID}";
|
|
$tokenData = $this->getCache($key);
|
|
|
|
if (!$tokenData) {
|
|
// Token不存在或已过期,需要重新获取
|
|
return null;
|
|
}
|
|
|
|
$data = json_decode($tokenData, true);
|
|
return $data['token'] ?? null;
|
|
}
|
|
|
|
/**
|
|
* 保存管理员Token
|
|
* @param string $userID 管理员ID
|
|
* @param string $token Token
|
|
* @param int|null $expireTimeSeconds Token过期时间(秒)
|
|
* @return bool
|
|
*/
|
|
public function saveAdminToken(string $userID, string $token, ?int $expireTimeSeconds = null): bool
|
|
{
|
|
$key = "admin_token_{$userID}";
|
|
$expireTime = $expireTimeSeconds ?? $this->defaultTokenExpire;
|
|
|
|
// 存储token和过期时间
|
|
$data = [
|
|
'token' => $token,
|
|
'expireTimeSeconds' => $expireTime
|
|
];
|
|
|
|
return $this->setCache($key, json_encode($data), $expireTime);
|
|
}
|
|
|
|
/**
|
|
* 获取用户Token
|
|
* @param string $userID 用户ID
|
|
* @return string|null
|
|
*/
|
|
public function getUserToken(string $userID): ?string
|
|
{
|
|
$key = "user_token_{$userID}";
|
|
$tokenData = $this->getCache($key);
|
|
|
|
if (!$tokenData) {
|
|
// Token不存在或已过期,需要重新获取
|
|
return null;
|
|
}
|
|
|
|
$data = json_decode($tokenData, true);
|
|
return $data['token'] ?? null;
|
|
}
|
|
|
|
/**
|
|
* 保存用户Token
|
|
* @param string $userID 用户ID
|
|
* @param string $token Token
|
|
* @param int|null $expireTimeSeconds Token过期时间(秒)
|
|
* @return bool
|
|
*/
|
|
public function saveUserToken(string $userID, string $token, ?int $expireTimeSeconds = null): bool
|
|
{
|
|
$key = "user_token_{$userID}";
|
|
$expireTime = $expireTimeSeconds ?? $this->defaultTokenExpire;
|
|
|
|
// 存储token和过期时间
|
|
$data = [
|
|
'token' => $token,
|
|
'expireTimeSeconds' => $expireTime
|
|
];
|
|
|
|
return $this->setCache($key, json_encode($data), $expireTime);
|
|
}
|
|
|
|
/**
|
|
* 清除Token
|
|
* @param string $userID 用户ID
|
|
* @param bool $isAdmin 是否为管理员Token
|
|
* @return bool
|
|
*/
|
|
public function clearToken(string $userID, bool $isAdmin = false): bool
|
|
{
|
|
$key = $isAdmin ? "admin_token_{$userID}" : "user_token_{$userID}";
|
|
return $this->deleteCache($key);
|
|
}
|
|
|
|
/**
|
|
* 获取缓存
|
|
* @param string $key 缓存键
|
|
* @return string|null
|
|
*/
|
|
private function getCache(string $key): ?string
|
|
{
|
|
return cache($key);
|
|
}
|
|
|
|
/**
|
|
* 设置缓存
|
|
* @param string $key 缓存键
|
|
* @param string $value 缓存值
|
|
* @param int $expire 过期时间(秒)
|
|
* @return bool
|
|
*/
|
|
private function setCache(string $key, string $value, int $expire): bool
|
|
{
|
|
return cache($key,$value,$expire);
|
|
}
|
|
|
|
/**
|
|
* 删除缓存
|
|
* @param string $key 缓存键
|
|
* @return bool
|
|
*/
|
|
private function deleteCache(string $key): bool
|
|
{
|
|
return cache($key,null);
|
|
}
|
|
}
|