feat : city
This commit is contained in:
@@ -8,6 +8,7 @@ use App\Constants\Admin\AuthCode;
|
|||||||
use Hyperf\Database\Model\Builder;
|
use Hyperf\Database\Model\Builder;
|
||||||
use Hyperf\Database\Model\Collection;
|
use Hyperf\Database\Model\Collection;
|
||||||
use Hyperf\DbConnection\Model\Model;
|
use Hyperf\DbConnection\Model\Model;
|
||||||
|
use Hyperf\Tappable\HigherOrderTapProxy;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @property int $id
|
* @property int $id
|
||||||
@@ -71,4 +72,13 @@ class AdminSection extends Model
|
|||||||
->get([['id','pid','name','status','remark']])
|
->get([['id','pid','name','status','remark']])
|
||||||
->toArray();
|
->toArray();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param int $id
|
||||||
|
* @return int
|
||||||
|
*/
|
||||||
|
public function getCityById(int $id): int
|
||||||
|
{
|
||||||
|
return $this->where('id',$id)->value('city_id') ?? 0;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,6 +18,7 @@ use App\Extend\SystemUtil;
|
|||||||
use App\Lib\Crypto\CryptoFactory;
|
use App\Lib\Crypto\CryptoFactory;
|
||||||
use App\Model\AdminUser;
|
use App\Model\AdminUser;
|
||||||
use App\Service\Admin\BaseService;
|
use App\Service\Admin\BaseService;
|
||||||
|
use App\Service\ServiceTrait\Admin\GetUserInfoTrait;
|
||||||
use Hyperf\Di\Annotation\Inject;
|
use Hyperf\Di\Annotation\Inject;
|
||||||
use Psr\Container\ContainerExceptionInterface;
|
use Psr\Container\ContainerExceptionInterface;
|
||||||
use Psr\Container\NotFoundExceptionInterface;
|
use Psr\Container\NotFoundExceptionInterface;
|
||||||
@@ -25,6 +26,7 @@ use function Hyperf\Config\config;
|
|||||||
|
|
||||||
class LoginService extends BaseService
|
class LoginService extends BaseService
|
||||||
{
|
{
|
||||||
|
use GetUserInfoTrait;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 注入管理员模型
|
* 注入管理员模型
|
||||||
@@ -71,10 +73,12 @@ class LoginService extends BaseService
|
|||||||
|
|
||||||
if (!$userInfo->save()) throw new AdminException('登录失败');
|
if (!$userInfo->save()) throw new AdminException('登录失败');
|
||||||
|
|
||||||
|
$cityId = $this->getCityById($userInfo->id);
|
||||||
//生成 token
|
//生成 token
|
||||||
$token = $this->cryptoFactory->cryptoClass('admin-jwt',json_encode([
|
$token = $this->cryptoFactory->cryptoClass('admin-jwt',json_encode([
|
||||||
'id' => $userInfo->id,
|
'id' => $userInfo->id,
|
||||||
'role' => $userInfo->role_id,
|
'role' => $userInfo->role_id,
|
||||||
|
'city_id' => $cityId
|
||||||
]))->encrypt();
|
]))->encrypt();
|
||||||
|
|
||||||
//单点登录
|
//单点登录
|
||||||
@@ -87,6 +91,7 @@ class LoginService extends BaseService
|
|||||||
'avatar' => $userInfo->avatar,
|
'avatar' => $userInfo->avatar,
|
||||||
'name' => $userInfo->chinese_name,
|
'name' => $userInfo->chinese_name,
|
||||||
'role_id' => $userInfo->role_id,
|
'role_id' => $userInfo->role_id,
|
||||||
|
'city_id' => $cityId
|
||||||
]
|
]
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,7 +2,75 @@
|
|||||||
|
|
||||||
namespace App\Service\ServiceTrait\Admin;
|
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
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user