Files
spa-api/app/BaseApp/BaseController.php

171 lines
4.0 KiB
PHP
Raw Normal View History

2025-05-04 22:42:51 +08:00
<?php
namespace App\BaseApp;
2025-05-05 09:44:50 +08:00
use App\Service\common\UtilsService;
2025-05-04 22:42:51 +08:00
use Exception;
use Illuminate\Http\JsonResponse;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
2025-05-08 13:28:56 +08:00
use function PHPUnit\Framework\isString;
2025-05-04 22:42:51 +08:00
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) && !empty($checkValue)) {
$result[$fieldName] = $checkValue;
2025-05-04 22:42:51 +08:00
} else {
2025-05-08 13:28:56 +08:00
if ((empty($checkValue) && $checkValue !== 0) && !in_array($fieldName, $this->notRequest)) {
$requiredFields[] = $fieldName;
} else {
if (!empty($checkValue) || $checkValue === 0) {
$result[$fieldName] = $checkValue;
}
2025-05-05 14:20:17 +08:00
}
2025-05-04 22:42:51 +08:00
}
2025-05-04 22:42:51 +08:00
}
if ($throw && $requiredFields) {
UtilsService::getInstance()->notFound('未填写的字段: ' . implode(',', $requiredFields));
}
if (!empty($requiredFields)) {
return $requiredFields;
}
return $result;
}
}