mirror of
https://gitee.com/ctexthuang/hyperf-micro-svc.git
synced 2026-02-08 10:20:16 +08:00
193 lines
4.7 KiB
PHP
193 lines
4.7 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\Common\Repository\AdminRoleRepository;
|
|
use App\Common\Repository\AdminUserRepository;
|
|
use App\Common\Trait\AdminUserTrait;
|
|
use App\Exception\ErrException;
|
|
use App\Model\AdminRole;
|
|
use App\Service\Admin\BaseAdminService;
|
|
use App\Service\BaseTokenService;
|
|
use Hyperf\Collection\Arr;
|
|
use Hyperf\Di\Annotation\Inject;
|
|
|
|
class UserService extends BaseAdminService
|
|
{
|
|
use AdminUserTrait;
|
|
|
|
/**
|
|
* @var BaseTokenService
|
|
*/
|
|
#[Inject]
|
|
protected BaseTokenService $tokenService;
|
|
|
|
/**
|
|
* @var AdminUserRepository
|
|
*/
|
|
#[Inject]
|
|
protected AdminUserRepository $adminUserRepository;
|
|
|
|
/**
|
|
* @var AdminRoleRepository
|
|
*/
|
|
#[Inject]
|
|
protected AdminRoleRepository $adminRoleRepository;
|
|
|
|
/**
|
|
* @return array
|
|
*/
|
|
public function handle(): array
|
|
{
|
|
return $this->adminReturn->success(
|
|
'success',
|
|
Arr::only(
|
|
$this->getAdminUserInfo($this->adminId)?->toArray() ?: [],
|
|
['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;
|
|
})
|
|
);
|
|
|
|
return $this->adminReturn->success();
|
|
} catch (\Throwable $e) {
|
|
throw new ErrException($e->getMessage());
|
|
}
|
|
}
|
|
} |