This commit is contained in:
Aether
2025-09-25 08:48:23 +08:00
parent 8be88311d4
commit f286e18e71
9 changed files with 57 additions and 347 deletions

View File

@@ -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();
}
}

View File

@@ -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;
}

View File

@@ -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;
}
}

View File

@@ -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;