管理员列表支持解除电子签署身份,处理同一实名绑定冲突

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]>
This commit is contained in:
Your Name
2026-11-27 09:42:37 +08:00
co-authored by Claude Opus 5 (1M context)
parent e4d788ed32
commit 2019f9dca1
9 changed files with 176 additions and 14 deletions
+5
View File
@@ -27,3 +27,8 @@ export function adminDelete(params: any) {
export function adminDetail(params: any) {
return request.get({ url: '/auth.admin/detail', params })
}
// 解除管理员的电子签署身份
export function adminEsignUnbind(params: { id: number }) {
return request.post({ url: '/auth.admin/esignUnbind', params })
}
+24 -2
View File
@@ -73,6 +73,11 @@
<el-table-column label="创建时间" prop="create_time" min-width="180" />
<el-table-column label="最近登录时间" prop="login_time" min-width="180" />
<el-table-column label="最近登录IP" prop="login_ip" min-width="120" />
<el-table-column label="签署身份" min-width="120">
<template #default="{ row }">
{{ row.esign_signer_name || '-' }}
</template>
</el-table-column>
<el-table-column label="状态" min-width="100" v-perms="['auth.admin/edit']">
<template #default="{ row }">
<el-switch
@@ -84,7 +89,7 @@
/>
</template>
</el-table-column>
<el-table-column label="操作" width="120" fixed="right">
<el-table-column label="操作" width="200" fixed="right">
<template #default="{ row }">
<el-button
v-perms="['auth.admin/edit']"
@@ -94,6 +99,15 @@
>
编辑
</el-button>
<el-button
v-if="row.esign_signer_name"
v-perms="['auth.admin/esignUnbind']"
type="warning"
link
@click="handleEsignUnbind(row)"
>
解除签署
</el-button>
<el-button
v-if="row.root != 1"
v-perms="['auth.admin/delete']"
@@ -116,7 +130,7 @@
</template>
<script lang="ts" setup name="admin">
import { adminDelete, adminEdit, adminLists } from '@/api/perms/admin'
import { adminDelete, adminEdit, adminEsignUnbind, adminLists } from '@/api/perms/admin'
import { roleAll } from '@/api/perms/role'
import { useDictOptions } from '@/hooks/useDictOptions'
import { usePaging } from '@/hooks/usePaging'
@@ -171,6 +185,14 @@ const handleDelete = async (id: number) => {
getLists()
}
const handleEsignUnbind = async (row: any) => {
await feedback.confirm(
`确定解除「${row.name}」绑定的签署身份(${row.esign_signer_name})?解除后该账号需重新人脸认证才能电子签署,已签处方不受影响。`
)
await adminEsignUnbind({ id: row.id })
getLists()
}
const { optionsData } = useDictOptions<{
role: any[]
}>({
@@ -22,6 +22,7 @@ use app\adminapi\logic\LoginLogic;
use app\adminapi\validate\auth\editSelfValidate;
use app\common\cache\AdminTokenCache;
use app\common\model\auth\Admin;
use app\common\service\esign\DoctorEsignIdentityService;
/**
* 管理员控制器
@@ -211,4 +212,20 @@ class AdminController extends BaseAdminController
return $this->success('解绑成功');
}
/**
* @notes 解除管理员的电子签署身份(同一实名改绑到其他后台账号时使用)
*/
public function esignUnbind()
{
if (!$this->request->isPost()) {
return $this->fail('请使用 POST 请求');
}
try {
DoctorEsignIdentityService::unbind((int) $this->request->post('id', 0));
} catch (\DomainException $error) {
return $this->fail($error->getMessage());
}
return $this->success('已解除签署身份', [], 1, 1);
}
}
@@ -356,13 +356,16 @@ class PrescriptionController extends BaseAdminController
return $this->data($data)->header(['Cache-Control' => 'private, no-store']);
} catch (\Throwable $error) {
// 只记录异常类型与位置,不记录手机号、证件号或 e签宝原始响应。
\think\facade\Log::warning('Doctor esign ' . $operation . ' failed', [
'admin_id' => (int) $this->adminId,
'exception' => get_class($error),
'provider_code' => $error instanceof \app\common\service\esign\EsignProviderException
? $error->operation . ':' . $error->providerCode . ':' . ($error->reason ?? '') : '',
'at' => basename($error->getFile()) . ':' . $error->getLine(),
]);
// 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) : '医师认证暂不可用,请稍后重试');
}
@@ -355,6 +355,8 @@ class AuthMiddleware
'tcm.prescription/void',
'tcm.prescription/audit',
'tcm.prescription/submitforreview',
// 解除他人签署身份:菜单迁移未执行时也不能因“未登记路由”而放行。
'auth.admin/esignunbind',
], true);
}
@@ -27,6 +27,7 @@ use app\common\model\dept\Dept;
use app\common\model\dept\Jobs;
use app\common\model\doctor\Appointment;
use app\common\model\tcm\Diagnosis;
use app\common\service\esign\DoctorEsignIdentityService;
use think\facade\Db;
/**
@@ -247,6 +248,12 @@ class AdminLists extends BaseAdminDataLists implements ListsExtendInterface, Lis
$adminLists[$k]['jobs_name'] = trim($jobsName, '/');
}
// 当前 e签宝 应用下已绑定的签署实名,供管理员解除改绑
$signerNames = DoctorEsignIdentityService::signerNames(array_column($adminLists, 'id'));
foreach ($adminLists as $k => $v) {
$adminLists[$k]['esign_signer_name'] = $signerNames[$v['id']] ?? '';
}
return $adminLists;
}
@@ -528,13 +528,56 @@ final class DoctorEsignIdentityService
return null;
}
/** Returns doctor_id => signer name for accounts bound under the current eSign application. */
public static function signerNames(array $doctorIds): array
{
$doctorIds = array_values(array_filter(array_map('intval', $doctorIds)));
if ($doctorIds === []) {
return [];
}
return Db::name('doctor_esign_account')->where(self::configuredScope())
->whereIn('doctor_id', $doctorIds)->column('doctor_name', 'doctor_id');
}
/**
* 管理员解除某后台账号的签署身份(当前 e签宝 应用),同一实名才能改绑到另一个后台账号。
* 已签处方保留原签署证据,不受影响。
*/
public static function unbind(int $doctorId): void
{
if ($doctorId <= 0) {
throw new DomainException('管理员ID无效');
}
$scope = ['doctor_id' => $doctorId] + self::configuredScope();
Db::transaction(static function () use ($scope): void {
$account = Db::name('doctor_esign_account')->where($scope)->lock(true)->find();
foreach ([self::IDENTITY_FLOW_TABLE, self::QUICK_FLOW_TABLE] as $table) {
if (self::hasLease(Db::name($table)->where($scope)->lock(true)->find())) {
throw new DomainException('该账号的签署身份正在处理中,请稍后再试');
}
}
if (!$account) {
throw new DomainException('该账号未绑定签署身份');
}
Db::name('doctor_esign_account')->where($scope)->delete();
// 认证与快捷签授权都挂在旧签署账号上,一并清除,避免解绑后继续沿用。
Db::name(self::IDENTITY_FLOW_TABLE)->where($scope)->delete();
Db::name(self::QUICK_FLOW_TABLE)->where($scope)->delete();
});
}
private static function configuredScope(): array
{
return ['environment' => (string) Config::get('esign.environment', 'sandbox'),
'app_id' => (string) Config::get('esign.app_id', '')];
}
private static function scope(int $actor): array
{
if (!self::enabled()) {
throw new DomainException('医师电子签署认证尚未启用');
}
return ['doctor_id' => $actor, 'environment' => (string) Config::get('esign.environment', 'sandbox'),
'app_id' => (string) Config::get('esign.app_id', '')];
return ['doctor_id' => $actor] + self::configuredScope();
}
private static function snapshot(array $scope, bool $lock = false, string $flowTable = self::IDENTITY_FLOW_TABLE): array
@@ -625,7 +668,14 @@ final class DoctorEsignIdentityService
$owner = Db::name('doctor_esign_account')->where('environment', $scope['environment'])
->where('app_id', $scope['app_id'])->where('account_id', $newAccountId)->lock(true)->value('doctor_id');
if ($owner !== null) {
throw new DomainException('签署账号绑定冲突,请联系管理员核实');
// e签宝 对同一实名只签发一个签署账号;指明占用方,便于管理员在管理员列表中解除后重试。
// 用名称+ID 而非登录账号:账号常为手机号,桌面端会屏蔽含长数字的提示。
$holder = (string) Db::name('admin')->where('id', $owner)->value('name');
$label = ($holder !== '' ? $holder : '已删除账号') . '(ID ' . (int) $owner . ')';
throw new DomainException(sprintf(
'该实名的签署账号已绑定后台账号「%s」,请管理员在「管理员」列表解除其签署身份后再确认绑定(刷脸结果 30 分钟内有效)',
$label
));
}
Db::name('doctor_esign_account')->insert($scope + [
'account_id' => $newAccountId, 'doctor_name' => $snapshot['name'], 'create_time' => time(),
@@ -0,0 +1,14 @@
-- 管理员列表新增「解除签署」按钮权限:同一实名的 e签宝 签署账号只能绑定一个后台账号,改绑前需管理员解除旧绑定。
-- 该操作会使对方无法继续电子签署,默认不授予任何角色,仅超级管理员可用;需要时在角色权限中单独勾选。
START TRANSACTION;
INSERT INTO zyt_system_menu
(pid, type, name, icon, sort, perms, paths, component, selected, params, is_cache, is_show, is_disable, create_time, update_time)
SELECT parent.id, 'A', '解除签署', '', 0, 'auth.admin/esignUnbind', '', '', '', '', 0, 1, 0, UNIX_TIMESTAMP(), UNIX_TIMESTAMP()
FROM zyt_system_menu parent
WHERE parent.type = 'C' AND parent.perms = 'auth.admin/lists' AND parent.component = 'permission/admin/index'
AND NOT EXISTS (SELECT 1 FROM zyt_system_menu WHERE perms = 'auth.admin/esignUnbind')
ORDER BY parent.id
LIMIT 1;
COMMIT;
+44 -2
View File
@@ -137,7 +137,7 @@ namespace {
Db::execute($sql);
}
Db::execute('CREATE TABLE zyt_config (id INTEGER PRIMARY KEY,type TEXT,name TEXT,value TEXT,create_time INTEGER,update_time INTEGER)');
Db::execute('CREATE TABLE zyt_admin (id INTEGER PRIMARY KEY,name TEXT,disable INTEGER DEFAULT 0,delete_time INTEGER,root INTEGER DEFAULT 0)');
Db::execute('CREATE TABLE zyt_admin (id INTEGER PRIMARY KEY,name TEXT,account TEXT,disable INTEGER DEFAULT 0,delete_time INTEGER,root INTEGER DEFAULT 0)');
Db::execute('CREATE TABLE zyt_admin_role (admin_id INTEGER,role_id INTEGER)');
Db::execute('CREATE TABLE zyt_doctor_esign_account (doctor_id INTEGER,environment TEXT,app_id TEXT,account_id TEXT,
doctor_name TEXT,create_time INTEGER,UNIQUE(doctor_id,environment,app_id))');
@@ -345,7 +345,7 @@ namespace {
$assert(Enrollment::complete(1)['quicksign_expires_at'] === $now + 120, 'provider validity is preserved when shorter than the local bound');
$config->set(['review_enabled' => true], 'esign');
Db::name('admin')->insert(['id' => 4, 'name' => 'admin', 'root' => 1]);
Db::name('admin')->insert(['id' => 4, 'name' => 'admin', 'account' => 'root-account', 'root' => 1]);
Provider::$identity = ['status' => 'INIT'];
Provider::$newAccountId = 'reviewer-account';
$startsBeforeReview = Provider::$starts;
@@ -371,5 +371,47 @@ namespace {
'verified legal name is saved only to the reviewer signing account');
$throws(static fn () => Enrollment::start(4, $phone, '不同姓名'), DomainException::class,
'a verified signing account cannot be renamed through enrollment');
// 同一实名在另一个后台账号认证:e签宝 返回同一签署账号,需管理员解除旧绑定后才能改绑。
Db::name('admin')->insert(['id' => 5, 'name' => 'admin2', 'account' => 'second-root', 'root' => 1]);
Provider::$identity = ['status' => 'INIT'];
Enrollment::start(5, $phone, Provider::NAME);
Provider::$identity = ['status' => 'SUCCESS', 'indivInfo' => ['name' => Provider::NAME,
'certType' => 'INDIVIDUAL_CH_IDCARD', 'certNo' => Provider::DOCUMENT]];
$conflict = '';
try {
Enrollment::complete(5);
} catch (DomainException $error) {
$conflict = $error->getMessage();
}
$assert(str_contains($conflict, '「admin(ID 4)」') && !str_contains($conflict, 'root-account')
&& !str_contains($conflict, Provider::DOCUMENT)
&& Db::name('doctor_esign_account')->where('doctor_id', 5)->count() === 0
&& Db::name('doctor_esign_identity')->where('doctor_id', 5)->value('operation_token') === '',
'a conflict names the holding back-office account and keeps the verified flow for retry');
$assert(Enrollment::signerNames([4, 5, 0]) === [4 => Provider::NAME], 'admin list sees only current-app bindings');
$lease = ['doctor_id' => 4, 'environment' => 'sandbox', 'app_id' => 'fixture',
'operation_token' => str_repeat('a', 32), 'operation_expires_at' => time() + 60];
Db::name('doctor_esign_identity')->insert($lease);
$throws(static fn () => Enrollment::unbind(4), DomainException::class, 'an in-flight provider operation blocks unbinding');
$assert(Db::name('doctor_esign_account')->where('doctor_id', 4)->count() === 1, 'a blocked unbind changes nothing');
Db::name('doctor_esign_identity')->where('doctor_id', 4)->update(['operation_expires_at' => time() - 1]);
Db::name('doctor_esign_quicksign')->insert(['doctor_id' => 4, 'environment' => 'sandbox', 'app_id' => 'fixture',
'account_id' => 'reviewer-account', 'flow_id' => 'old-grant', 'expires_at' => time() + 3600]);
$config->set(['app_id' => 'other-app'], 'esign');
$throws(static fn () => Enrollment::unbind(4), DomainException::class, 'unbinding is scoped to the current eSign application');
$config->set(['app_id' => 'fixture'], 'esign');
Enrollment::unbind(4);
$assert(Db::name('doctor_esign_account')->where('doctor_id', 4)->count() === 0
&& Db::name('doctor_esign_identity')->where('doctor_id', 4)->count() === 0
&& Db::name('doctor_esign_quicksign')->where('doctor_id', 4)->count() === 0
&& Db::name('doctor_esign_identity')->where('doctor_id', 5)->count() === 1,
'unbinding clears only the target binding, its pending flow and quick-sign grant');
$throws(static fn () => Enrollment::unbind(4), DomainException::class, 'unbinding an unbound account is reported');
$rebound = Enrollment::complete(5);
$assert($rebound['status'] === 'ready'
&& Db::name('doctor_esign_account')->where('doctor_id', 5)->value('account_id') === 'reviewer-account',
'after unbinding, the verified flow binds the same provider account to the new back-office account');
echo "Doctor eSign enrollment: $checks checks passed (SQLite and provider doubles)\n";
}