From 1f6b60ee0f48123b57b1c334f57f7c340f6c1ead Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=98=BF=E4=B8=8D=E5=8F=AE=E5=92=9A?= Date: Wed, 24 Sep 2025 16:37:22 +0800 Subject: [PATCH] .. --- app/Controller/CampusController.php | 132 +++------------- app/Controller/TeacherController.php | 148 ++++-------------- app/JsonRpc/Service/CampusService.php | 25 ++- app/JsonRpc/Service/TeacherService.php | 25 ++- app/Validator/CampusValidator.php | 111 +++++++------ app/Validator/TeacherValidator.php | 120 +++++++------- config/autoload/middlewares.php | 5 + extend/Aether/PHP/Hyperf/AetherController.php | 11 +- 8 files changed, 217 insertions(+), 360 deletions(-) diff --git a/app/Controller/CampusController.php b/app/Controller/CampusController.php index 0f607fe..f3c119b 100644 --- a/app/Controller/CampusController.php +++ b/app/Controller/CampusController.php @@ -5,11 +5,9 @@ declare(strict_types=1); namespace App\Controller; use Aether\AetherController; +use Aether\AetherCrudService; use Aether\AetherResponse; -use App\Exception\BusinessException; -use Aether\Campus; -use Aether\CampusValidator; -use Exception; +use App\JsonRpc\Service\CampusService; use Hyperf\Di\Annotation\Inject; use Hyperf\HttpServer\Annotation\Controller; use Hyperf\HttpServer\Annotation\DeleteMapping; @@ -17,61 +15,23 @@ use Hyperf\HttpServer\Annotation\GetMapping; use Hyperf\HttpServer\Annotation\PostMapping; use Hyperf\HttpServer\Annotation\PutMapping; use Hyperf\HttpServer\Contract\RequestInterface; +use Throwable; #[Controller(prefix: '/api/v1')] class CampusController extends AetherController { #[Inject] - protected CampusValidator $validator; + protected CampusService $service; + #[Inject] + protected RequestInterface $request; /** * 校区列表. */ #[GetMapping(path: 'campus/list')] public function list(RequestInterface $request): array { - $data = $request->all(); - $validator = $this->validator->validateQuery($data); - if ($validator->fails()) { - throw new BusinessException(400, $validator->errors()->first()); - } - - $query = Campus::query(); - - if (! empty($data['id'])) { - $query->where('id', $data['id']); - } - if (! empty($data['name'])) { - $query->where('name', 'like', "%{$data['name']}%"); - } - if (isset($data['parent_id'])) { - $query->where('parent_id', $data['parent_id']); - } - if (isset($data['level'])) { - $query->where('level', $data['level']); - } - if (! empty($data['province'])) { - $query->where('province', $data['province']); - } - if (! empty($data['city'])) { - $query->where('city', $data['city']); - } - if (isset($data['status'])) { - $query->where('status', $data['status']); - } - - $page = $data['page'] ?? 1; - $size = $data['size'] ?? 20; - - $total = $query->count(); - $list = $query->forPage($page, $size)->get()->toArray(); - - return AetherResponse::success([ - 'total' => $total, - 'page' => $page, - 'size' => $size, - 'list' => $list, - ]); + return AetherResponse::success($this->service->list($request->all())); } /** @@ -80,13 +40,7 @@ class CampusController extends AetherController #[GetMapping(path: 'campus/provinces')] public function provinces(): array { - $provinces = Campus::level(1) - ->enabled() - ->orderBy('name') - ->get(['id', 'name', 'province']) - ->toArray(); - - return AetherResponse::success($provinces); + return AetherResponse::success($this->service->getProvinces()); } /** @@ -95,38 +49,17 @@ class CampusController extends AetherController #[GetMapping(path: 'campus/cities')] public function cities(RequestInterface $request): array { - $province = $request->input('province'); - if (empty($province)) { - throw new BusinessException(400, '省份不能为空'); - } - - $cities = Campus::level(2) - ->province($province) - ->enabled() - ->orderBy('name') - ->get(['id', 'name', 'city']) - ->toArray(); - - return AetherResponse::success($cities); + return AetherResponse::success($this->service->getCitiesByProvince($request->input('province_id'))); } /** * 创建校区. + * @throws Throwable */ #[PostMapping(path: 'campus')] - public function create(RequestInterface $request): array + public function create(): 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(), '校区创建成功'); + return AetherResponse::success($this->service->create($this->request->all()), '校区创建成功'); } /** @@ -135,52 +68,31 @@ class CampusController extends AetherController #[GetMapping(path: 'campus/{id}')] public function get(int $id): array { - $campus = Campus::find($id); - if (! $campus) { - throw new BusinessException(10001); - } - - return AetherResponse::success($campus->toArray()); + return AetherResponse::success($this->service->detail($id)); } /** * 更新校区. + * @throws Throwable */ #[PutMapping(path: 'campus/{id}')] - public function update(int $id, RequestInterface $request): array + public function update(int $id): 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(), '校区更新成功'); + return AetherResponse::success($this->service->update($id, $this->request->all()), '校区更新成功'); } /** * 删除校区. - * @throws Exception + * @throws Throwable */ #[DeleteMapping(path: 'campus/{id}')] public function delete(int $id): array { - $campus = Campus::find($id); - if (! $campus) { - throw new BusinessException(10001); - } + return AetherResponse::success($this->service->delete($id), '校区删除成功'); + } - $campus->delete(); - return AetherResponse::success(null, '校区删除成功'); + protected function getService(): AetherCrudService + { + return $this->service; } } diff --git a/app/Controller/TeacherController.php b/app/Controller/TeacherController.php index 60be093..5dad47a 100644 --- a/app/Controller/TeacherController.php +++ b/app/Controller/TeacherController.php @@ -5,10 +5,9 @@ declare(strict_types=1); namespace App\Controller; use Aether\AetherController; +use Aether\AetherCrudService; use Aether\AetherResponse; -use App\Exception\BusinessException; -use Aether\Teacher; -use Aether\TeacherValidator; +use App\JsonRpc\Service\TeacherService; use Hyperf\Di\Annotation\Inject; use Hyperf\HttpServer\Annotation\Controller; use Hyperf\HttpServer\Annotation\DeleteMapping; @@ -16,30 +15,39 @@ use Hyperf\HttpServer\Annotation\GetMapping; use Hyperf\HttpServer\Annotation\PostMapping; use Hyperf\HttpServer\Annotation\PutMapping; use Hyperf\HttpServer\Contract\RequestInterface; +use Throwable; -#[Controller(prefix: '/api/v1/')] +#[Controller(prefix: '/api/v1')] class TeacherController extends AetherController { #[Inject] - protected TeacherValidator $validator; + protected TeacherService $service; + + #[Inject] + protected RequestInterface $request; + + protected function getService(): AetherCrudService + { + return $this->service; + } + + /** + * 教师列表. + */ + #[GetMapping(path: 'teacher/list')] + public function list(RequestInterface $request): array + { + return AetherResponse::success($this->service->list($request->all())); + } /** * 创建教师. + * @throws Throwable */ #[PostMapping(path: 'teacher')] - public function create(RequestInterface $request): array + public function create(): array { - $data = $request->all(); - $validator = $this->validator->validateCreate($data); - if ($validator->fails()) { - throw new BusinessException(400, $validator->errors()->first()); - } - - $teacher = new Teacher(); - $teacher->fill($data); - $teacher->save(); - - return AetherResponse::success($teacher->toArray(), '教师创建成功'); + return AetherResponse::success($this->service->create($this->request->all()), '教师创建成功'); } /** @@ -48,118 +56,26 @@ class TeacherController extends AetherController #[GetMapping(path: 'teacher/{id}')] public function get(int $id): array { - $teacher = Teacher::find($id); - if (! $teacher) { - throw new BusinessException(10101); - } - - return AetherResponse::success($teacher->toArray()); + return AetherResponse::success($this->service->detail($id)); } /** - * 更新教师信息. + * 更新教师. + * @throws Throwable */ #[PutMapping(path: 'teacher/{id}')] - public function update(int $id, RequestInterface $request): array + public function update(int $id): array { - $data = $request->all(); - $data['id'] = $id; - - $validator = $this->validator->validateUpdate($data); - if ($validator->fails()) { - throw new BusinessException(400, $validator->errors()->first()); - } - - $teacher = Teacher::find($id); - if (! $teacher) { - throw new BusinessException(10101); - } - - $teacher->fill($data); - $teacher->save(); - - return AetherResponse::success($teacher->toArray(), '教师信息更新成功'); + return AetherResponse::success($this->service->update($id, $this->request->all()), '教师更新成功'); } /** * 删除教师. + * @throws Throwable */ #[DeleteMapping(path: 'teacher/{id}')] public function delete(int $id): array { - $teacher = Teacher::find($id); - if (! $teacher) { - throw new BusinessException(10101); - } - - $teacher->delete(); - return AetherResponse::success(null, '教师删除成功'); - } - - /** - * 教师列表. - */ - #[GetMapping(path: 'teachers')] - public function list(RequestInterface $request): array - { - $data = $request->all(); - $validator = $this->validator->validateQuery($data); - if ($validator->fails()) { - throw new BusinessException(400, $validator->errors()->first()); - } - - $query = Teacher::query(); - - if (! empty($data['id'])) { - $query->where('id', $data['id']); - } - if (! empty($data['name'])) { - $query->where('name', 'like', "%{$data['name']}%"); - } - if (isset($data['campus_id'])) { - $query->where('campus_id', $data['campus_id']); - } - if (! empty($data['major_subject'])) { - $query->where('major_subject', 'like', "%{$data['major_subject']}%"); - } - if (isset($data['status'])) { - $query->where('status', $data['status']); - } - - $page = $data['page'] ?? 1; - $size = $data['size'] ?? 20; - - $total = $query->count(); - $list = $query->forPage($page, $size)->get()->toArray(); - - return AetherResponse::success([ - 'total' => $total, - 'page' => $page, - 'size' => $size, - 'list' => $list, - ]); - } - - /** - * 搜索教师. - */ - #[GetMapping(path: 'teacher')] - public function search(RequestInterface $request): array - { - $keyword = $request->input('keyword', ''); - $page = (int) $request->input('page', 1); - $size = (int) $request->input('size', 20); - - $query = Teacher::search($keyword)->enabled(); - - $total = $query->count(); - $list = $query->forPage($page, $size)->get()->toArray(); - - return AetherResponse::success([ - 'total' => $total, - 'page' => $page, - 'size' => $size, - 'list' => $list, - ]); + return AetherResponse::success($this->service->delete($id), '教师删除成功'); } } diff --git a/app/JsonRpc/Service/CampusService.php b/app/JsonRpc/Service/CampusService.php index f59a056..81e7c21 100644 --- a/app/JsonRpc/Service/CampusService.php +++ b/app/JsonRpc/Service/CampusService.php @@ -4,8 +4,13 @@ declare(strict_types=1); namespace App\JsonRpc\Service; +use Aether\AetherCrudService; +use Aether\AetherModel; +use Aether\AetherValidator; use Aether\Exception\BusinessException; -use Aether\Campus; +use App\Model\Campus; +use App\Validator\CampusValidator; +use Hyperf\Di\Annotation\Inject; use Hyperf\RpcServer\Annotation\RpcService; use MicroService\Contract\CampusServiceInterface; @@ -15,8 +20,24 @@ use MicroService\Contract\CampusServiceInterface; protocol: 'jsonrpc-http', publishTo: 'nacos' )] -class CampusService implements CampusServiceInterface +class CampusService extends AetherCrudService implements CampusServiceInterface { + #[Inject] + protected Campus $model; + + #[Inject] + protected CampusValidator $validator; + + protected function getModel(): AetherModel + { + return $this->model; + } + + protected function getValidator(): AetherValidator + { + return $this->validator; + } + public function getCampusById(int $id): array { $campus = Campus::find($id); diff --git a/app/JsonRpc/Service/TeacherService.php b/app/JsonRpc/Service/TeacherService.php index 62697c7..886544d 100644 --- a/app/JsonRpc/Service/TeacherService.php +++ b/app/JsonRpc/Service/TeacherService.php @@ -4,8 +4,13 @@ declare(strict_types=1); namespace App\JsonRpc\Service; +use Aether\AetherCrudService; +use Aether\AetherModel; +use Aether\AetherValidator; use Aether\Exception\BusinessException; -use Aether\Teacher; +use App\Model\Teacher; +use App\Validator\TeacherValidator; +use Hyperf\Di\Annotation\Inject; use Hyperf\RpcServer\Annotation\RpcService; use MicroService\Contract\TeacherServiceInterface; @@ -15,8 +20,24 @@ use MicroService\Contract\TeacherServiceInterface; protocol: 'jsonrpc-http', publishTo: 'nacos' )] -class TeacherService implements TeacherServiceInterface +class TeacherService extends AetherCrudService implements TeacherServiceInterface { + #[Inject] + protected Teacher $model; + + #[Inject] + protected TeacherValidator $validator; + + protected function getModel(): AetherModel + { + return $this->model; + } + + protected function getValidator(): AetherValidator + { + return $this->validator; + } + public function getTeacherById(int $id): array { $teacher = Teacher::find($id); diff --git a/app/Validator/CampusValidator.php b/app/Validator/CampusValidator.php index 885a424..c89d510 100644 --- a/app/Validator/CampusValidator.php +++ b/app/Validator/CampusValidator.php @@ -4,66 +4,61 @@ declare(strict_types=1); namespace App\Validator; -use Hyperf\Validation\Contract\ValidatorFactoryInterface; -use Hyperf\Validation\Validator; +use Aether\AetherValidator; -class CampusValidator +class CampusValidator extends AetherValidator { - public function __construct(protected ValidatorFactoryInterface $validationFactory) + protected function scenes(): array { - } - - public function validateCreate(array $data): Validator - { - return $this->validationFactory->make($data, [ - 'name' => 'required|string|max:255', - 'parent_id' => 'required|integer|min:0', - 'level' => 'required|integer|in:1,2,3', - 'province' => 'nullable|string|max:100', - 'city' => 'nullable|string|max:100', - 'address' => 'nullable|string|max:500', - 'contact_phone' => 'nullable|string|max:20', - 'contact_person' => 'nullable|string|max:20', - 'status' => 'nullable|integer|in:0,1', - ], [ - 'name.required' => '校区名称不能为空', - 'parent_id.required' => '父级ID不能为空', - 'level.required' => '层级不能为空', - 'level.in' => '层级只能是1、2、3', - ]); - } - - public function validateUpdate(array $data): Validator - { - return $this->validationFactory->make($data, [ - 'id' => 'required|integer|min:1', - 'name' => 'nullable|string|max:255', - 'parent_id' => 'nullable|integer|min:0', - 'level' => 'nullable|integer|in:1,2,3', - 'province' => 'nullable|string|max:100', - 'city' => 'nullable|string|max:100', - 'address' => 'nullable|string|max:500', - 'contact_phone' => 'nullable|string|max:20', - 'contact_person' => 'nullable|string|max:20', - 'status' => 'nullable|integer|in:0,1', - ], [ - 'id.required' => '校区ID不能为空', - 'level.in' => '层级只能是1、2、3', - ]); - } - - public function validateQuery(array $data): Validator - { - return $this->validationFactory->make($data, [ - 'id' => 'nullable|integer|min:1', - 'name' => 'nullable|string|max:255', - 'parent_id' => 'nullable|integer|min:0', - 'level' => 'nullable|integer|in:1,2,3', - 'province' => 'nullable|string|max:100', - 'city' => 'nullable|string|max:100', - 'status' => 'nullable|integer|in:0,1', - 'page' => 'nullable|integer|min:1', - 'size' => 'nullable|integer|min:1|max:100', - ]); + return [ + 'create' => [ + 'rules' => [ + 'name' => 'required|string|max:255', + 'parent_id' => 'required|integer|min:0', + 'level' => 'required|integer|in:1,2,3', + 'province' => 'nullable|string|max:100', + 'city' => 'nullable|string|max:100', + 'address' => 'nullable|string|max:500', + 'contact_phone' => 'nullable|string|max:20', + 'contact_person' => 'nullable|string|max:20', + 'status' => 'nullable|integer|in:0,1', + ], + 'messages' => [ + 'name.required' => '校区名称不能为空', + 'parent_id.required' => '父级ID不能为空', + 'level.required' => '层级不能为空', + 'level.in' => '层级只能是1、2、3', + ] + ], + 'update' => [ + 'rules' => [ + 'id' => 'required|integer|min:1', + 'name' => 'nullable|string|max:255', + 'parent_id' => 'nullable|integer|min:0', + 'level' => 'nullable|integer|in:1,2,3', + 'province' => 'nullable|string|max:100', + 'city' => 'nullable|string|max:100', + 'address' => 'nullable|string|max:500', + ], + 'messages' => [ + 'id.required' => '校区ID不能为空', + 'level.in' => '层级只能是1、2、3', + ] + ], + 'query' => [ + 'rules' => [ + 'id' => 'nullable|integer|min:1', + 'name' => 'nullable|string|max:255', + 'parent_id' => 'nullable|integer|min:0', + 'level' => 'nullable|integer|in:1,2,3', + 'province' => 'nullable|string|max:100', + 'city' => 'nullable|string|max:100', + 'status' => 'nullable|integer|in:0,1', + ], + 'messages' => [ + 'level.in' => '层级只能是1、2、3', + ] + ] + ]; } } diff --git a/app/Validator/TeacherValidator.php b/app/Validator/TeacherValidator.php index e9895e9..37303e1 100644 --- a/app/Validator/TeacherValidator.php +++ b/app/Validator/TeacherValidator.php @@ -4,74 +4,62 @@ declare(strict_types=1); namespace App\Validator; -use Hyperf\Validation\Contract\ValidatorFactoryInterface; -use Hyperf\Validation\Validator; +use Aether\AetherValidator; -class TeacherValidator +class TeacherValidator extends AetherValidator { - public function __construct(protected ValidatorFactoryInterface $validationFactory) + protected function scenes(): array { - } - - public function validateCreate(array $data): Validator - { - return $this->validationFactory->make($data, [ - 'name' => 'required|string|max:50', - 'age' => 'nullable|integer|min:18|max:65', - 'gender' => 'nullable|integer|in:1,2', - 'avatar' => 'nullable|url|max:255', - 'title' => 'nullable|string|max:100', - 'major_subject' => 'required|string|max:100', - 'teaching_style' => 'nullable|string', - 'introduction' => 'nullable|string', - 'campus_id' => 'nullable|integer|min:0', - 'status' => 'nullable|integer|in:0,1', - 'sort_order' => 'nullable|integer|min:0', - ], [ - 'name.required' => '教师姓名不能为空', - 'major_subject.required' => '主讲科目不能为空', - 'age.min' => '年龄不能小于18岁', - 'age.max' => '年龄不能大于65岁', - 'gender.in' => '性别只能是1(男)或2(女)', - 'avatar.url' => '头像必须是有效的URL', - ]); - } - - public function validateUpdate(array $data): Validator - { - return $this->validationFactory->make($data, [ - 'id' => 'required|integer|min:1', - 'name' => 'nullable|string|max:50', - 'age' => 'nullable|integer|min:18|max:65', - 'gender' => 'nullable|integer|in:1,2', - 'avatar' => 'nullable|url|max:255', - 'title' => 'nullable|string|max:100', - 'major_subject' => 'nullable|string|max:100', - 'teaching_style' => 'nullable|string', - 'introduction' => 'nullable|string', - 'campus_id' => 'nullable|integer|min:0', - 'status' => 'nullable|integer|in:0,1', - 'sort_order' => 'nullable|integer|min:0', - ], [ - 'id.required' => '教师ID不能为空', - 'age.min' => '年龄不能小于18岁', - 'age.max' => '年龄不能大于65岁', - 'gender.in' => '性别只能是1(男)或2(女)', - 'avatar.url' => '头像必须是有效的URL', - ]); - } - - public function validateQuery(array $data): Validator - { - return $this->validationFactory->make($data, [ - 'id' => 'nullable|integer|min:1', - 'name' => 'nullable|string|max:50', - 'campus_id' => 'nullable|integer|min:0', - 'major_subject' => 'nullable|string|max:100', - 'status' => 'nullable|integer|in:0,1', - 'page' => 'nullable|integer|min:1', - 'size' => 'nullable|integer|min:1|max:100', - 'keyword' => 'nullable|string|max:100', - ]); + return [ + 'create' => [ + 'rules' => [ + 'name' => 'required|string|max:50', + 'age' => 'nullable|integer|min:18|max:65', + 'gender' => 'nullable|integer|in:1,2', + 'avatar' => 'nullable|url|max:255', + 'title' => 'nullable|string|max:100', + 'major_subject' => 'required|string|max:100', + 'teaching_style' => 'nullable|string', + 'introduction' => 'nullable|string', + ], + 'messages' => [ + 'name.required' => '教师姓名不能为空', + 'major_subject.required' => '主讲科目不能为空', + 'age.min' => '年龄不能小于18岁', + 'age.max' => '年龄不能大于65岁', + 'gender.in' => '性别只能是1(男)或2(女)', + 'avatar.url' => '头像必须是有效的URL', + ] + ], + 'update' => [ + 'rules' => [ + 'id' => 'required|integer|min:1', + 'name' => 'nullable|string|max:50', + 'age' => 'nullable|integer|min:18|max:65', + 'gender' => 'nullable|integer|in:1,2', + 'avatar' => 'nullable|url|max:255', + 'title' => 'nullable|string|max:100', + 'major_subject' => 'nullable|string|max:100', + ], + 'messages' => [ + 'id.required' => '教师ID不能为空', + 'age.min' => '年龄不能小于18岁', + 'age.max' => '年龄不能大于65岁', + 'gender.in' => '性别只能是1(男)或2(女)', + 'avatar.url' => '头像必须是有效的URL', + ] + ], + 'query' => [ + 'rules' => [ + 'id' => 'nullable|integer|min:1', + 'name' => 'nullable|string|max:50', + 'campus_id' => 'nullable|integer|min:0', + 'major_subject' => 'nullable|string|max:100', + 'status' => 'nullable|integer|in:0,1', + 'page' => 'nullable|integer|min:1', + 'size' => 'nullable|integer|min:1|max' + ], + ] + ]; } } \ No newline at end of file diff --git a/config/autoload/middlewares.php b/config/autoload/middlewares.php index a999228..3c1dceb 100644 --- a/config/autoload/middlewares.php +++ b/config/autoload/middlewares.php @@ -1,7 +1,10 @@ [ + ApiExceptionHandler::class, ], 'jsonrpc-http' => [ RequestId::class, + RpcExceptionHandler::class, ], ]; diff --git a/extend/Aether/PHP/Hyperf/AetherController.php b/extend/Aether/PHP/Hyperf/AetherController.php index 455c961..5d502e3 100644 --- a/extend/Aether/PHP/Hyperf/AetherController.php +++ b/extend/Aether/PHP/Hyperf/AetherController.php @@ -34,7 +34,7 @@ abstract class AetherController /** * 获取单个资源 (RESTFul: GET /resources/{id}) */ - public function show(int $id): array + public function detail(int $id): array { $result = $this->getService()->detail($id); return AetherResponse::success($result); @@ -43,7 +43,7 @@ abstract class AetherController /** * 创建资源 (RESTFul: POST /resources) */ - public function store(): array + public function create(): array { $data = $this->request->all(); $id = $this->getService()->create($data); @@ -63,7 +63,7 @@ abstract class AetherController /** * 删除资源 (RESTFul: DELETE /resources/{id}) */ - public function destroy(int $id): array + public function delete(int $id): array { $this->getService()->delete($id); return AetherResponse::success(null, '删除成功'); @@ -71,8 +71,7 @@ abstract class AetherController /** * 获取对应的服务类 - * @return AbstractService + * @return AetherCrudService */ - abstract protected function getService(): AbstractService; - + abstract protected function getService(): AetherCrudService; }