This commit is contained in:
阿不叮咚
2025-09-24 16:37:22 +08:00
parent 998f8d8356
commit 1f6b60ee0f
8 changed files with 217 additions and 360 deletions

View File

@@ -5,11 +5,9 @@ declare(strict_types=1);
namespace App\Controller; namespace App\Controller;
use Aether\AetherController; use Aether\AetherController;
use Aether\AetherCrudService;
use Aether\AetherResponse; use Aether\AetherResponse;
use App\Exception\BusinessException; use App\JsonRpc\Service\CampusService;
use Aether\Campus;
use Aether\CampusValidator;
use Exception;
use Hyperf\Di\Annotation\Inject; use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller; use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\DeleteMapping; use Hyperf\HttpServer\Annotation\DeleteMapping;
@@ -17,61 +15,23 @@ use Hyperf\HttpServer\Annotation\GetMapping;
use Hyperf\HttpServer\Annotation\PostMapping; use Hyperf\HttpServer\Annotation\PostMapping;
use Hyperf\HttpServer\Annotation\PutMapping; use Hyperf\HttpServer\Annotation\PutMapping;
use Hyperf\HttpServer\Contract\RequestInterface; use Hyperf\HttpServer\Contract\RequestInterface;
use Throwable;
#[Controller(prefix: '/api/v1')] #[Controller(prefix: '/api/v1')]
class CampusController extends AetherController class CampusController extends AetherController
{ {
#[Inject] #[Inject]
protected CampusValidator $validator; protected CampusService $service;
#[Inject]
protected RequestInterface $request;
/** /**
* 校区列表. * 校区列表.
*/ */
#[GetMapping(path: 'campus/list')] #[GetMapping(path: 'campus/list')]
public function list(RequestInterface $request): array public function list(RequestInterface $request): array
{ {
$data = $request->all(); return AetherResponse::success($this->service->list($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,
]);
} }
/** /**
@@ -80,13 +40,7 @@ class CampusController extends AetherController
#[GetMapping(path: 'campus/provinces')] #[GetMapping(path: 'campus/provinces')]
public function provinces(): array public function provinces(): array
{ {
$provinces = Campus::level(1) return AetherResponse::success($this->service->getProvinces());
->enabled()
->orderBy('name')
->get(['id', 'name', 'province'])
->toArray();
return AetherResponse::success($provinces);
} }
/** /**
@@ -95,38 +49,17 @@ class CampusController extends AetherController
#[GetMapping(path: 'campus/cities')] #[GetMapping(path: 'campus/cities')]
public function cities(RequestInterface $request): array public function cities(RequestInterface $request): array
{ {
$province = $request->input('province'); return AetherResponse::success($this->service->getCitiesByProvince($request->input('province_id')));
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);
} }
/** /**
* 创建校区. * 创建校区.
* @throws Throwable
*/ */
#[PostMapping(path: 'campus')] #[PostMapping(path: 'campus')]
public function create(RequestInterface $request): array public function create(): array
{ {
$data = $request->all(); return AetherResponse::success($this->service->create($this->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(), '校区创建成功');
} }
/** /**
@@ -135,52 +68,31 @@ class CampusController extends AetherController
#[GetMapping(path: 'campus/{id}')] #[GetMapping(path: 'campus/{id}')]
public function get(int $id): array public function get(int $id): array
{ {
$campus = Campus::find($id); return AetherResponse::success($this->service->detail($id));
if (! $campus) {
throw new BusinessException(10001);
}
return AetherResponse::success($campus->toArray());
} }
/** /**
* 更新校区. * 更新校区.
* @throws Throwable
*/ */
#[PutMapping(path: 'campus/{id}')] #[PutMapping(path: 'campus/{id}')]
public function update(int $id, RequestInterface $request): array public function update(int $id): array
{ {
$data = $request->all(); return AetherResponse::success($this->service->update($id, $this->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 * @throws Throwable
*/ */
#[DeleteMapping(path: 'campus/{id}')] #[DeleteMapping(path: 'campus/{id}')]
public function delete(int $id): array public function delete(int $id): array
{ {
$campus = Campus::find($id); return AetherResponse::success($this->service->delete($id), '校区删除成功');
if (! $campus) {
throw new BusinessException(10001);
} }
$campus->delete(); protected function getService(): AetherCrudService
return AetherResponse::success(null, '校区删除成功'); {
return $this->service;
} }
} }

View File

@@ -5,10 +5,9 @@ declare(strict_types=1);
namespace App\Controller; namespace App\Controller;
use Aether\AetherController; use Aether\AetherController;
use Aether\AetherCrudService;
use Aether\AetherResponse; use Aether\AetherResponse;
use App\Exception\BusinessException; use App\JsonRpc\Service\TeacherService;
use Aether\Teacher;
use Aether\TeacherValidator;
use Hyperf\Di\Annotation\Inject; use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller; use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\DeleteMapping; use Hyperf\HttpServer\Annotation\DeleteMapping;
@@ -16,30 +15,39 @@ use Hyperf\HttpServer\Annotation\GetMapping;
use Hyperf\HttpServer\Annotation\PostMapping; use Hyperf\HttpServer\Annotation\PostMapping;
use Hyperf\HttpServer\Annotation\PutMapping; use Hyperf\HttpServer\Annotation\PutMapping;
use Hyperf\HttpServer\Contract\RequestInterface; use Hyperf\HttpServer\Contract\RequestInterface;
use Throwable;
#[Controller(prefix: '/api/v1/')] #[Controller(prefix: '/api/v1')]
class TeacherController extends AetherController class TeacherController extends AetherController
{ {
#[Inject] #[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')] #[PostMapping(path: 'teacher')]
public function create(RequestInterface $request): array public function create(): array
{ {
$data = $request->all(); return AetherResponse::success($this->service->create($this->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(), '教师创建成功');
} }
/** /**
@@ -48,118 +56,26 @@ class TeacherController extends AetherController
#[GetMapping(path: 'teacher/{id}')] #[GetMapping(path: 'teacher/{id}')]
public function get(int $id): array public function get(int $id): array
{ {
$teacher = Teacher::find($id); return AetherResponse::success($this->service->detail($id));
if (! $teacher) {
throw new BusinessException(10101);
}
return AetherResponse::success($teacher->toArray());
} }
/** /**
* 更新教师信息. * 更新教师.
* @throws Throwable
*/ */
#[PutMapping(path: 'teacher/{id}')] #[PutMapping(path: 'teacher/{id}')]
public function update(int $id, RequestInterface $request): array public function update(int $id): array
{ {
$data = $request->all(); return AetherResponse::success($this->service->update($id, $this->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(), '教师信息更新成功');
} }
/** /**
* 删除教师. * 删除教师.
* @throws Throwable
*/ */
#[DeleteMapping(path: 'teacher/{id}')] #[DeleteMapping(path: 'teacher/{id}')]
public function delete(int $id): array public function delete(int $id): array
{ {
$teacher = Teacher::find($id); return AetherResponse::success($this->service->delete($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,
]);
} }
} }

View File

@@ -4,8 +4,13 @@ declare(strict_types=1);
namespace App\JsonRpc\Service; namespace App\JsonRpc\Service;
use Aether\AetherCrudService;
use Aether\AetherModel;
use Aether\AetherValidator;
use Aether\Exception\BusinessException; 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 Hyperf\RpcServer\Annotation\RpcService;
use MicroService\Contract\CampusServiceInterface; use MicroService\Contract\CampusServiceInterface;
@@ -15,8 +20,24 @@ use MicroService\Contract\CampusServiceInterface;
protocol: 'jsonrpc-http', protocol: 'jsonrpc-http',
publishTo: 'nacos' 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 public function getCampusById(int $id): array
{ {
$campus = Campus::find($id); $campus = Campus::find($id);

View File

@@ -4,8 +4,13 @@ declare(strict_types=1);
namespace App\JsonRpc\Service; namespace App\JsonRpc\Service;
use Aether\AetherCrudService;
use Aether\AetherModel;
use Aether\AetherValidator;
use Aether\Exception\BusinessException; 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 Hyperf\RpcServer\Annotation\RpcService;
use MicroService\Contract\TeacherServiceInterface; use MicroService\Contract\TeacherServiceInterface;
@@ -15,8 +20,24 @@ use MicroService\Contract\TeacherServiceInterface;
protocol: 'jsonrpc-http', protocol: 'jsonrpc-http',
publishTo: 'nacos' 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 public function getTeacherById(int $id): array
{ {
$teacher = Teacher::find($id); $teacher = Teacher::find($id);

View File

@@ -4,18 +4,15 @@ declare(strict_types=1);
namespace App\Validator; namespace App\Validator;
use Hyperf\Validation\Contract\ValidatorFactoryInterface; use Aether\AetherValidator;
use Hyperf\Validation\Validator;
class CampusValidator class CampusValidator extends AetherValidator
{ {
public function __construct(protected ValidatorFactoryInterface $validationFactory) protected function scenes(): array
{ {
} return [
'create' => [
public function validateCreate(array $data): Validator 'rules' => [
{
return $this->validationFactory->make($data, [
'name' => 'required|string|max:255', 'name' => 'required|string|max:255',
'parent_id' => 'required|integer|min:0', 'parent_id' => 'required|integer|min:0',
'level' => 'required|integer|in:1,2,3', 'level' => 'required|integer|in:1,2,3',
@@ -25,17 +22,16 @@ class CampusValidator
'contact_phone' => 'nullable|string|max:20', 'contact_phone' => 'nullable|string|max:20',
'contact_person' => 'nullable|string|max:20', 'contact_person' => 'nullable|string|max:20',
'status' => 'nullable|integer|in:0,1', 'status' => 'nullable|integer|in:0,1',
], [ ],
'messages' => [
'name.required' => '校区名称不能为空', 'name.required' => '校区名称不能为空',
'parent_id.required' => '父级ID不能为空', 'parent_id.required' => '父级ID不能为空',
'level.required' => '层级不能为空', 'level.required' => '层级不能为空',
'level.in' => '层级只能是1、2、3', 'level.in' => '层级只能是1、2、3',
]); ]
} ],
'update' => [
public function validateUpdate(array $data): Validator 'rules' => [
{
return $this->validationFactory->make($data, [
'id' => 'required|integer|min:1', 'id' => 'required|integer|min:1',
'name' => 'nullable|string|max:255', 'name' => 'nullable|string|max:255',
'parent_id' => 'nullable|integer|min:0', 'parent_id' => 'nullable|integer|min:0',
@@ -43,18 +39,14 @@ class CampusValidator
'province' => 'nullable|string|max:100', 'province' => 'nullable|string|max:100',
'city' => 'nullable|string|max:100', 'city' => 'nullable|string|max:100',
'address' => 'nullable|string|max:500', 'address' => 'nullable|string|max:500',
'contact_phone' => 'nullable|string|max:20', ],
'contact_person' => 'nullable|string|max:20', 'messages' => [
'status' => 'nullable|integer|in:0,1',
], [
'id.required' => '校区ID不能为空', 'id.required' => '校区ID不能为空',
'level.in' => '层级只能是1、2、3', 'level.in' => '层级只能是1、2、3',
]); ]
} ],
'query' => [
public function validateQuery(array $data): Validator 'rules' => [
{
return $this->validationFactory->make($data, [
'id' => 'nullable|integer|min:1', 'id' => 'nullable|integer|min:1',
'name' => 'nullable|string|max:255', 'name' => 'nullable|string|max:255',
'parent_id' => 'nullable|integer|min:0', 'parent_id' => 'nullable|integer|min:0',
@@ -62,8 +54,11 @@ class CampusValidator
'province' => 'nullable|string|max:100', 'province' => 'nullable|string|max:100',
'city' => 'nullable|string|max:100', 'city' => 'nullable|string|max:100',
'status' => 'nullable|integer|in:0,1', 'status' => 'nullable|integer|in:0,1',
'page' => 'nullable|integer|min:1', ],
'size' => 'nullable|integer|min:1|max:100', 'messages' => [
]); 'level.in' => '层级只能是1、2、3',
]
]
];
} }
} }

View File

@@ -4,18 +4,15 @@ declare(strict_types=1);
namespace App\Validator; namespace App\Validator;
use Hyperf\Validation\Contract\ValidatorFactoryInterface; use Aether\AetherValidator;
use Hyperf\Validation\Validator;
class TeacherValidator class TeacherValidator extends AetherValidator
{ {
public function __construct(protected ValidatorFactoryInterface $validationFactory) protected function scenes(): array
{ {
} return [
'create' => [
public function validateCreate(array $data): Validator 'rules' => [
{
return $this->validationFactory->make($data, [
'name' => 'required|string|max:50', 'name' => 'required|string|max:50',
'age' => 'nullable|integer|min:18|max:65', 'age' => 'nullable|integer|min:18|max:65',
'gender' => 'nullable|integer|in:1,2', 'gender' => 'nullable|integer|in:1,2',
@@ -24,22 +21,18 @@ class TeacherValidator
'major_subject' => 'required|string|max:100', 'major_subject' => 'required|string|max:100',
'teaching_style' => 'nullable|string', 'teaching_style' => 'nullable|string',
'introduction' => 'nullable|string', 'introduction' => 'nullable|string',
'campus_id' => 'nullable|integer|min:0', ],
'status' => 'nullable|integer|in:0,1', 'messages' => [
'sort_order' => 'nullable|integer|min:0',
], [
'name.required' => '教师姓名不能为空', 'name.required' => '教师姓名不能为空',
'major_subject.required' => '主讲科目不能为空', 'major_subject.required' => '主讲科目不能为空',
'age.min' => '年龄不能小于18岁', 'age.min' => '年龄不能小于18岁',
'age.max' => '年龄不能大于65岁', 'age.max' => '年龄不能大于65岁',
'gender.in' => '性别只能是1(男)或2(女)', 'gender.in' => '性别只能是1(男)或2(女)',
'avatar.url' => '头像必须是有效的URL', 'avatar.url' => '头像必须是有效的URL',
]); ]
} ],
'update' => [
public function validateUpdate(array $data): Validator 'rules' => [
{
return $this->validationFactory->make($data, [
'id' => 'required|integer|min:1', 'id' => 'required|integer|min:1',
'name' => 'nullable|string|max:50', 'name' => 'nullable|string|max:50',
'age' => 'nullable|integer|min:18|max:65', 'age' => 'nullable|integer|min:18|max:65',
@@ -47,31 +40,26 @@ class TeacherValidator
'avatar' => 'nullable|url|max:255', 'avatar' => 'nullable|url|max:255',
'title' => 'nullable|string|max:100', 'title' => 'nullable|string|max:100',
'major_subject' => 'nullable|string|max:100', 'major_subject' => 'nullable|string|max:100',
'teaching_style' => 'nullable|string', ],
'introduction' => 'nullable|string', 'messages' => [
'campus_id' => 'nullable|integer|min:0',
'status' => 'nullable|integer|in:0,1',
'sort_order' => 'nullable|integer|min:0',
], [
'id.required' => '教师ID不能为空', 'id.required' => '教师ID不能为空',
'age.min' => '年龄不能小于18岁', 'age.min' => '年龄不能小于18岁',
'age.max' => '年龄不能大于65岁', 'age.max' => '年龄不能大于65岁',
'gender.in' => '性别只能是1(男)或2(女)', 'gender.in' => '性别只能是1(男)或2(女)',
'avatar.url' => '头像必须是有效的URL', 'avatar.url' => '头像必须是有效的URL',
]); ]
} ],
'query' => [
public function validateQuery(array $data): Validator 'rules' => [
{
return $this->validationFactory->make($data, [
'id' => 'nullable|integer|min:1', 'id' => 'nullable|integer|min:1',
'name' => 'nullable|string|max:50', 'name' => 'nullable|string|max:50',
'campus_id' => 'nullable|integer|min:0', 'campus_id' => 'nullable|integer|min:0',
'major_subject' => 'nullable|string|max:100', 'major_subject' => 'nullable|string|max:100',
'status' => 'nullable|integer|in:0,1', 'status' => 'nullable|integer|in:0,1',
'page' => 'nullable|integer|min:1', 'page' => 'nullable|integer|min:1',
'size' => 'nullable|integer|min:1|max:100', 'size' => 'nullable|integer|min:1|max'
'keyword' => 'nullable|string|max:100', ],
]); ]
];
} }
} }

View File

@@ -1,7 +1,10 @@
<?php <?php
declare(strict_types=1); declare(strict_types=1);
use Aether\ApiExceptionHandler;
use Aether\Middleware\RequestId; use Aether\Middleware\RequestId;
use Aether\RpcExceptionHandler;
/* /*
* This file is part of Hyperf. * This file is part of Hyperf.
@@ -14,8 +17,10 @@ use Aether\Middleware\RequestId;
return [ return [
'http' => [ 'http' => [
ApiExceptionHandler::class,
], ],
'jsonrpc-http' => [ 'jsonrpc-http' => [
RequestId::class, RequestId::class,
RpcExceptionHandler::class,
], ],
]; ];

View File

@@ -34,7 +34,7 @@ abstract class AetherController
/** /**
* 获取单个资源 (RESTFul: GET /resources/{id}) * 获取单个资源 (RESTFul: GET /resources/{id})
*/ */
public function show(int $id): array public function detail(int $id): array
{ {
$result = $this->getService()->detail($id); $result = $this->getService()->detail($id);
return AetherResponse::success($result); return AetherResponse::success($result);
@@ -43,7 +43,7 @@ abstract class AetherController
/** /**
* 创建资源 (RESTFul: POST /resources) * 创建资源 (RESTFul: POST /resources)
*/ */
public function store(): array public function create(): array
{ {
$data = $this->request->all(); $data = $this->request->all();
$id = $this->getService()->create($data); $id = $this->getService()->create($data);
@@ -63,7 +63,7 @@ abstract class AetherController
/** /**
* 删除资源 (RESTFul: DELETE /resources/{id}) * 删除资源 (RESTFul: DELETE /resources/{id})
*/ */
public function destroy(int $id): array public function delete(int $id): array
{ {
$this->getService()->delete($id); $this->getService()->delete($id);
return AetherResponse::success(null, '删除成功'); 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;
} }