询单接口&调整脚手架
This commit is contained in:
@@ -38,14 +38,6 @@ trait CrudLogicTrait
|
||||
return CrudUtil::findOneBy(CrudUtil::buildQuery($config));
|
||||
}
|
||||
|
||||
/**
|
||||
* 任意条件查询单条(可带 dataLimit)
|
||||
*/
|
||||
// public function logicShowBy(array $where, array $with = [], array $dataLimit = [])
|
||||
// {
|
||||
// return CrudUtil::findOneBy($this->modelClass, $where, $with, $dataLimit);
|
||||
// }
|
||||
|
||||
/**
|
||||
* 创建(可带 dataLimit)
|
||||
*/
|
||||
@@ -54,14 +46,6 @@ trait CrudLogicTrait
|
||||
return CrudUtil::create($this->modelClass, $data, $dataLimit);
|
||||
}
|
||||
|
||||
/**
|
||||
* 主键更新(可带 dataLimit)
|
||||
*/
|
||||
// public function logicUpdateByPk($id, array $data, array $dataLimit = [])
|
||||
// {
|
||||
// return CrudUtil::updateByPk($this->modelClass, $id, $data, $dataLimit);
|
||||
// }
|
||||
|
||||
/**
|
||||
* 任意条件更新(可带 dataLimit)
|
||||
*/
|
||||
@@ -70,14 +54,6 @@ trait CrudLogicTrait
|
||||
return CrudUtil::updateBy(CrudUtil::buildQuery($config), $data);
|
||||
}
|
||||
|
||||
/**
|
||||
* 主键删除(批量)
|
||||
*/
|
||||
// public function logicDeleteByPk(array $ids)
|
||||
// {
|
||||
// return CrudUtil::deleteByPk($this->modelClass, $ids);
|
||||
// }
|
||||
|
||||
/**
|
||||
* 任意条件删除(可带 dataLimit)
|
||||
*/
|
||||
|
||||
@@ -32,8 +32,8 @@ trait CrudServiceTrait
|
||||
$result = $this->complexQuery($request->get());
|
||||
} else {
|
||||
$config = $this->buildQueryConfig($request, 'index');
|
||||
$limit = (int)$request->get('limit', 15);
|
||||
$page = (int)$request->get('page', 1);
|
||||
$limit = (int)$request->get('size', 15);
|
||||
$page = (int)$request->get('current', 1);
|
||||
$result = $this->logicList($config, $limit, $page);
|
||||
}
|
||||
return $this->postProcess('List', $result);
|
||||
@@ -62,7 +62,7 @@ trait CrudServiceTrait
|
||||
/** 创建 */
|
||||
public function serviceStore(Request $request, array $data = [])
|
||||
{
|
||||
// $data = $request->post();
|
||||
$data = $this->preProcess('Store', $data, $request);
|
||||
$dl = $this->getDataLimitFromRequest($request);
|
||||
$created = $this->logicCreate($data, $dl);
|
||||
return $this->postProcess('Store', $created);
|
||||
@@ -71,7 +71,7 @@ trait CrudServiceTrait
|
||||
/** 更新 */
|
||||
public function serviceUpdate(Request $request, array $data = [])
|
||||
{
|
||||
// $data= $request->post();
|
||||
$data = $this->preProcess('Update', $data, $request);
|
||||
$config = $this->buildQueryConfig($request, 'update');
|
||||
$updated = $this->logicUpdateBy($config, $data);
|
||||
return $this->postProcess('Update', $updated);
|
||||
@@ -88,16 +88,7 @@ trait CrudServiceTrait
|
||||
/** ----------------- 公共辅助方法 ----------------- */
|
||||
|
||||
/**
|
||||
* 构造 CrudUtil 的查询配置1
|
||||
*
|
||||
* 前端可以通过以下参数控制:
|
||||
* - filter_<column>=<value> → where
|
||||
* - sort_by=<column>&order=<asc|desc>
|
||||
* - fields=id,name,email → select
|
||||
* - with=roles,posts → 关联
|
||||
* - withFields=roles:id,name;... → 关联字段
|
||||
* - dataLimitType=personal|auth
|
||||
* - dataLimitField=admin_id
|
||||
* 构造 CrudUtil 查询配置
|
||||
*/
|
||||
protected function buildQueryConfig(Request $request, string $scene = 'default'): array
|
||||
{
|
||||
@@ -126,42 +117,11 @@ trait CrudServiceTrait
|
||||
$config['where']['id'] = $p['id'];
|
||||
break;
|
||||
case 'destroy':
|
||||
$config['where']['id'] = $p['id'];
|
||||
// $config['where']['id'] = $p['id'];
|
||||
break;
|
||||
}
|
||||
|
||||
return $config;
|
||||
|
||||
// // 1. where 从 filter_ 前缀自动映射
|
||||
// $where = [];
|
||||
// foreach ($p as $key => $val) {
|
||||
// if (strpos($key, 'filter_') === 0 && $val !== '') {
|
||||
// $col = substr($key, 7);
|
||||
// // 支持 %like% 查询
|
||||
// $where[$col] = strpos($val, '%') !== false
|
||||
// ? ['like', $val]
|
||||
// : $val;
|
||||
// }
|
||||
// }
|
||||
|
||||
// // 2. 其余配置
|
||||
// return [
|
||||
// 'model' => $this->modelClass,
|
||||
// 'where' => $where,
|
||||
// 'order' => [
|
||||
// $p['sort_by'] ?? 'id' => $p['order'] ?? 'desc'
|
||||
// ],
|
||||
// 'fields' => !empty($p['fields'])
|
||||
// ? explode(',', $p['fields'])
|
||||
// : [],
|
||||
// 'with' => !empty($p['with'])
|
||||
// ? explode(',', $p['with'])
|
||||
// : [],
|
||||
// 'withFields' => $this->parseWithFields(
|
||||
// $p['withFields'] ?? ''
|
||||
// ),
|
||||
// 'dataLimit' => $this->getDataLimitFromRequest($request),
|
||||
// ];
|
||||
}
|
||||
|
||||
/** 解析 withFields 字符串 "rel:field,field;..." */
|
||||
@@ -191,14 +151,20 @@ trait CrudServiceTrait
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理复杂查询
|
||||
* 覆盖此方法,实现复杂查询逻辑
|
||||
/**
|
||||
* $data 前置处理
|
||||
*
|
||||
* @param array $data
|
||||
* @param Request $request
|
||||
* @return array
|
||||
*/
|
||||
// protected function complexQuery(array $params): array
|
||||
// {
|
||||
// throw new \LogicException('请在子类实现 complexQuery');
|
||||
// }
|
||||
protected function preProcess(string $op, mixed $data, Request $request): mixed
|
||||
{
|
||||
$hook = "beforeService{$op}";
|
||||
return method_exists($this, $hook)
|
||||
? $this->{$hook}($data, $request)
|
||||
: $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据操作类型,调用对应的后置钩子
|
||||
@@ -214,4 +180,14 @@ trait CrudServiceTrait
|
||||
? $this->{$hook}($data)
|
||||
: $data;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 处理复杂查询
|
||||
* 覆盖此方法,实现复杂查询逻辑
|
||||
*/
|
||||
// protected function complexQuery(array $params): array
|
||||
// {
|
||||
// throw new \LogicException('请在子类实现 complexQuery');
|
||||
// }
|
||||
}
|
||||
Reference in New Issue
Block a user