Files
hyperf_service/app/Service/ServiceTrait/Admin/GetUserInfoTrait.php
2024-11-07 16:42:31 +08:00

76 lines
1.6 KiB
PHP

<?php
namespace App\Service\ServiceTrait\Admin;
use App\Model\AdminSection;
use App\Model\AdminUser;
use Hyperf\Context\Context;
use Hyperf\Database\Model\Builder;
use Hyperf\Database\Model\Model;
use Hyperf\Di\Annotation\Inject;
use Hyperf\Tappable\HigherOrderTapProxy;
trait GetUserInfoTrait
{
/**
* 后台用户模型
* @var AdminUser
*/
#[Inject]
protected AdminUser $mainAdminUserModel;
/**
* 后台部门模型
* @var AdminSection
*/
#[Inject]
protected AdminSection $mainAdminSectionModel;
/**
* 单例获取用户信息
* @param $adminId
* @return false|Builder|Model|mixed|string
*/
protected function getUserInfo($adminId): mixed
{
$key = 'admin_id:' . $adminId;
if (Context::has($key)) {
return Context::get($key, false);
}
$userInfo = $this->mainAdminUserModel->getAdminInfoById($adminId);
if (!$userInfo) {
return false;
}
Context::set($key, $userInfo);
return $userInfo;
}
/**
* @param int $adminId
* @return int
*/
protected function getCityById(int $adminId): int
{
$userInfo = $this->getUserInfo($adminId);
if (!$userInfo) {
return 0;
}
$key = 'section_city:' . $userInfo->section_id;
if (Context::has($key)) {
return Context::get($key, 0);
}
$cityId = $this->mainAdminSectionModel->getCityById($userInfo->section_id);
if (!$cityId) {
return 0;
}
Context::set($key, $cityId);
return $cityId;
}
}