mirror of
https://gitee.com/ctexthuang/hyperf_rbac_framework_server_ctexthuang.git
synced 2025-12-25 11:22:10 +08:00
50 lines
1.1 KiB
PHP
50 lines
1.1 KiB
PHP
<?php
|
|
|
|
namespace App\Trait;
|
|
|
|
use App\Cache\Redis\RedisCache;
|
|
use App\Cache\Redis\RedisKey;
|
|
use App\Repository\AdminUserRepository;
|
|
use Hyperf\Context\Context;
|
|
use Hyperf\Di\Annotation\Inject;
|
|
|
|
trait AdminUserTrait
|
|
{
|
|
/**
|
|
* @var RedisCache
|
|
*/
|
|
#[Inject]
|
|
protected RedisCache $redis;
|
|
|
|
/**
|
|
* @var AdminUserRepository
|
|
*/
|
|
#[Inject]
|
|
protected AdminUserRepository $adminUserRepository;
|
|
|
|
/**
|
|
* @param int $adminId
|
|
* @return array|null
|
|
*/
|
|
public function getAdminUserInfo(int $adminId): array|null
|
|
{
|
|
$key = RedisKey::getAdminUserInfoKey($adminId);
|
|
if (Context::has($key)) {
|
|
return Context::get($key,false);
|
|
}
|
|
|
|
if ($this->redis->with()->exists($key)) {
|
|
$userInfo = $this->redis->with()->get($key);
|
|
Context::set($key,$userInfo);
|
|
return json_decode($userInfo,true);
|
|
}
|
|
|
|
$userInfo = $this->adminUserRepository->findById($adminId);
|
|
if (!$userInfo) return null;
|
|
|
|
Context::set($key, $userInfo);
|
|
$this->redis->with()->set($key, json_encode($userInfo), 3600);
|
|
|
|
return $userInfo;
|
|
}
|
|
} |