hospital-applets-api/app/Controller/PatientFamilyController.php
2023-02-17 17:10:16 +08:00

82 lines
2.3 KiB
PHP

<?php
namespace App\Controller;
use App\Constants\HttpEnumCode;
use App\Request\PatientFamilyRequest;
use App\Services\PatientFamilyService;
use Hyperf\Validation\Contract\ValidatorFactoryInterface;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use Psr\Http\Message\ResponseInterface;
use Hyperf\Di\Annotation\Inject;
/**
* 家庭成员
*/
class PatientFamilyController extends AbstractController
{
#[Inject]
protected ValidatorFactoryInterface $validationFactory;
/**
* 获取家庭成员列表
* @return ResponseInterface
*/
public function getFamilyList(): ResponseInterface
{
$patientFamilyService = new PatientFamilyService();
$data = $patientFamilyService->getFamilyList();
return $this->response->json($data);
}
/**
* 新增家庭成员
* @return array|ResponseInterface
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public function addFamily(): array|ResponseInterface
{
$request = $this->container->get(PatientFamilyRequest::class);
$request->scene('addFamily')->validateResolved();
// 检测入参身份证号
$res = $this->checkRequestIdCard();
if (!empty($res)) {
return fail(HttpEnumCode::HTTP_ERROR, $res);
}
$patientFamilyService = new PatientFamilyService();
$data = $patientFamilyService->addFamily();
return $this->response->json($data);
}
/**
* 检测入参身份证号
* @return string
*/
private function checkRequestIdCard(): string
{
$messages = [
'id_number.regex' => '证件号错误',
];
$validator = $this->validationFactory->make($this->request->all(), [
'type' => 'required',
], $messages);
$validator->sometimes(
['id_number'],
['regex:/^(?:1[1-5]|2[1-3]|3[1-7]|4[1-6]|5[0-4]|6[1-5])\d{4}(?:1[89]|20)\d{2}(?:0[1-9]|1[0-2])(?:0[1-9]|[12]\d|3[01])\d{3}[\dxX]$/'],
function ($input) {
return $input->type == 1;
}
);
if ($validator->fails()) {
return $validator->errors()->first();
}
return "";
}
}