Skip to content

Commit

Permalink
Merge pull request #4 from thinksvip/smiler
Browse files Browse the repository at this point in the history
redis 调整key
  • Loading branch information
2166909366 authored May 29, 2023
2 parents d7a94f7 + ee6c281 commit 4c93998
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 11 deletions.
12 changes: 6 additions & 6 deletions src/Api/Login.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ protected function init()
*/
public function isLogin(): bool
{
$ident = $this->ts->uid . '-' . md5(Yii::$app->request->getUserIP() . Yii::$app->request->getUserAgent());
$key = $this->getCrKey('user:login_status:%s:%s', [$this->ts->tid, $ident]);
$key = $this->getCrKey('user:login_status:%s:%s', $this->ts->arrayRedisKeyTenantIdUserIdToken());

$value = self::redisGz()->get($key);

Expand All @@ -46,11 +45,12 @@ public function isLogin(): bool
*/
public function getLoginUserinfo()
{
$key = $this->getCrKey('user:access_token:%s', [md5($this->token)]);

$userinfo = self::redisGz()->get($key);
if (empty($this->loginUser)) {
$key = $this->getCrKey('user:access_token:%s:%s', $this->ts->arrayRedisKeyTenantIdUserIdToken());
$this->loginUser = self::redisGz()->get($key);
}

return $userinfo ?? [];
return $this->loginUser ?? [];
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Api/Perm/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ protected function init()
*/
public function get()
{
$key = $this->getCrKey('permission:data:%s:%s', $this->ts->listTenantIdAndUserId());
$key = $this->getCrKey('permission:data:%s:%s', $this->ts->arrayRedisKeyTenantIdUserIdToken());

$data = self::redisGz()->get($key);
return $data;
Expand Down
4 changes: 3 additions & 1 deletion src/Units/ApiAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@ abstract class ApiAbstract

protected TokenStruct $ts;// token数据结构

protected $loginUser;

public function __construct()
{
$this->setToken();

$params = JwtAuth::getInstance()->decode($this->token)->getParams();
$this->ts = new TokenStruct($params);
$this->ts = new TokenStruct($params, $this->token);
}

/**
Expand Down
18 changes: 15 additions & 3 deletions src/Units/Struct/TokenStruct.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ class TokenStruct
*/
public $uid;

public $token;

/**
* 登录类型
* @var int|mixed
Expand All @@ -32,11 +34,12 @@ class TokenStruct

protected array $params = [];

public function __construct(array $params)
public function __construct(array $params, string $token = '')
{
$this->tid = $params['tenant_id'] ?? null;
$this->uid = $params['user_id'] ?? null;
$this->loginType = $params['login_type'] ?? 1;
$this->token = $token;

if (empty($this->tid) || empty($this->uid)) {
throw new XcAuthException(XcAuthErrorCode::NO_LOGIN, XcAuthErrorCode::OAUTH_TOKEN_PARSE_ERROR);
Expand All @@ -54,12 +57,21 @@ public function getParams()
return $this->params;
}

/**
* 获取用户id Token key
* @return string
*/
protected function getUserIdTokenKey()
{
return sprintf('%s-%s', $this->uid, md5($this->token));
}

/**
* 获取租户id和用户id
* @return array
*/
public function listTenantIdAndUserId()
public function arrayRedisKeyTenantIdUserIdToken()
{
return [$this->tid, $this->uid];
return [$this->tid, $this->getUserIdTokenKey()];
}
}

0 comments on commit 4c93998

Please sign in to comment.