feat : admin user finish

This commit is contained in:
2025-09-14 22:33:32 +08:00
parent cf5d5059d7
commit 1d01ecfdcb
10 changed files with 339 additions and 19 deletions

View File

@@ -12,7 +12,10 @@ 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;
@@ -31,9 +34,24 @@ class UserService extends BaseAdminService
#[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);
@@ -48,11 +66,6 @@ class UserService extends BaseAdminService
);
}
private function user()
{
}
/**
* @return array
*/
@@ -62,4 +75,127 @@ class UserService extends BaseAdminService
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());
}
}
}

View File

@@ -52,4 +52,28 @@ abstract class BaseAdminService
* 主函数抽象类
*/
abstract public function handle();
/**
* @return int
*/
protected function getCurrentPage(): int
{
return (int) $this->request->input('page', 1);
}
/**
* @return int
*/
protected function getPageSize(): int
{
return (int) $this->request->input('page_size', 20);
}
/**
* @return array
*/
protected function getRequestData(): array
{
return $this->request->all();
}
}