From 485f59eac30b0c48c1e7fef4f7c68095f196c3ea Mon Sep 17 00:00:00 2001 From: Aether Date: Tue, 23 Sep 2025 15:29:55 +0800 Subject: [PATCH] .. --- app/Controller/CampusController.php | 162 +++++++++--------- config/autoload/server.php | 14 ++ config/routes.php | 20 +-- extend/Aether/PHP/Hyperf/AetherController.php | 4 + 4 files changed, 106 insertions(+), 94 deletions(-) diff --git a/app/Controller/CampusController.php b/app/Controller/CampusController.php index 620797a..4b33525 100644 --- a/app/Controller/CampusController.php +++ b/app/Controller/CampusController.php @@ -12,94 +12,22 @@ use App\Validator\CampusValidator; use Exception; use Hyperf\Di\Annotation\Inject; use Hyperf\HttpServer\Annotation\Controller; +use Hyperf\HttpServer\Annotation\DeleteMapping; +use Hyperf\HttpServer\Annotation\GetMapping; +use Hyperf\HttpServer\Annotation\PostMapping; +use Hyperf\HttpServer\Annotation\PutMapping; use Hyperf\HttpServer\Contract\RequestInterface; -#[Controller( - prefix: '/api/v1/campus', -)] +#[Controller(prefix: '/api/v1/campus')] class CampusController extends AetherController { #[Inject] protected CampusValidator $validator; - /** - * 创建校区. - * @PostMapping(path="") - */ - public function create(RequestInterface $request): array - { - $data = $request->all(); - $validator = $this->validator->validateCreate($data); - if ($validator->fails()) { - throw new BusinessException(400, $validator->errors()->first()); - } - - $campus = new Campus(); - $campus->fill($data); - $campus->save(); - - return AetherResponse::success($campus->toArray(), '校区创建成功'); - } - - /** - * 获取校区详情. - * @GetMapping(path="/{id}") - */ - public function get(int $id): array - { - $campus = Campus::find($id); - if (! $campus) { - throw new BusinessException(10001); - } - - return AetherResponse::success($campus->toArray()); - } - - /** - * 更新校区. - * @PutMapping(path="/{id}") - */ - public function update(int $id, RequestInterface $request): array - { - $data = $request->all(); - $data['id'] = $id; - - $validator = $this->validator->validateUpdate($data); - if ($validator->fails()) { - throw new BusinessException(400, $validator->errors()->first()); - } - - $campus = Campus::find($id); - if (! $campus) { - throw new BusinessException(10001); - } - - $campus->fill($data); - $campus->save(); - - return AetherResponse::success($campus->toArray(), '校区更新成功'); - } - - /** - * 删除校区. - * @DeleteMapping(path="/{id}") - * @throws Exception - */ - public function delete(int $id): array - { - $campus = Campus::find($id); - if (! $campus) { - throw new BusinessException(10001); - } - - $campus->delete(); - return AetherResponse::success(null, '校区删除成功'); - } - /** * 校区列表. - * @GetMapping(path="/list") */ + #[GetMapping(path: '/list')] public function list(RequestInterface $request): array { $data = $request->all(); @@ -148,8 +76,8 @@ class CampusController extends AetherController /** * 获取省份列表. - * @GetMapping(path="/provinces") */ + #[GetMapping(path: '/provinces')] public function provinces(): array { $provinces = Campus::level(1) @@ -163,8 +91,8 @@ class CampusController extends AetherController /** * 获取城市列表. - * @GetMapping(path="/cities") */ + #[GetMapping(path: '/cities')] public function cities(RequestInterface $request): array { $province = $request->input('province'); @@ -181,4 +109,78 @@ class CampusController extends AetherController return AetherResponse::success($cities); } + + /** + * 创建校区. + */ + #[PostMapping(path: '')] + public function create(RequestInterface $request): array + { + $data = $request->all(); + $validator = $this->validator->validateCreate($data); + if ($validator->fails()) { + throw new BusinessException(400, $validator->errors()->first()); + } + + $campus = new Campus(); + $campus->fill($data); + $campus->save(); + + return AetherResponse::success($campus->toArray(), '校区创建成功'); + } + + /** + * 获取校区详情. + */ + #[GetMapping(path: '/{id}')] + public function get(int $id): array + { + $campus = Campus::find($id); + if (! $campus) { + throw new BusinessException(10001); + } + + return AetherResponse::success($campus->toArray()); + } + + /** + * 更新校区. + */ + #[PutMapping(path: '/{id}')] + public function update(int $id, RequestInterface $request): array + { + $data = $request->all(); + $data['id'] = $id; + + $validator = $this->validator->validateUpdate($data); + if ($validator->fails()) { + throw new BusinessException(400, $validator->errors()->first()); + } + + $campus = Campus::find($id); + if (! $campus) { + throw new BusinessException(10001); + } + + $campus->fill($data); + $campus->save(); + + return AetherResponse::success($campus->toArray(), '校区更新成功'); + } + + /** + * 删除校区. + * @throws Exception + */ + #[DeleteMapping(path: '/{id}')] + public function delete(int $id): array + { + $campus = Campus::find($id); + if (! $campus) { + throw new BusinessException(10001); + } + + $campus->delete(); + return AetherResponse::success(null, '校区删除成功'); + } } diff --git a/config/autoload/server.php b/config/autoload/server.php index 2b6e983..342752b 100644 --- a/config/autoload/server.php +++ b/config/autoload/server.php @@ -15,6 +15,20 @@ use function Hyperf\Support\env; return [ 'mode' => SWOOLE_PROCESS, 'servers' => [ + [ + 'name' => 'http', + 'type' => Server::SERVER_HTTP, + 'host' => '0.0.0.0', + 'port' => 9501, + 'sock_type' => SWOOLE_SOCK_TCP, + 'callbacks' => [ + Event::ON_REQUEST => [Hyperf\HttpServer\Server::class, 'onRequest'], + ], + 'options' => [ + // Whether to enable request lifecycle event + 'enable_request_lifecycle' => false, + ], + ], [ 'name' => 'jsonrpc-http', 'type' => Server::SERVER_HTTP, diff --git a/config/routes.php b/config/routes.php index 46d04e5..cd60f65 100644 --- a/config/routes.php +++ b/config/routes.php @@ -1,18 +1,10 @@