..
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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',
|
||||
]
|
||||
]
|
||||
],
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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',
|
||||
],
|
||||
],
|
||||
]
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -1,98 +0,0 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Aether;
|
||||
|
||||
use Exception;
|
||||
use Hyperf\Database\Model\Builder;
|
||||
use Hyperf\Database\Model\ModelNotFoundException;
|
||||
use Hyperf\Database\Model\SoftDeletes;
|
||||
|
||||
abstract class AbstractModel extends AetherModel
|
||||
{
|
||||
use SoftDeletes;
|
||||
|
||||
/**
|
||||
* 时间戳字段格式
|
||||
* @var string|null
|
||||
*/
|
||||
protected ?string $dateFormat = 'Y-m-d H:i:s';
|
||||
|
||||
/**
|
||||
* 可批量赋值的字段
|
||||
* @var array
|
||||
*/
|
||||
protected array $fillable = [];
|
||||
|
||||
/**
|
||||
* 日期字段
|
||||
* @var array
|
||||
*/
|
||||
protected array $dates = [
|
||||
'created_at',
|
||||
'updated_at',
|
||||
'deleted_at',
|
||||
];
|
||||
|
||||
/**
|
||||
* 字段类型转换
|
||||
* @var array
|
||||
*/
|
||||
protected array $casts = [
|
||||
'id' => '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();
|
||||
}
|
||||
}
|
||||
@@ -1,147 +0,0 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Aether;
|
||||
|
||||
use App\Exception\BusinessException;
|
||||
use Hyperf\Di\Annotation\Inject;
|
||||
use Hyperf\Logger\LoggerFactory;
|
||||
use Psr\Log\LoggerInterface;
|
||||
|
||||
abstract class AbstractService
|
||||
{
|
||||
#[Inject]
|
||||
protected LoggerFactory $loggerFactory;
|
||||
|
||||
/**
|
||||
* @var LoggerInterface
|
||||
*/
|
||||
protected LoggerInterface $logger;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->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;
|
||||
}
|
||||
@@ -1,67 +0,0 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Aether;
|
||||
|
||||
use Hyperf\Validation\Contract\ValidatorFactoryInterface;
|
||||
use Hyperf\Validation\Validator;
|
||||
use Hyperf\Di\Annotation\Inject;
|
||||
|
||||
abstract class AbstractValidator
|
||||
{
|
||||
#[Inject]
|
||||
protected ValidatorFactoryInterface $validationFactory;
|
||||
|
||||
/**
|
||||
* 验证场景规则
|
||||
* @var array
|
||||
*/
|
||||
protected array $scenes = [];
|
||||
|
||||
/**
|
||||
* 默认验证规则
|
||||
* @var array
|
||||
*/
|
||||
protected array $rules = [];
|
||||
|
||||
/**
|
||||
* 验证消息
|
||||
* @var array
|
||||
*/
|
||||
protected array $messages = [];
|
||||
|
||||
/**
|
||||
* 属性名称
|
||||
* @var array
|
||||
*/
|
||||
protected array $attributes = [];
|
||||
|
||||
/**
|
||||
* 获取验证器实例
|
||||
*/
|
||||
public function scene(string $scene, array $data): Validator
|
||||
{
|
||||
$rules = $this->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;
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user