e签宝 对同一证件只签发一个签署账号,同一实名在另一后台账号认证时会绑定冲突。 - 新增 auth.admin/esignUnbind:清除目标账号在当前 e签宝 应用下的签署账号绑定、 在途认证与快捷签授权;有在途操作时拒绝;不注销 e签宝 账号,已签处方不受影响 - 该接口加入 AuthMiddleware 强制鉴权列表,菜单未登记时也不放行; 迁移 2026_11_27_admin_esign_unbind_menu.sql 登记按钮权限,默认仅超级管理员可用 - 管理员列表显示「签署身份」并提供「解除签署」按钮 - 绑定冲突提示指明占用的后台账号(名称 + ID) - 医师认证失败日志把字段写进消息正文(ThinkPHP 不落盘 context) Co-Authored-By: Claude Opus 5 (1M context) <[EMAIL]>
424 lines
16 KiB
PHP
Executable File
424 lines
16 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\PrescriptionCaSigningSettings;
|
|
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 submitForReview()
|
|
{
|
|
if (!$this->request->isPost()) {
|
|
return $this->fail('请使用 POST 上传处方');
|
|
}
|
|
$rawId = $this->request->post('id');
|
|
$id = (is_int($rawId) || is_string($rawId))
|
|
? filter_var($rawId, FILTER_VALIDATE_INT, ['options' => ['min_range' => 1]]) : false;
|
|
if ($id === false) {
|
|
return $this->fail('处方ID无效');
|
|
}
|
|
try {
|
|
return $this->data(PrescriptionLogic::submitForReview($id, (int) $this->adminId, $this->adminInfo));
|
|
} catch (\Throwable $error) {
|
|
return $this->fail($error instanceof \DomainException ? $error->getMessage() : '处方上传失败,请稍后重试');
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @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();
|
|
// The audit is already committed. The notification diagnostic belongs in server logs.
|
|
$data = $wecom === null ? [] : ['wecom_notify_ok' => !empty($wecom['ok'])];
|
|
|
|
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不能为空');
|
|
}
|
|
$pendingOnly = $this->request->post('pending_only', false, null);
|
|
if (!is_bool($pendingOnly)) {
|
|
return $this->fail('作废参数无效');
|
|
}
|
|
$ok = PrescriptionLogic::void($id, $this->adminId, $this->adminInfo, $pendingOnly);
|
|
if (!$ok) {
|
|
return $this->fail(PrescriptionLogic::getError());
|
|
}
|
|
return $this->success('作废成功');
|
|
}
|
|
|
|
public function esignConfig()
|
|
{
|
|
return $this->data(PrescriptionEsignService::capability());
|
|
}
|
|
|
|
/** @notes 修改处方 CA 签名设置 */
|
|
public function esignSetConfig()
|
|
{
|
|
if (!$this->request->isPost()) {
|
|
return $this->fail('请使用 POST 修改 CA 签名设置');
|
|
}
|
|
if ((int) ($this->adminInfo['root'] ?? 0) !== 1) {
|
|
return $this->fail('仅超级管理员可以修改 CA 签名设置');
|
|
}
|
|
$enabled = $this->request->post('enabled', null, null);
|
|
if (!is_bool($enabled)) {
|
|
return $this->fail('CA 签名开关必须为布尔值');
|
|
}
|
|
try {
|
|
PrescriptionCaSigningSettings::setEnabled($enabled, $this->adminId);
|
|
return $this->data(PrescriptionEsignService::capability());
|
|
} catch (\DomainException $error) {
|
|
return $this->fail($error->getMessage());
|
|
} catch (\Throwable) {
|
|
return $this->fail('CA 签名设置暂时无法保存,请稍后重试');
|
|
}
|
|
}
|
|
|
|
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(false) : $this->request->get();
|
|
$id = (int) ($params['id'] ?? 0);
|
|
$orderId = (int) ($params['order_id'] ?? 0);
|
|
$actor = (int) $this->adminId;
|
|
$verifyOnly = array_key_exists('verify_only', $params) ? $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);
|
|
$data = DoctorEsignIdentityService::complete((int) $this->adminId, $input);
|
|
} else {
|
|
$body = $this->request->post(false);
|
|
$signerName = $body['signer_name'] ?? null;
|
|
if (array_key_exists('signer_name', $body) && (!is_string($signerName) || strlen($signerName) > 200)) {
|
|
return $this->fail('证件姓名格式无效');
|
|
}
|
|
$data = DoctorEsignIdentityService::start((int) $this->adminId, $input, $signerName);
|
|
}
|
|
return $this->data($data)->header(['Cache-Control' => 'private, no-store']);
|
|
} catch (\Throwable $error) {
|
|
// 只记录异常类型与位置,不记录手机号、证件号或 e签宝原始响应。
|
|
// ThinkPHP 的 context 只用于占位符替换,不会写入日志,因此字段直接拼进消息。
|
|
\think\facade\Log::warning(sprintf(
|
|
'Doctor esign %s failed admin_id=%d exception=%s provider=%s at=%s',
|
|
$operation,
|
|
(int) $this->adminId,
|
|
get_class($error),
|
|
$error instanceof \app\common\service\esign\EsignProviderException
|
|
? $error->operation . ':' . $error->providerCode . ':' . ($error->reason ?? '') : '-',
|
|
basename($error->getFile()) . ':' . $error->getLine()
|
|
));
|
|
return $this->fail($error instanceof \DomainException || $error instanceof \app\common\service\esign\EsignProviderException
|
|
? mb_substr($error->getMessage(), 0, 240) : '医师认证暂不可用,请稍后重试');
|
|
}
|
|
}
|
|
|
|
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() : '电子签署暂不可用,请联系管理员');
|
|
}
|
|
}
|
|
}
|