2025-05-12 00:19:35 +08:00
|
|
|
|
<?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] ?? '';
|
2026-08-13 19:01:21 +08:00
|
|
|
|
if (is_array($checkValue)) {
|
|
|
|
|
|
// 数组值单独处理:非空才收;空数组按「跳过」(与 GF 版一致),
|
|
|
|
|
|
// 且绝不对数组做 strval,避免触发 "Array to string conversion" 警告
|
|
|
|
|
|
if (!empty($checkValue)) {
|
|
|
|
|
|
$result[$fieldName] = $checkValue;
|
|
|
|
|
|
}
|
2025-05-12 00:19:35 +08:00
|
|
|
|
} else {
|
|
|
|
|
|
if (mb_strlen(strval($checkValue)) <= 0 && !in_array($fieldName, $this->notRequest)) {
|
|
|
|
|
|
$requiredFields[] = $fieldName;
|
|
|
|
|
|
} else {
|
2026-08-13 19:01:21 +08:00
|
|
|
|
// 用字符串长度判断是否有值,避免 !empty 把合法的 0 / "0" 误当空丢弃
|
|
|
|
|
|
// (HTTP 参数皆为字符串,status=0 会传成 "0",原写法会导致状态无法写入/改回启用)
|
|
|
|
|
|
if (mb_strlen(strval($checkValue)) > 0) {
|
2025-05-12 00:19:35 +08:00
|
|
|
|
$result[$fieldName] = $checkValue;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if ($throw && $requiredFields) {
|
|
|
|
|
|
UtilsService::getInstance()->notFound('未填写的字段: ' . implode(',', $requiredFields));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (!empty($requiredFields)) {
|
|
|
|
|
|
return $requiredFields;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|