From f286e18e71dcd6e43310e75eede4ac1b69f12125 Mon Sep 17 00:00:00 2001 From: Aether Date: Thu, 25 Sep 2025 08:48:23 +0800 Subject: [PATCH] .. --- app/Controller/TeacherController.php | 10 +- app/JsonRpc/Service/CampusService.php | 20 +-- app/JsonRpc/Service/TeacherService.php | 20 +-- app/Validator/CampusValidator.php | 8 +- app/Validator/TeacherValidator.php | 10 +- extend/Aether/PHP/Hyperf/AbstractModel.php | 98 ------------ extend/Aether/PHP/Hyperf/AbstractService.php | 147 ------------------ .../Aether/PHP/Hyperf/AbstractValidator.php | 67 -------- .../Aether/PHP/Hyperf/AetherCrudService.php | 24 ++- 9 files changed, 57 insertions(+), 347 deletions(-) delete mode 100644 extend/Aether/PHP/Hyperf/AbstractModel.php delete mode 100644 extend/Aether/PHP/Hyperf/AbstractService.php delete mode 100644 extend/Aether/PHP/Hyperf/AbstractValidator.php diff --git a/app/Controller/TeacherController.php b/app/Controller/TeacherController.php index 5dad47a..f80f086 100644 --- a/app/Controller/TeacherController.php +++ b/app/Controller/TeacherController.php @@ -26,11 +26,6 @@ class TeacherController extends AetherController #[Inject] protected RequestInterface $request; - protected function getService(): AetherCrudService - { - return $this->service; - } - /** * 教师列表. */ @@ -78,4 +73,9 @@ class TeacherController extends AetherController { return AetherResponse::success($this->service->delete($id), '教师删除成功'); } + + protected function getService(): AetherCrudService + { + return $this->service; + } } diff --git a/app/JsonRpc/Service/CampusService.php b/app/JsonRpc/Service/CampusService.php index 81e7c21..0a1f34b 100644 --- a/app/JsonRpc/Service/CampusService.php +++ b/app/JsonRpc/Service/CampusService.php @@ -28,16 +28,6 @@ class CampusService extends AetherCrudService implements CampusServiceInterface #[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); @@ -81,4 +71,14 @@ class CampusService extends AetherCrudService implements CampusServiceInterface ->get(['id', 'name', 'city']) ->toArray(); } + + protected function getModel(): AetherModel + { + return $this->model; + } + + protected function getValidator(): AetherValidator + { + return $this->validator; + } } diff --git a/app/JsonRpc/Service/TeacherService.php b/app/JsonRpc/Service/TeacherService.php index 886544d..7c2d157 100644 --- a/app/JsonRpc/Service/TeacherService.php +++ b/app/JsonRpc/Service/TeacherService.php @@ -28,16 +28,6 @@ class TeacherService extends AetherCrudService implements TeacherServiceInterfac #[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); @@ -106,4 +96,14 @@ class TeacherService extends AetherCrudService implements TeacherServiceInterfac 'list' => $list, ]; } + + protected function getModel(): AetherModel + { + return $this->model; + } + + protected function getValidator(): AetherValidator + { + return $this->validator; + } } diff --git a/app/Validator/CampusValidator.php b/app/Validator/CampusValidator.php index c89d510..0a81896 100644 --- a/app/Validator/CampusValidator.php +++ b/app/Validator/CampusValidator.php @@ -28,7 +28,7 @@ class CampusValidator extends AetherValidator 'parent_id.required' => '父级ID不能为空', 'level.required' => '层级不能为空', 'level.in' => '层级只能是1、2、3', - ] + ], ], 'update' => [ 'rules' => [ @@ -43,7 +43,7 @@ class CampusValidator extends AetherValidator 'messages' => [ 'id.required' => '校区ID不能为空', 'level.in' => '层级只能是1、2、3', - ] + ], ], 'query' => [ 'rules' => [ @@ -57,8 +57,8 @@ class CampusValidator extends AetherValidator ], 'messages' => [ 'level.in' => '层级只能是1、2、3', - ] - ] + ], + ], ]; } } diff --git a/app/Validator/TeacherValidator.php b/app/Validator/TeacherValidator.php index 37303e1..6d9f8df 100644 --- a/app/Validator/TeacherValidator.php +++ b/app/Validator/TeacherValidator.php @@ -29,7 +29,7 @@ class TeacherValidator extends AetherValidator 'age.max' => '年龄不能大于65岁', 'gender.in' => '性别只能是1(男)或2(女)', 'avatar.url' => '头像必须是有效的URL', - ] + ], ], 'update' => [ 'rules' => [ @@ -47,7 +47,7 @@ class TeacherValidator extends AetherValidator 'age.max' => '年龄不能大于65岁', 'gender.in' => '性别只能是1(男)或2(女)', 'avatar.url' => '头像必须是有效的URL', - ] + ], ], 'query' => [ 'rules' => [ @@ -57,9 +57,9 @@ class TeacherValidator extends AetherValidator 'major_subject' => 'nullable|string|max:100', 'status' => 'nullable|integer|in:0,1', 'page' => 'nullable|integer|min:1', - 'size' => 'nullable|integer|min:1|max' + 'size' => 'nullable|integer|min:1|max', ], - ] + ], ]; } -} \ No newline at end of file +} diff --git a/extend/Aether/PHP/Hyperf/AbstractModel.php b/extend/Aether/PHP/Hyperf/AbstractModel.php deleted file mode 100644 index eb4f8aa..0000000 --- a/extend/Aether/PHP/Hyperf/AbstractModel.php +++ /dev/null @@ -1,98 +0,0 @@ - 'integer', - 'created_at' => 'datetime', - 'updated_at' => 'datetime', - 'deleted_at' => 'datetime', - ]; - - /** - * 启用状态查询作用域 - */ - public function scopeEnabled(Builder $query): Builder - { - return $query->where('status', 1); - } - - /** - * 按ID查询并检查存在性 - * @param int $id - * @param array $columns - * @return AbstractModel - */ - public static function findOrFail(mixed $id, $columns = []): self - { - $model = self::find($id, $columns); - if (!$model) { - throw new ModelNotFoundException(); - } - return $model; - } - - /** - * 创建记录并返回实例 - */ - public static function createOne(array $data): self - { - return static::query()->create($data); - } - - /** - * 更新记录 - */ - public static function updateById(int $id, array $data): bool - { - $model = self::findOrFail($id); - return $model->update($data); - } - - /** - * 删除记录 - * @throws Exception - */ - public static function deleteById(int $id): bool - { - $model = self::findOrFail($id); - return $model->delete(); - } -} \ No newline at end of file diff --git a/extend/Aether/PHP/Hyperf/AbstractService.php b/extend/Aether/PHP/Hyperf/AbstractService.php deleted file mode 100644 index a8ed6e6..0000000 --- a/extend/Aether/PHP/Hyperf/AbstractService.php +++ /dev/null @@ -1,147 +0,0 @@ -logger = $this->loggerFactory->get($this->getLoggerName()); - } - - /** - * 获取资源列表 - */ - public function list(array $params = []): array - { - $this->validateQuery($params); - return $this->getModel()::query() - ->when(isset($params['page']), function ($query) use ($params) { - $page = (int)($params['page'] ?? 1); - $size = (int)($params['size'] ?? 20); - return $query->forPage($page, $size); - }) - ->get() - ->toArray(); - } - - /** - * 获取资源详情 - */ - public function detail(int $id): array - { - $model = $this->getModel()::find($id); - if (!$model) { - throw new BusinessException(404, '资源不存在'); - } - return $model->toArray(); - } - - /** - * 创建资源 - */ - public function create(array $data): int - { - $this->validateCreate($data); - $model = $this->getModel()::create($data); - $this->logger->info('资源创建成功', ['id' => $model->id, 'data' => $data]); - return $model->id; - } - - /** - * 更新资源 - */ - public function update(int $id, array $data): bool - { - $this->validateUpdate($data); - $model = $this->getModel()::find($id); - if (!$model) { - throw new BusinessException(404, '资源不存在'); - } - $result = $model->update($data); - $this->logger->info('资源更新成功', ['id' => $id, 'data' => $data]); - return $result; - } - - /** - * 删除资源 - */ - public function delete(int $id): bool - { - $model = $this->getModel()::find($id); - if (!$model) { - throw new BusinessException(404, '资源不存在'); - } - $result = $model->delete(); - $this->logger->info('资源删除成功', ['id' => $id]); - return $result; - } - - /** - * 验证查询参数 - */ - protected function validateQuery(array $params): void - { - $validator = $this->getValidator()->scene('query', $params); - if ($validator->fails()) { - throw new BusinessException(400, $validator->errors()->first()); - } - } - - /** - * 验证创建参数 - */ - protected function validateCreate(array $data): void - { - $validator = $this->getValidator()->scene('create', $data); - if ($validator->fails()) { - throw new BusinessException(400, $validator->errors()->first()); - } - } - - /** - * 验证更新参数 - */ - protected function validateUpdate(array $data): void - { - $validator = $this->getValidator()->scene('update', $data); - if ($validator->fails()) { - throw new BusinessException(400, $validator->errors()->first()); - } - } - - /** - * 获取日志名称 - */ - protected function getLoggerName(): string - { - return strtolower((new \ReflectionClass($this))->getShortName()); - } - - /** - * 获取对应的模型类 - * @return AbstractModel - */ - abstract protected function getModel(): AbstractModel; - - /** - * 获取对应的验证器类 - * @return AbstractValidator - */ - abstract protected function getValidator(): AbstractValidator; -} \ No newline at end of file diff --git a/extend/Aether/PHP/Hyperf/AbstractValidator.php b/extend/Aether/PHP/Hyperf/AbstractValidator.php deleted file mode 100644 index 7f90e7d..0000000 --- a/extend/Aether/PHP/Hyperf/AbstractValidator.php +++ /dev/null @@ -1,67 +0,0 @@ -getSceneRules($scene); - return $this->validationFactory->make($data, $rules, $this->messages, $this->attributes); - } - - /** - * 获取场景验证规则 - */ - protected function getSceneRules(string $scene): array - { - if (empty($this->scenes[$scene])) { - return $this->rules; - } - - $sceneRules = []; - foreach ($this->scenes[$scene] as $field) { - if (isset($this->rules[$field])) { - $sceneRules[$field] = $this->rules[$field]; - } - } - - return $sceneRules; - } -} \ No newline at end of file diff --git a/extend/Aether/PHP/Hyperf/AetherCrudService.php b/extend/Aether/PHP/Hyperf/AetherCrudService.php index ed688d4..03e8926 100644 --- a/extend/Aether/PHP/Hyperf/AetherCrudService.php +++ b/extend/Aether/PHP/Hyperf/AetherCrudService.php @@ -7,6 +7,10 @@ namespace Aether; use Aether\Contract\TreeableInterface; use Aether\Exception\BusinessException; use Hyperf\Database\Model\Builder; +use Hyperf\Di\Annotation\Inject; +use Hyperf\Logger\LoggerFactory; +use Psr\Log\LoggerInterface; +use ReflectionClass; use Throwable; /** @@ -14,10 +18,20 @@ use Throwable; */ abstract class AetherCrudService extends AetherService implements AetherCrudInterface { + #[Inject] + protected LoggerFactory $loggerFactory; + + protected LoggerInterface $logger; + protected array $search = []; protected array $ignoreSearchFields = []; + public function __construct() + { + $this->logger = $this->loggerFactory->get($this->getLoggerName()); + } + /** * 通用列表查询(支持分页和树形结构). */ @@ -71,7 +85,7 @@ abstract class AetherCrudService extends AetherService implements AetherCrudInte */ public function detail(int $id): object { - var_dump('detail'); + $this->logger->info('获取资源详情', ['id' => $id]); return $this->getModel()->findOrFailById($id); } @@ -248,6 +262,14 @@ abstract class AetherCrudService extends AetherService implements AetherCrudInte }); } + /** + * 获取日志名称. + */ + protected function getLoggerName(): string + { + return strtolower((new ReflectionClass($this))->getShortName()); + } + protected function getSearch(): array { return $this->search;