You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

80 lines
2.0 KiB
PHP

<?php
namespace App\Services;
use Illuminate\Support\Facades\DB;
use App\Services\JwtService;
class PatientAuthService
{
private JwtService $jwtService;
public function __construct(JwtService $jwtService)
{
$this->jwtService = $jwtService;
}
/**
* 患者登录(身份证号+姓名)
* 从 exam_application 表中查找匹配的患者记录
*/
public function login(string $idCardNo, string $name): array
{
$application = DB::table('exam_application')
->where('patient_id_card_no', $idCardNo)
->where('patient_name', $name)
->first();
if (!$application) {
return ['success' => false, 'message' => '未找到匹配的患者信息,请核实身份证号和姓名'];
}
$patientIds = DB::table('exam_application')
->where('patient_id_card_no', $idCardNo)
->pluck('patient_id')
->unique()
->values()
->toArray();
$payload = [
'type' => 'patient',
'patient_id_card_no' => $idCardNo,
'patient_name' => $name,
'patient_ids' => $patientIds,
];
$token = $this->jwtService->generateToken($payload);
return [
'success' => true,
'message' => '登录成功',
'data' => [
'access_token' => $token,
'token_type' => 'Bearer',
'patient_name' => $name,
'patient_id_card_no' => $idCardNo,
]
];
}
public function getPatientIdCardNo(array $payload): ?string
{
return $payload['patient_id_card_no'] ?? null;
}
public function getPatientName(array $payload): ?string
{
return $payload['patient_name'] ?? null;
}
public function getPatientIds(array $payload): array
{
return $payload['patient_ids'] ?? [];
}
public function isPatient(array $payload): bool
{
return ($payload['type'] ?? '') === 'patient';
}
}