Files
zyt/server/app/adminapi/controller/tcm/PrescriptionController.php
2026-11-20 10:30:04 +08:00

361 lines
13 KiB
PHP
Executable File

<?php
declare(strict_types=1);
namespace app\adminapi\controller\tcm;
use app\adminapi\controller\BaseAdminController;
use app\adminapi\logic\tcm\PrescriptionLogic;
use app\adminapi\validate\tcm\PrescriptionValidate;
use app\common\service\esign\DoctorEsignIdentityService;
use app\common\service\esign\PrescriptionEsignService;
use app\common\service\esign\PrescriptionReviewEsignService;
/**
* 中医处方单控制器
*/
class PrescriptionController extends BaseAdminController
{
/**
* @notes 处方列表
*/
public function lists()
{
return $this->dataLists(new \app\adminapi\lists\tcm\PrescriptionLists());
}
/**
* @notes 添加处方
*/
public function add()
{
$params = (new PrescriptionValidate())->post()->goCheck('add');
$params['creator_id'] = $this->adminId;
$id = PrescriptionLogic::add($params, $this->adminId, $this->adminInfo);
if ($id === null) {
return $this->fail(PrescriptionLogic::getError());
}
return $this->success('保存成功', ['id' => $id, 'esign' => PrescriptionEsignService::status($id, (int) $this->adminId, $this->adminInfo)]);
}
/**
* @notes 编辑处方
*/
public function edit()
{
$params = (new PrescriptionValidate())->post()->goCheck('edit');
$result = PrescriptionLogic::edit($params, $this->adminId);
if (!$result) {
return $this->fail(PrescriptionLogic::getError());
}
return $this->success('编辑成功', ['id' => (int) $params['id'], 'esign' => PrescriptionEsignService::status((int) $params['id'], (int) $this->adminId, $this->adminInfo)]);
}
/**
* @notes 修正处方患者姓名手机性别
*/
public function patchPatient()
{
$params = (new PrescriptionValidate())->post()->goCheck('patchPatient');
$ok = PrescriptionLogic::patchPatientContact(
(int) $params['id'],
(string) $params['patient_name'],
(string) $params['phone'],
(int) $params['gender'],
(int) $this->adminId,
$this->adminInfo
);
if (!$ok) {
return $this->fail(PrescriptionLogic::getError());
}
return $this->success('已更新');
}
/**
* @notes 删除处方
*/
public function delete()
{
$params = (new PrescriptionValidate())->post()->goCheck('delete');
$result = PrescriptionLogic::delete($params['id']);
if (!$result) {
return $this->fail(PrescriptionLogic::getError());
}
return $this->success('删除成功');
}
/**
* @notes 处方详情
*/
public function detail()
{
$params = (new PrescriptionValidate())->get()->goCheck('detail');
$detail = PrescriptionLogic::detail((int) $params['id'], (int) $this->adminId, $this->adminInfo);
if (!$detail) {
$msg = PrescriptionLogic::getError();
return $this->fail($msg !== '' ? $msg : '处方不存在');
}
return $this->data($detail);
}
/**
* @notes 审核处方(通过 / 驳回,驳回即作废)
*/
public function audit()
{
$params = (new PrescriptionValidate())->post()->goCheck('audit');
$ok = PrescriptionLogic::audit(
(int) $params['id'],
(string) $params['action'],
(string) ($params['remark'] ?? ''),
(int) $this->adminId,
$this->adminInfo
);
if (!$ok) {
return $this->fail(PrescriptionLogic::getError());
}
$msg = $params['action'] === 'reject' ? '已驳回并作废处方' : '审核通过';
$wecom = PrescriptionLogic::consumeLastAuditWecomNotify();
$data = [];
if (is_array($wecom)) {
$data['wecom_notify_ok'] = !empty($wecom['ok']);
if (empty($wecom['ok']) && !empty($wecom['message'])) {
$data['wecom_notify_hint'] = (string) $wecom['message'];
}
}
return $this->success($msg, $data);
}
/**
* @notes 根据诊单获取处方列表
*/
public function listByDiagnosis()
{
$diagnosisId = (int)($this->request->get('diagnosis_id') ?? 0);
if (!$diagnosisId) {
return $this->fail('诊单ID不能为空');
}
$list = PrescriptionLogic::listByDiagnosis($diagnosisId, (int) $this->adminId, $this->adminInfo);
if (PrescriptionLogic::getError() !== '') {
return $this->fail(PrescriptionLogic::getError());
}
return $this->data($list);
}
/**
* @notes 根据预约获取处方
*/
public function getByAppointment()
{
$appointmentId = (int)($this->request->get('appointment_id') ?? 0);
if (!$appointmentId) {
return $this->fail('预约ID不能为空');
}
$prescription = PrescriptionLogic::getByAppointment($appointmentId, (int)$this->adminId, $this->adminInfo);
if ($prescription === null) {
$msg = PrescriptionLogic::getError();
if ($msg !== '') {
return $this->fail($msg);
}
return $this->data([]);
}
return $this->data($prescription);
}
/**
* @notes 作废处方
*/
public function void()
{
if (!$this->request->isPost()) {
return $this->fail('请使用 POST 作废处方');
}
$id = (int)($this->request->post('id') ?? 0);
if ($id <= 0) {
return $this->fail('处方ID不能为空');
}
$ok = PrescriptionLogic::void($id, $this->adminId, $this->adminInfo);
if (!$ok) {
return $this->fail(PrescriptionLogic::getError());
}
return $this->success('作废成功');
}
public function esignConfig()
{
return $this->data(PrescriptionEsignService::capability());
}
public function esignStatus()
{
return $this->esignOperation('status', false);
}
public function esignIdentityStatus()
{
return $this->esignOperation('identityStatus', false);
}
public function esignStart()
{
return $this->esignOperation('start', true);
}
public function esignComplete()
{
return $this->esignOperation('complete', true);
}
/** @notes 验证处方签署短信 */
public function esignVerifyWillingness()
{
return $this->esignOperation('verifyWillingness', true);
}
public function esignDownload()
{
return $this->esignOperation('download', false);
}
public function esignReviewStatus()
{
return $this->esignReviewOperation(false);
}
public function esignReviewConfirm()
{
return $this->esignReviewOperation(true);
}
private function esignReviewOperation(bool $confirm)
{
if ($confirm ? !$this->request->isPost() : !$this->request->isGet()) {
return $this->fail($confirm ? '请使用 POST 请求' : '请使用 GET 请求');
}
$params = $confirm ? $this->request->post() : $this->request->get();
$id = (int) ($params['id'] ?? 0);
$orderId = (int) ($params['order_id'] ?? 0);
$actor = (int) $this->adminId;
$verifyOnly = $params['verify_only'] ?? false;
if ($id <= 0 || $orderId < 0 || !is_bool($verifyOnly)) {
return $this->fail('审核签署参数无效');
}
if ($confirm && (int) ($params['actor_id'] ?? 0) !== $actor) {
return $this->fail('登录账号已切换,请刷新并由当前审核人确认');
}
try {
if ($confirm) {
ignore_user_abort(true);
set_time_limit(360);
}
$data = $confirm
? PrescriptionReviewEsignService::confirm($id, $orderId, $actor, $this->adminInfo, $verifyOnly)
: PrescriptionReviewEsignService::status($id, $orderId, $actor, $this->adminInfo);
return $this->data($data)->header(['Cache-Control' => 'private, no-store']);
} catch (\app\common\service\esign\DoctorEsignAuthorizationRequired) {
return $this->fail('审核人快捷签授权需要开通或续期,请在「审核签署身份」中处理');
} catch (\Throwable $error) {
return $this->fail($error instanceof \DomainException || $error instanceof \app\common\service\esign\EsignProviderException
? $error->getMessage() : '审核签署暂不可用,请刷新状态后重试');
}
}
/** @notes 查询医师电子签署认证 */
public function esignDoctorStatus()
{
return $this->esignDoctorOperation('status', false);
}
/** @notes 发起医师电子签署认证 */
public function esignDoctorStart()
{
return $this->esignDoctorOperation('start', true, 'mobile');
}
/** @notes 完成医师电子签署认证 */
public function esignDoctorComplete()
{
return $this->esignDoctorOperation('complete', true, 'identity_number');
}
private function esignDoctorOperation(string $operation, bool $post, string $field = '')
{
if ($post ? !$this->request->isPost() : !$this->request->isGet()) {
return $this->fail($post ? '请使用 POST 请求' : '请使用 GET 请求');
}
try {
if (!$post) {
return $this->data(DoctorEsignIdentityService::status((int) $this->adminId))
->header(['Cache-Control' => 'private, no-store']);
}
$input = $this->request->post($field, '');
if (!is_string($input)) {
return $this->fail($field === 'mobile' ? '手机号格式无效' : '身份证号码格式无效');
}
if ($operation === 'complete') {
ignore_user_abort(true);
}
return $this->data(DoctorEsignIdentityService::{$operation}((int) $this->adminId, $input))
->header(['Cache-Control' => 'private, no-store']);
} catch (\Throwable $error) {
return $this->fail($error instanceof \DomainException ? $error->getMessage() : '医师认证暂不可用,请稍后重试');
}
}
private function esignOperation(string $operation, bool $post)
{
if ($post && !$this->request->isPost()) {
return $this->fail('请使用 POST 请求');
}
$id = (int) ($post ? $this->request->post('id', 0) : $this->request->get('id', 0));
if ($id <= 0) {
return $this->fail('处方ID无效');
}
if ($operation === 'complete') {
// A browser disconnect must not interrupt archival after signing.
ignore_user_abort(true);
set_time_limit(300);
}
try {
$body = $post ? $this->request->post(false) : [];
if ($operation === 'verifyWillingness') {
$code = $this->request->post('auth_code', '');
if (!is_string($code)) {
return $this->fail('短信验证码格式无效');
}
return $this->data(PrescriptionEsignService::verifyWillingness($id, (int) $this->adminId,
$this->adminInfo, $code));
}
if ($operation === 'start') {
$restartIdentity = array_key_exists('restart_identity', $body) ? $body['restart_identity'] : false;
if (!is_bool($restartIdentity)) {
return $this->fail('重新核身标记格式无效');
}
return $this->data(PrescriptionEsignService::start($id, (int) $this->adminId,
$this->adminInfo, $restartIdentity));
}
if ($operation === 'complete') {
$verifyOnly = array_key_exists('verify_only', $body) ? $body['verify_only'] : false;
if (!is_bool($verifyOnly)) {
return $this->fail('验签标记格式无效');
}
$identityNumber = $this->request->post('identity_number', '');
if (!is_string($identityNumber)) {
return $this->fail('身份证号码格式无效');
}
return $this->data(PrescriptionEsignService::complete($id, (int) $this->adminId,
$this->adminInfo, $identityNumber, $verifyOnly));
}
return $this->data(PrescriptionEsignService::{$operation}($id, (int) $this->adminId, $this->adminInfo));
} catch (\Throwable $e) {
return $this->fail($e instanceof \DomainException ? $e->getMessage() : '电子签署暂不可用,请联系管理员');
}
}
}