202 lines
6.1 KiB
PHP
202 lines
6.1 KiB
PHP
<?php
|
|
namespace app\common\traits\crud;
|
|
|
|
use support\exception\BusinessException;
|
|
use support\Request;
|
|
|
|
/**
|
|
* Trait CrudServiceTrait
|
|
*
|
|
* 提供标准的增删改查 Service 接口,依赖 CrudLogicTrait。
|
|
* 使用此 Trait 的 Service 类,须:
|
|
* use CrudLogicTrait, CrudServiceTrait;
|
|
* protected string $modelClass = \App\common\model\YourModel::class;
|
|
*/
|
|
trait CrudServiceTrait
|
|
{
|
|
use CrudLogicTrait;
|
|
|
|
/** 具体 Service 里定义:protected string $modelClass = YourModel::class; */
|
|
|
|
/**
|
|
* 获取验证器
|
|
*/
|
|
public function getValidator()
|
|
{
|
|
return $this->validator;
|
|
}
|
|
|
|
/** 分页列表 */
|
|
public function serviceList(Request $request, string $scene = 'index')
|
|
{
|
|
$result = $this->cqyProcess(ucfirst($scene), $request);
|
|
if ($result === false) {
|
|
$config = $this->buildQueryConfig($request, $scene);
|
|
$limit = (int)$request->get('size', 15);
|
|
$page = (int)$request->get('current', 1);
|
|
$result = $this->logicList($config, $limit, $page);
|
|
}
|
|
return $this->postProcess(ucfirst($scene), $result);
|
|
}
|
|
|
|
/** 全量列表 */
|
|
public function serviceListAll(Request $request, string $scene = 'default')
|
|
{
|
|
$result = $this->cqyProcess(ucfirst($scene), $request);
|
|
if ($result === false) {
|
|
$config = $this->buildQueryConfig($request, $scene);
|
|
$result = $this->logicListAll($config);
|
|
}
|
|
return $this->postProcess(ucfirst($scene), $result);
|
|
}
|
|
|
|
/** 查看单条 */
|
|
public function serviceShow(Request $request, string $scene = 'show')
|
|
{
|
|
$config = $this->buildQueryConfig($request, $scene);
|
|
$item = $this->logicShow($config);
|
|
return $this->postProcess(ucfirst($scene), $item);
|
|
}
|
|
|
|
/** 创建 */
|
|
public function serviceStore(Request $request, array $data = [], string $scene = 'store')
|
|
{
|
|
$data = $this->preProcess(ucfirst($scene), $data, $request);
|
|
$dl = $this->getDataLimitFromRequest($request);
|
|
$created = $this->logicCreate($data, $dl);
|
|
return $this->postProcess(ucfirst($scene), $created);
|
|
}
|
|
|
|
/** 更新 */
|
|
public function serviceUpdate(Request $request, array $data = [], string $scene = 'update')
|
|
{
|
|
$data = $this->preProcess(ucfirst($scene), $data, $request);
|
|
$config = $this->buildQueryConfig($request, $scene);
|
|
$updated = $this->logicUpdateBy($config, $data);
|
|
return $this->postProcess(ucfirst($scene), $data);
|
|
}
|
|
|
|
/** 删除 */
|
|
public function serviceDestroy(Request $request, string $scene = 'destroy')
|
|
{
|
|
$config = $this->buildQueryConfig($request, $scene);
|
|
$deleted = $this->logicDeleteBy($config);
|
|
return $this->postProcess(ucfirst($scene), $deleted);
|
|
}
|
|
|
|
/** ----------------- 公共辅助方法 ----------------- */
|
|
|
|
/**
|
|
* 构造 CrudUtil 查询配置
|
|
*/
|
|
protected function buildQueryConfig(Request $request, string $scene = 'default'): array
|
|
{
|
|
$p = $request->all();
|
|
|
|
$config = [];
|
|
$config['model'] = $this->modelClass;
|
|
switch ($scene) {
|
|
case 'default':
|
|
$config['where']['status'] = 1;
|
|
$config['order']['sort'] = 'desc';
|
|
$config['order']['id'] = 'desc';
|
|
break;
|
|
case 'index':
|
|
$config['where']['status'] = 1;
|
|
$config['order']['sort'] = 'desc';
|
|
$config['order']['id'] = 'desc';
|
|
break;
|
|
case 'show':
|
|
$config['where']['id'] = $p['id'];
|
|
$config['where']['status'] = 1;
|
|
break;
|
|
case 'store':
|
|
break;
|
|
case 'update':
|
|
$config['where']['id'] = $p['id'];
|
|
break;
|
|
case 'destroy':
|
|
$config['where']['id'] = $p['id'];
|
|
break;
|
|
}
|
|
|
|
return $config;
|
|
}
|
|
|
|
/** 解析 withFields 字符串 "rel:field,field;..." */
|
|
protected function parseWithFields(string $str): array
|
|
{
|
|
if (!$str) {
|
|
return [];
|
|
}
|
|
$out = [];
|
|
foreach (explode(';', $str) as $pair) {
|
|
[$rel, $flds] = explode(':', $pair);
|
|
$out[$rel] = explode(',', $flds);
|
|
}
|
|
return $out;
|
|
}
|
|
|
|
/** 从请求读取 dataLimitType, dataLimitField */
|
|
protected function getDataLimitFromRequest(Request $req): array
|
|
{
|
|
$type = $req->get('dataLimitType');
|
|
if (!$type) {
|
|
return [];
|
|
}
|
|
return [
|
|
'type' => $type,
|
|
'field' => $req->get('dataLimitField', 'admin_id'),
|
|
];
|
|
}
|
|
|
|
/**
|
|
* $data 前置处理
|
|
*
|
|
* @param array $data
|
|
* @param Request $request
|
|
* @return array
|
|
*/
|
|
protected function preProcess(string $op, mixed $data, Request $request): mixed
|
|
{
|
|
$hook = "beforeService{$op}";
|
|
return method_exists($this, $hook)
|
|
? $this->{$hook}($data, $request)
|
|
: $data;
|
|
}
|
|
|
|
/**
|
|
* 根据操作类型,调用对应的后置钩子
|
|
*
|
|
* @param string $op 操作:List, Show, Store, Update, Destroy
|
|
* @param mixed $data 原始返回值
|
|
* @return mixed 处理后的返回值
|
|
*/
|
|
protected function postProcess(string $op, mixed $data): mixed
|
|
{
|
|
$hook = "afterService{$op}";
|
|
if (method_exists($this, $hook)) {
|
|
$args = func_get_args();
|
|
array_shift($args);
|
|
array_shift($args);
|
|
$params = array_merge([$data], $args);
|
|
return call_user_func_array([$this, $hook], $params);
|
|
}
|
|
return $data;
|
|
}
|
|
|
|
/**
|
|
* 复杂/自定义查询
|
|
*
|
|
* @param string $op 场景
|
|
* @param mixed $data 原始返回值
|
|
* @return mixed 处理后的返回值
|
|
*/
|
|
protected function cqyProcess(string $op, Request $request): mixed
|
|
{
|
|
$hook = "complexQuery{$op}";
|
|
return method_exists($this, $hook)
|
|
? $this->{$hook}($request)
|
|
: false;
|
|
}
|
|
} |