crud调整

This commit is contained in:
2025-07-21 15:09:06 +08:00
parent 228408149b
commit 45679bc5e4
15 changed files with 318 additions and 278 deletions
+38
View File
@@ -0,0 +1,38 @@
<?php
namespace app\common\traits;
use think\Validate;
use support\Request;
use support\exception\BusinessException;
trait RequestTrait
{
/**
* 验证数据
*
* @param Validate $validator 验证器实例
* @param array $data 待验证的数据
* @param string $scene 验证场景
* @return array 验证后的数据
* @throws BusinessException 验证失败时抛出业务异常
*/
public function validate(Validate $validator, array $data, string $scene = ''): array
{
// 如果指定了验证场景,并且验证器支持场景方法,则设置验证场景
if ($scene && method_exists($validator, 'scene')) {
$validator->scene($scene);
}
// 执行数据验证
if (!$validator->check($data)) {
// 验证失败,抛出业务异常,异常信息为验证器返回的错误信息
throw new BusinessException($validator->getError());
}
// 验证成功,返回原始数据
return $data;
}
}
+43 -21
View File
@@ -11,22 +11,26 @@ use support\Request;
trait CrudControllerTrait
{
/**
* 获取资源列表(GET /resource)
* 获取列表
*
* @param Request $request 请求对象
* @return mixed 返回分页数据及总数
*/
public function index(Request $request)
{
$raw = $this->service->serviceList($request);
$data = $this->transformData($raw, method_exists($this, 'indexFormat')
? [$this, 'indexFormat']
: null);
return $this->success($data);
try {
$raw = $this->service->serviceList($request);
$data = $this->transformData($raw, method_exists($this, 'indexFormat')
? [$this, 'indexFormat']
: null);
return $this->success($data);
} catch (\Exception $e) {
return $this->error($e->getMessage());
}
}
/**
* 获取指定资源详情(GET /resource/{id})
* 获取详情
*
* @param Request $request 请求对象
* @param int|string $id 资源 ID
@@ -34,47 +38,65 @@ trait CrudControllerTrait
*/
public function show(Request $request)
{
$raw = $this->service->serviceShow($request);
$data = $this->transformData($raw, method_exists($this, 'showFormat')
? [$this, 'showFormat']
: null);
return $this->success($data);
try {
$raw = $this->service->serviceShow($request);
$data = $this->transformData($raw, method_exists($this, 'showFormat')
? [$this, 'showFormat']
: null);
return $this->success($data);
} catch (\Exception $e) {
return $this->error($e->getMessage());
}
}
/**
* 创建新资源(POST /resource)
* 创建
*
* @param Request $request 请求对象
* @return mixed 返回创建成功的数据或信息
*/
public function store(Request $request)
{
$res = $this->service->serviceStore($request);
return $this->success($res, '创建成功');
try {
$data = $this->validate($this->service->getValidator(), $request->post(), 'create');
$res = $this->service->serviceStore($request, $data);
return $this->success($res, '创建成功');
} catch (\Exception $e) {
return $this->error($e->getMessage());
}
}
/**
* 更新指定资源(PUT/PATCH /resource/{id})
* 更新
*
* @param Request $request 请求对象
* @return mixed 返回更新成功的数据或信息
*/
public function update(Request $request)
{
$res = $this->service->serviceUpdate($request);
return $this->success($res, '更新成功');
try {
$data = $this->validate($this->service->getValidator(), $request->post(), 'update');
$res = $this->service->serviceUpdate($request, $data);
return $this->success([], '更新成功');
} catch (\Exception $e) {
return $this->error($e->getMessage());
}
}
/**
* 删除指定资源(DELETE /resource/{id})
* 删除
*
* @param Request $request 请求对象
* @return mixed 返回删除操作结果
*/
public function destroy(Request $request)
{
$count = $this->service->serviceDestroy($request);
return $this->success([], "删除了 {$count} 条");
try {
$count = $this->service->serviceDestroy($request);
return $this->success([], "成功删除了 {$count} 条");
} catch (\Exception $e) {
return $this->error($e->getMessage());
}
}
+16 -8
View File
@@ -17,6 +17,14 @@ trait CrudServiceTrait
/** 具体 Service 里定义:protected string $modelClass = YourModel::class; */
/**
* 获取验证器
*/
public function getValidator()
{
return $this->validator;
}
/** 分页列表 */
public function serviceList(Request $request)
{
@@ -52,18 +60,18 @@ trait CrudServiceTrait
}
/** 创建 */
public function serviceStore(Request $request)
public function serviceStore(Request $request, array $data = [])
{
$data = $request->post();
// $data = $request->post();
$dl = $this->getDataLimitFromRequest($request);
$created = $this->logicCreate($data, $dl);
return $this->postProcess('Store', $created);
}
/** 更新 */
public function serviceUpdate(Request $request)
public function serviceUpdate(Request $request, array $data = [])
{
$data= $request->post();
// $data= $request->post();
$config = $this->buildQueryConfig($request, 'update');
$updated = $this->logicUpdateBy($config, $data);
return $this->postProcess('Update', $updated);
@@ -100,13 +108,13 @@ trait CrudServiceTrait
switch ($scene) {
case 'default':
$config['where']['status'] = 1;
$config['order']['sort'] = ['desc'];
$config['order']['id'] = ['desc'];
$config['order']['sort'] = 'desc';
$config['order']['id'] = 'desc';
break;
case 'index':
$config['where']['status'] = 1;
$config['order']['sort'] = ['desc'];
$config['order']['id'] = ['desc'];
$config['order']['sort'] = 'desc';
$config['order']['id'] = 'desc';
break;
case 'show':
$config['where']['id'] = $p['id'];