Files
hyperf-micro-svc/app/Service/Admin/AdminUser/UserService.php
2025-09-14 22:33:32 +08:00

201 lines
5.0 KiB
PHP

<?php
/**
* This service file is part of item.
*
* @author ctexthuang
* @contact ctexthuang@qq.com
*/
declare(strict_types=1);
namespace App\Service\Admin\AdminUser;
use App\Cache\Redis\Lua\RateLimit;
use App\Cache\Redis\RedisCache;
use App\Exception\ErrException;
use App\Lib\Jwt\RequestScopedTokenTrait;
use App\Model\AdminRole;
use App\Repository\AdminRoleRepository;
use App\Repository\AdminUserRepository;
use App\Service\Admin\BaseAdminService;
use App\Service\BaseTokenService;
use App\Trait\AdminUserTrait;
use Hyperf\Collection\Arr;
use Hyperf\Di\Annotation\Inject;
use Lcobucci\JWT\Token\RegisteredClaims;
class UserService extends BaseAdminService
{
use AdminUserTrait;
/**
* @var BaseTokenService
*/
#[Inject]
protected BaseTokenService $tokenService;
/**
* @var AdminUserRepository
*/
#[Inject]
protected AdminUserRepository $adminUserRepository;
/**
* @var AdminRoleRepository
*/
#[Inject]
protected AdminRoleRepository $adminRoleRepository;
#[Inject]
protected RedisCache $redisCache;
/**
* @return array
*/
public function handle(): array
{
$this->redisCache->with()->set('123',1);
$this->redisCache->with()->lua(RateLimit::class)->check('user:123', 10, 60);
return $this->adminReturn->success(
'success',
Arr::only(
$this->getAdminUserInfo($this->adminId) ?: [],
['username', 'nickname', 'avatar', 'signed', 'backend_setting', 'phone', 'email']
)
);
}
/**
* @return array
*/
public function logout(): array
{
$this->tokenService->setJwt('admin')->getJwt()->addBlackList($this->getToken());
return $this->adminReturn->success();
}
/**
* @return array
*/
public function list(): array
{
return $this->adminReturn->success('success',$this->adminUserRepository->page(
$this->getRequestData(),
$this->getCurrentPage(),
$this->getPageSize()
));
}
/**
* @return array
*/
public function updateInfo(): array
{
$res = $this->adminUserRepository->updateById($this->adminId,Arr::except($this->getRequestData(),['password']));
if (!$res) throw new ErrException('修改失败');
return $this->adminReturn->success();
}
/**
* @return array
*/
public function resetPassword(): array
{
$adminUserInfo = $this->adminUserRepository->findById($this->adminId);
if (!$adminUserInfo) throw new ErrException('用户异常');
$adminUserInfo->resetPassword();
if (!$adminUserInfo->save()) throw new ErrException('保存密码失败');
return $this->adminReturn->success();
}
/**
* @return array
*/
public function createUser(): array
{
if (! $this->adminUserRepository->create(array_merge(
$this->getRequestData(),
['created_by' => $this->adminId]
))) throw new ErrException();
return $this->adminReturn->success();
}
/**
* @return array
*/
public function deleteUser(): array
{
if (! $this->adminUserRepository->deleteById($this->getRequestData())) throw new ErrException();
return $this->adminReturn->success();
}
/**
* @param int $userId
* @return array
*/
public function saveUser(int $userId): array
{
if (! $this->adminUserRepository->updateById(
$userId,
array_merge(
$this->getRequestData(),
['updated_by' => $this->adminId]
)
)) throw new ErrException();
return $this->adminReturn->success();
}
/**
* @param int $userId
* @return array
*/
public function getUserRole(int $userId): array
{
$userInfo = $this->adminUserRepository->findById($userId);
if (!$userInfo) throw new ErrException('获取用户信息失败');
return $this->adminReturn->success(
'success',
$userInfo->roles()->get()->map(
static fn (AdminRole $adminRole) => $adminRole->only([
'id','code','name'
])
)->toArray()
);
}
/**
* @param int $userId
* @return array
*/
public function batchGrantRoleForUser(int $userId): array
{
$userInfo = $this->adminUserRepository->findById($userId);
if (!$userInfo) throw new ErrException('获取用户信息失败');
try {
$userInfo->roles()->sync(
$this->adminRoleRepository->list([
'code' => $this->request->input('role_codes')
])->map(static function(AdminRole $adminRole) {
return $adminRole->id;
})->all()
);
return $this->adminReturn->success();
} catch (\Throwable $e) {
throw new ErrException($e->getMessage());
}
}
}