feat : city

This commit is contained in:
2024-11-07 16:42:31 +08:00
parent df421ede3d
commit 0c126ac468
3 changed files with 83 additions and 0 deletions

View File

@@ -8,6 +8,7 @@ use App\Constants\Admin\AuthCode;
use Hyperf\Database\Model\Builder;
use Hyperf\Database\Model\Collection;
use Hyperf\DbConnection\Model\Model;
use Hyperf\Tappable\HigherOrderTapProxy;
/**
* @property int $id
@@ -71,4 +72,13 @@ class AdminSection extends Model
->get([['id','pid','name','status','remark']])
->toArray();
}
/**
* @param int $id
* @return int
*/
public function getCityById(int $id): int
{
return $this->where('id',$id)->value('city_id') ?? 0;
}
}

View File

@@ -18,6 +18,7 @@ use App\Extend\SystemUtil;
use App\Lib\Crypto\CryptoFactory;
use App\Model\AdminUser;
use App\Service\Admin\BaseService;
use App\Service\ServiceTrait\Admin\GetUserInfoTrait;
use Hyperf\Di\Annotation\Inject;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
@@ -25,6 +26,7 @@ use function Hyperf\Config\config;
class LoginService extends BaseService
{
use GetUserInfoTrait;
/**
* 注入管理员模型
@@ -71,10 +73,12 @@ class LoginService extends BaseService
if (!$userInfo->save()) throw new AdminException('登录失败');
$cityId = $this->getCityById($userInfo->id);
//生成 token
$token = $this->cryptoFactory->cryptoClass('admin-jwt',json_encode([
'id' => $userInfo->id,
'role' => $userInfo->role_id,
'city_id' => $cityId
]))->encrypt();
//单点登录
@@ -87,6 +91,7 @@ class LoginService extends BaseService
'avatar' => $userInfo->avatar,
'name' => $userInfo->chinese_name,
'role_id' => $userInfo->role_id,
'city_id' => $cityId
]
]);
}

View File

@@ -2,7 +2,75 @@
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;
}
}