diff --git a/app/Annotation/Permission.php b/app/Annotation/Permission.php new file mode 100644 index 0000000..70f2a67 --- /dev/null +++ b/app/Annotation/Permission.php @@ -0,0 +1,39 @@ +code; + } + + /** + * @return string + */ + public function getOperation(): string + { + return $this->operation; + } +} \ No newline at end of file diff --git a/app/Annotation/ResponseFormat.php b/app/Annotation/ResponseFormat.php index 8010aaf..592ab10 100644 --- a/app/Annotation/ResponseFormat.php +++ b/app/Annotation/ResponseFormat.php @@ -3,9 +3,10 @@ namespace App\Annotation; use Attribute; +use Hyperf\Di\Annotation\AbstractAnnotation; #[Attribute(Attribute::TARGET_CLASS | Attribute::TARGET_METHOD)] -class ResponseFormat +class ResponseFormat extends AbstractAnnotation { /** * @param string $format diff --git a/app/Controller/Admin/AdminUserController.php b/app/Controller/Admin/AdminUserController.php index 2c19bb0..e3b55f5 100644 --- a/app/Controller/Admin/AdminUserController.php +++ b/app/Controller/Admin/AdminUserController.php @@ -4,9 +4,11 @@ declare(strict_types=1); namespace App\Controller\Admin; +use App\Annotation\Permission; use App\Annotation\ResponseFormat; use App\Middleware\Token\AdminTokenMiddleware; use App\Service\Admin\AdminUser\UserService; +use Hyperf\Di\Annotation\Inject; use Hyperf\HttpServer\Annotation\Controller; use Hyperf\HttpServer\Annotation\Middleware; use Hyperf\HttpServer\Annotation\RequestMapping; @@ -18,13 +20,19 @@ use Hyperf\Validation\Annotation\Scene; #[Middleware(AdminTokenMiddleware::class)] class AdminUserController { + /** + * @var UserService + */ + #[Inject] + protected UserService $service; + /** * @return array */ #[RequestMapping(path: "getInfo", methods: "GET")] public function getInfo(): array { - return (new UserService)->handle(); + return $this->service->handle(); } /** @@ -33,6 +41,85 @@ class AdminUserController #[RequestMapping(path: "logout", methods: "POST")] public function logout(): array { - return (new UserService)->logout(); + return $this->service->logout(); + } + + /** + * @return array + */ + #[RequestMapping(path: "list", methods: "GET")] + #[Permission(code: 'permission:user:index')] + public function pageList(): array + { + return $this->service->list(); + } + + /** + * @return array + */ + #[RequestMapping(path: "", methods: "PUT")] + #[Permission(code: 'permission:user:update')] + public function updateInfo(): array + { + return $this->service->updateInfo(); + } + + /** + * @return array + */ + #[RequestMapping(path: "password", methods: "PUT")] + #[Permission(code: 'permission:user:password')] + public function resetPassword(): array + { + return $this->service->resetPassword(); + } + + /** + * @return array + */ + #[RequestMapping(path: "", methods: "POST")] + #[Permission(code: 'permission:user:save')] + public function createAdminUser(): array + { + return $this->service->createUser(); + } + + /** + * @return array + */ + #[RequestMapping(path: "", methods: "DELETE")] + #[Permission(code: 'permission:user:delete')] + public function deleteAdminUser(): array + { + return $this->service->deleteUser(); + } + + #[RequestMapping(path: "{userId}", methods: "PUT")] + #[Permission(code: 'permission:user:update')] + public function saveInfo(int $userId): array + { + return $this->service->saveUser($userId); + } + + /** + * @param int $userId + * @return array + */ + #[RequestMapping(path: "{userId}/roles", methods: "GET")] + #[Permission(code: 'permission:user:getRole')] + public function getAdminUserRole(int $userId): array + { + return $this->service->getUserRole($userId); + } + + /** + * @param int $userId + * @return array + */ + #[RequestMapping(path: "{userId}/roles", methods: "PUT")] + #[Permission(code: 'permission:user:setRole')] + public function batchGrantRolesForAdminUser(int $userId): array + { + return $this->service->batchGrantRoleForUser($userId); } } diff --git a/app/Exception/ErrException.php b/app/Exception/ErrException.php index 2c84351..da4ff38 100644 --- a/app/Exception/ErrException.php +++ b/app/Exception/ErrException.php @@ -18,7 +18,7 @@ class ErrException extends ServerException */ protected array $data = []; - public function __construct(string $message = "", int $code = 0, array $data = [], ?Throwable $previous = null) + public function __construct(string $message = 'failed', int $code = 0, array $data = [], ?Throwable $previous = null) { parent::__construct($message, $code, $previous); $this->data = $data; diff --git a/app/Repository/AdminRoleRepository.php b/app/Repository/AdminRoleRepository.php new file mode 100644 index 0000000..2770f3a --- /dev/null +++ b/app/Repository/AdminRoleRepository.php @@ -0,0 +1,18 @@ +where('username', $username) ->first(); } - - /** - * @param mixed $id - * @return array|null - */ - public function findById(mixed $id): ?array - { - return $this->getQuery()->whereKey($id)->first()?->toArray() ?? null; - } } \ No newline at end of file diff --git a/app/Service/Admin/AdminUser/UserService.php b/app/Service/Admin/AdminUser/UserService.php index 0ad7530..5abdc22 100644 --- a/app/Service/Admin/AdminUser/UserService.php +++ b/app/Service/Admin/AdminUser/UserService.php @@ -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()); + } + } } \ No newline at end of file diff --git a/app/Service/Admin/BaseAdminService.php b/app/Service/Admin/BaseAdminService.php index fd9f284..1efb7e3 100644 --- a/app/Service/Admin/BaseAdminService.php +++ b/app/Service/Admin/BaseAdminService.php @@ -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(); + } } \ No newline at end of file diff --git a/app/Trait/AdminUserTrait.php b/app/Trait/AdminUserTrait.php index c1b7ba6..8af5267 100644 --- a/app/Trait/AdminUserTrait.php +++ b/app/Trait/AdminUserTrait.php @@ -39,7 +39,7 @@ trait AdminUserTrait return json_decode($userInfo,true); } - $userInfo = $this->adminUserRepository->findById($adminId); + $userInfo = $this->adminUserRepository->findById($adminId)?->toArray() ?? null; if (!$userInfo) return null; Context::set($key, $userInfo); diff --git a/request/admin.http b/request/admin.http index 8cc36d0..52944f7 100644 --- a/request/admin.http +++ b/request/admin.http @@ -16,6 +16,27 @@ Content-Type: application/x-www-form-urlencoded Authorization: Bearer {{admin_token}} +### 列表 +GET {{host}}/admin/adminUser/list +Content-Type: application/x-www-form-urlencoded +Authorization: Bearer {{admin_token}} + + +### 修改用户资料 +PUT {{host}}/admin/adminUser +Content-Type: application/x-www-form-urlencoded +Authorization: Bearer {{admin_token}} + +nickname=超超级管理员 + + + +### 获取角色 +GET {{host}}/admin/adminUser/1/roles +Content-Type: application/x-www-form-urlencoded +Authorization: Bearer {{admin_token}} + + ### 登录 POST {{host}}/admin/login/refresh Content-Type: application/x-www-form-urlencoded