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