57 lines
2.1 KiB
PHP
57 lines
2.1 KiB
PHP
<?php
|
||
declare(strict_types=1);
|
||
|
||
namespace app\common\service\esign;
|
||
|
||
use app\common\service\ConfigService;
|
||
use DomainException;
|
||
use think\facade\Config;
|
||
use think\facade\Db;
|
||
|
||
final class PrescriptionCaSigningSettings
|
||
{
|
||
private const TYPE = 'esign';
|
||
private const NAME = 'ca_signing_enabled';
|
||
private const REQUEST_KEY = 'prescription_ca_signing_settings';
|
||
|
||
public static function override(): ?bool
|
||
{
|
||
$request = PHP_SAPI !== 'cli' ? request() : null;
|
||
$cached = $request?->middleware(self::REQUEST_KEY);
|
||
if (is_array($cached)) {
|
||
return $cached['enabled'];
|
||
}
|
||
|
||
$stored = ConfigService::get(self::TYPE, self::NAME, '');
|
||
$enabled = in_array($stored, [0, '0', 1, '1'], true) ? (bool) $stored : null;
|
||
$request?->withMiddleware([self::REQUEST_KEY => ['enabled' => $enabled]]);
|
||
return $enabled;
|
||
}
|
||
|
||
public static function setEnabled(bool $enabled, int $actor): void
|
||
{
|
||
Db::transaction(static function () use ($enabled, $actor): void {
|
||
// Serialize the first write as well as updates, since the shared config table
|
||
// does not require a unique type/name key.
|
||
$admins = Db::name('admin')->where('root', 1)->where('disable', 0)->whereNull('delete_time')
|
||
->order('id')->lock(true)->column('id');
|
||
if (!in_array($actor, array_map('intval', $admins), true)) {
|
||
throw new DomainException('仅超级管理员可以修改 CA 签名设置');
|
||
}
|
||
if ($enabled) {
|
||
$config = (array) Config::get('esign', []);
|
||
try {
|
||
(new EsignClient(array_replace($config, ['enabled' => true])))->assertConfigured();
|
||
} catch (EsignProviderException $error) {
|
||
throw new DomainException('请先检查 e签宝应用、SDK 地址和 CA 证书配置(' . $error->providerCode . ')');
|
||
}
|
||
}
|
||
ConfigService::set(self::TYPE, self::NAME, (int) $enabled);
|
||
});
|
||
|
||
if (PHP_SAPI !== 'cli') {
|
||
request()->withMiddleware([self::REQUEST_KEY => ['enabled' => $enabled]]);
|
||
}
|
||
}
|
||
}
|