168 lines
3.9 KiB
PHP
Executable File
168 lines
3.9 KiB
PHP
Executable File
<?php
|
|
|
|
namespace App\BaseApp;
|
|
|
|
use App\Service\common\UtilsService;
|
|
use Exception;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Psr\Container\ContainerExceptionInterface;
|
|
use Psr\Container\NotFoundExceptionInterface;
|
|
|
|
class BaseController
|
|
{
|
|
protected array $insertField = [];
|
|
protected array $notRequest = [];
|
|
protected array $updateField = [];
|
|
protected $service;
|
|
public function __construct()
|
|
{
|
|
}
|
|
|
|
/**
|
|
* 获取列表
|
|
* @Method GET
|
|
* @return JsonResponse
|
|
* @throws Exception
|
|
*/
|
|
public function list(): JsonResponse
|
|
{
|
|
return jok(
|
|
$this->service->list(),
|
|
'列表获取成功'
|
|
);
|
|
}
|
|
|
|
/**
|
|
* 获取下拉列表
|
|
* @Method GET
|
|
* @return mixed
|
|
* @throws Exception
|
|
*/
|
|
public function option(): mixed
|
|
{
|
|
return jok(
|
|
$this->service->option(),
|
|
'列表获取成功'
|
|
);
|
|
}
|
|
|
|
/**
|
|
* 获取详情
|
|
*
|
|
* @Method GET
|
|
* @return JsonResponse
|
|
* @throws ContainerExceptionInterface
|
|
* @throws NotFoundExceptionInterface
|
|
* @throws Exception
|
|
*/
|
|
public function detail(): JsonResponse
|
|
{
|
|
$id = request()->get('id');
|
|
if (empty($id)) UtilsService::getInstance()->notFound('参数错误');
|
|
return jok(
|
|
$this->service->detail($id),
|
|
'详情获取成功'
|
|
);
|
|
}
|
|
|
|
/**
|
|
* 创建
|
|
*
|
|
* @Method POST
|
|
* @return JsonResponse
|
|
* @throws Exception
|
|
*/
|
|
public function create(): JsonResponse
|
|
{
|
|
$params = $this->checkRequiredFields(request()->post());
|
|
return jok(
|
|
$this->service->create($params),
|
|
'创建成功'
|
|
);
|
|
}
|
|
|
|
/**
|
|
* 更新
|
|
*
|
|
* @Method POST
|
|
* @return JsonResponse
|
|
* @throws Exception
|
|
*/
|
|
public function update(): JsonResponse
|
|
{
|
|
$params = $this->checkRequiredFields(request()->post(), 'update');
|
|
$id = $params['id'];
|
|
unset($params['id']);
|
|
return jok(
|
|
$this->service->update($id, $params),
|
|
'更新成功'
|
|
);
|
|
}
|
|
|
|
/**
|
|
* 删除
|
|
*
|
|
* @Method POST
|
|
* @return JsonResponse
|
|
* @throws ContainerExceptionInterface
|
|
* @throws NotFoundExceptionInterface
|
|
* @throws Exception
|
|
*/
|
|
public function delete(): JsonResponse
|
|
{
|
|
$id = request()->post('ids');
|
|
if (empty($id)) UtilsService::getInstance()->notFound('参数错误');
|
|
return jok(
|
|
$this->service->delete($id),
|
|
'删除成功'
|
|
);
|
|
}
|
|
|
|
|
|
/**
|
|
* 检查必填字段
|
|
*
|
|
* @Method NO
|
|
* @param array $data
|
|
* @param string $checkType
|
|
* @param bool $throw
|
|
* @return array
|
|
* @throws Exception
|
|
*/
|
|
public function checkRequiredFields(array $data = [], string $checkType = 'create', bool $throw = true): array
|
|
{
|
|
$checkFields = $checkType == 'create' ? $this->insertField : $this->updateField;
|
|
|
|
if (!empty($this->notRequest)) {
|
|
$checkFields = array_merge($this->notRequest, $checkFields);
|
|
}
|
|
|
|
$requiredFields = [];
|
|
$result = [];
|
|
foreach ($checkFields as $fieldName) {
|
|
$checkValue = $data[$fieldName] ?? '';
|
|
if (is_array($checkValue)) {
|
|
$checkValue = json_encode($checkValue);
|
|
}
|
|
if (mb_strlen(strval($checkValue)) <= 0 && !in_array($fieldName, $this->notRequest)) {
|
|
$requiredFields[] = $fieldName;
|
|
} else {
|
|
if (!empty($checkValue)) {
|
|
$result[$fieldName] = $checkValue;
|
|
}
|
|
}
|
|
}
|
|
|
|
if ($throw && $requiredFields) {
|
|
UtilsService::getInstance()->notFound('未填写的字段: ' . implode(',', $requiredFields));
|
|
}
|
|
|
|
if (!empty($requiredFields)) {
|
|
return $requiredFields;
|
|
}
|
|
|
|
return $result;
|
|
}
|
|
|
|
}
|