210 lines
12 KiB
PHP
210 lines
12 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
namespace app\common\service\esign {
|
|
final class PrescriptionReviewEsignService
|
|
{
|
|
public static array $calls = [];
|
|
public static function confirm(int $id, int $orderId, int $actor, array $info, bool $verifyOnly): array
|
|
{
|
|
self::$calls[] = [$id, $orderId, $actor, $verifyOnly];
|
|
return ['state' => 'signed'];
|
|
}
|
|
}
|
|
|
|
final class PrescriptionEsignService
|
|
{
|
|
public static array $calls = [];
|
|
public static array $starts = [];
|
|
public static array $verifications = [];
|
|
public static function start(int $id, int $actor, array $info, bool $restartIdentity = false): array
|
|
{
|
|
self::$starts[] = [$id, $actor, $info, $restartIdentity];
|
|
return ['status' => 'authenticating'];
|
|
}
|
|
public static function complete(int $id, int $actor, array $info, string $identityNumber = '', bool $verifyOnly = false): array
|
|
{
|
|
self::$calls[] = [$id, $actor, $info, $identityNumber, $verifyOnly];
|
|
return ['status' => 'signed'];
|
|
}
|
|
public static function verifyWillingness(int $id, int $actor, array $info, string $code): array
|
|
{
|
|
self::$verifications[] = [$id, $actor, $info, $code];
|
|
return ['status' => 'authenticating'];
|
|
}
|
|
}
|
|
}
|
|
namespace app\common\model {
|
|
class OperationLog
|
|
{
|
|
public array $fields = [];
|
|
public static array $saved = [];
|
|
public function __set(string $key, $value): void { $this->fields[$key] = $value; }
|
|
public function save(): bool { self::$saved[] = $this->fields; return true; }
|
|
}
|
|
}
|
|
namespace app\adminapi\logic\tcm {
|
|
final class PrescriptionLogic
|
|
{
|
|
public static bool $auditOk = true;
|
|
public static ?array $notice = null;
|
|
|
|
public static function audit(int $id, string $action, string $remark, int $actor, array $info): bool
|
|
{
|
|
return self::$auditOk;
|
|
}
|
|
|
|
public static function getError(): string
|
|
{
|
|
return '无审核权限';
|
|
}
|
|
|
|
public static function consumeLastAuditWecomNotify(): ?array
|
|
{
|
|
$notice = self::$notice;
|
|
self::$notice = null;
|
|
return $notice;
|
|
}
|
|
}
|
|
}
|
|
namespace app\adminapi\controller {
|
|
class BaseAdminController
|
|
{
|
|
public $request;
|
|
public int $adminId = 7;
|
|
public array $adminInfo = ['admin_id' => 7, 'name' => '测试医师'];
|
|
public function isNotNeedLogin(): bool { return false; }
|
|
protected function data(array $data): \ArrayObject
|
|
{
|
|
return new class(['code' => 1, 'data' => $data]) extends \ArrayObject {
|
|
public function header(array $headers): self { return $this; }
|
|
};
|
|
}
|
|
protected function fail(string $message): array { return ['code' => 0, 'msg' => $message]; }
|
|
protected function success(string $message, array $data = []): array
|
|
{
|
|
return ['code' => 1, 'msg' => $message, 'data' => $data];
|
|
}
|
|
}
|
|
}
|
|
namespace {
|
|
require dirname(__DIR__) . '/vendor/autoload.php';
|
|
require dirname(__DIR__) . '/vendor/topthink/framework/src/helper.php';
|
|
$app = new think\App(); // No initialize, environment, real DB, credentials or network.
|
|
$checks = 0;
|
|
$expect = static function (bool $condition, string $label) use (&$checks): void {
|
|
if (!$condition) { throw new RuntimeException($label); }
|
|
++$checks;
|
|
};
|
|
$number = '110101199001011237';
|
|
$controller = new app\adminapi\controller\tcm\PrescriptionController();
|
|
$request = (new app\Request())->withServer(['REQUEST_METHOD' => 'POST'])->setMethod('POST')->setController('tcm.Prescription')->setAction('esignComplete')
|
|
->setUrl('/adminapi/tcm.prescription/esignComplete?identity_number=' . $number)
|
|
->withPost(['id' => 41, 'identity_number' => $number, 'extra' => ['identity_number' => $number]]);
|
|
$request->controllerObject = $controller;
|
|
$request->adminInfo = $controller->adminInfo;
|
|
$controller->request = $request;
|
|
$result = $controller->esignComplete();
|
|
$calls = app\common\service\esign\PrescriptionEsignService::$calls;
|
|
$expect($result['code'] === 1 && $calls[0] === [41, 7, $controller->adminInfo, $number, false], 'POST input forwarded only to current authenticated doctor service');
|
|
$expect(!str_contains(json_encode($result), $number), 'controller response has no supplemental input');
|
|
|
|
think\Container::getInstance()->instance('request', $request);
|
|
$response = new class($result) {
|
|
public function __construct(private \ArrayObject $result) {}
|
|
public function getContent(): string { return json_encode($this->result); }
|
|
};
|
|
(new app\adminapi\listener\OperationLog())->handle($response);
|
|
$audit = app\common\model\OperationLog::$saved[0];
|
|
$expect(json_decode($audit['params'], true) === ['id' => 41], 'operation audit allowlists prescription id only, including nested malicious inputs');
|
|
$expect(!str_contains(json_encode($audit), $number) && !str_contains($audit['url'], '?'), 'operation audit removes submitted identity and URL query');
|
|
|
|
$controller->request = (new app\Request())->setMethod('POST')->withPost(['id' => 41, 'identity_number' => [$number]]);
|
|
$expect($controller->esignComplete()['code'] === 0 && count(app\common\service\esign\PrescriptionEsignService::$calls) === 1, 'array identity input rejected without invoking completion');
|
|
$controller->request = (new app\Request())->setMethod('GET')->withGet(['id' => 41, 'identity_number' => $number]);
|
|
$expect($controller->esignComplete()['code'] === 0 && count(app\common\service\esign\PrescriptionEsignService::$calls) === 1, 'GET never performs completion');
|
|
$controller->request = (new app\Request())->setMethod('POST')->withPost(['id' => 42]);
|
|
$expect($controller->esignComplete()['code'] === 1 && app\common\service\esign\PrescriptionEsignService::$calls[1][3] === '', 'existing complete callers keep optional input compatibility');
|
|
|
|
$controller->request = (new app\Request())->setMethod('POST')->withPost(['id' => 42, 'verify_only' => true]);
|
|
$expect($controller->esignComplete()['code'] === 1 && app\common\service\esign\PrescriptionEsignService::$calls[2][4] === true, 'explicit archive verification flag forwarded');
|
|
$controller->request = (new app\Request())->setMethod('POST')->withPost(['id' => 42, 'restart_identity' => true]);
|
|
$expect($controller->esignStart()['code'] === 1 && app\common\service\esign\PrescriptionEsignService::$starts[0][3] === true, 'explicit replacement flag forwarded');
|
|
foreach ([null, 'true', 'false', 1, [], ['true']] as $invalid) {
|
|
$controller->request = (new app\Request())->setMethod('POST')->withPost(['id' => 42, 'verify_only' => $invalid, 'restart_identity' => $invalid]);
|
|
$expect($controller->esignComplete()['code'] === 0 && count(app\common\service\esign\PrescriptionEsignService::$calls) === 3, 'invalid archive verification flag cannot fall through to signing');
|
|
$expect($controller->esignStart()['code'] === 0 && count(app\common\service\esign\PrescriptionEsignService::$starts) === 1, 'invalid replacement flag cannot change identity flow');
|
|
}
|
|
|
|
foreach ([false, true] as $verifyOnly) {
|
|
$controller->request = (new app\Request())->setMethod('POST')->withPost([
|
|
'id' => 42, 'order_id' => 5, 'actor_id' => 7, 'verify_only' => $verifyOnly,
|
|
]);
|
|
$expect($controller->esignReviewConfirm()['code'] === 1, 'review accepts a JSON boolean without the global trim filter');
|
|
}
|
|
$expect(app\common\service\esign\PrescriptionReviewEsignService::$calls === [[42, 5, 7, false], [42, 5, 7, true]],
|
|
'review confirmation preserves the explicit sign versus verify choice');
|
|
foreach ([null, 'false', 'true', 0, 1, []] as $invalid) {
|
|
$controller->request = (new app\Request())->setMethod('POST')->withPost([
|
|
'id' => 42, 'actor_id' => 7, 'verify_only' => $invalid,
|
|
]);
|
|
$expect($controller->esignReviewConfirm()['code'] === 0, 'malformed review flag never dispatches');
|
|
}
|
|
$controller->request = (new app\Request())->setMethod('POST')->withPost(['id' => 42, 'actor_id' => 8, 'verify_only' => false]);
|
|
$expect($controller->esignReviewConfirm()['code'] === 0
|
|
&& count(app\common\service\esign\PrescriptionReviewEsignService::$calls) === 2, 'actor mismatch cannot dispatch review');
|
|
|
|
$code = '023941';
|
|
$mobile = '13800000000';
|
|
$authUrl = 'https://smlt.esign.cn/example/private-auth-url';
|
|
$controller->request = (new app\Request())->setMethod('POST')->withPost(['id' => 42, 'auth_code' => $code, 'doctor_id' => 41]);
|
|
$expect($controller->esignVerifyWillingness()['code'] === 1
|
|
&& app\common\service\esign\PrescriptionEsignService::$verifications === [[42, 7, $controller->adminInfo, $code]], 'OTP verifies only for current authenticated actor; leading zero preserved');
|
|
$controller->request = (new app\Request())->setMethod('GET')->withGet(['id' => 42, 'auth_code' => $code]);
|
|
$expect($controller->esignVerifyWillingness()['code'] === 0, 'GET cannot submit an OTP');
|
|
foreach (['esignVerifyWillingness', 'esignDoctorStart', 'esignDoctorComplete', 'esignDoctorStatus', 'detail'] as $action) {
|
|
$privacyRequest = (new app\Request())->setMethod('POST')->setController('tcm.Prescription')->setAction($action)
|
|
->setUrl('/adminapi/tcm.prescription/' . $action . '?mobile=' . $mobile);
|
|
if ($action !== 'detail') {
|
|
$privacyRequest->withPost(['id' => 42, 'mobile' => $mobile, 'identity_number' => $number, 'auth_code' => $code]);
|
|
}
|
|
$privacyRequest->controllerObject = $controller;
|
|
$privacyRequest->adminInfo = $controller->adminInfo;
|
|
think\Container::getInstance()->instance('request', $privacyRequest);
|
|
$privacyResponse = new class($authUrl) {
|
|
public function __construct(private string $url) {}
|
|
public function getContent(): string { return json_encode(['code' => 1, 'data' => ['status' => 'authenticating', 'auth_url' => $this->url]]); }
|
|
};
|
|
if ($action === 'detail') {
|
|
$privacyRequest->setUrl('/adminapi/tcm.prescription/detail');
|
|
}
|
|
(new app\adminapi\listener\OperationLog())->handle($privacyResponse);
|
|
$audit = end(app\common\model\OperationLog::$saved);
|
|
$entry = json_encode($audit);
|
|
$expect(!str_contains($entry, $mobile) && !str_contains($entry, $number) && !str_contains($entry, $code)
|
|
&& !str_contains($audit['result'], 'private-auth-url'), "$action audit omits signing secrets and authorization URL");
|
|
}
|
|
$auditRequest = (new app\Request())->setMethod('POST');
|
|
$controller->request = $auditRequest;
|
|
think\Container::getInstance()->instance('request', $auditRequest);
|
|
think\Validate::maker(static function (think\Validate $validate) use ($auditRequest, $app): void {
|
|
$validate->setRequest($auditRequest);
|
|
$validate->setLang(new think\Lang($app));
|
|
});
|
|
foreach (['approve' => '审核通过', 'reject' => '已驳回并作废处方'] as $action => $message) {
|
|
$auditRequest->withPost(['id' => 42, 'action' => $action, 'remark' => '虚构测试']);
|
|
app\adminapi\logic\tcm\PrescriptionLogic::$notice = [
|
|
'ok' => false, 'errcode' => 81013, 'message' => 'user & party & tag all invalid, private provider diagnostic',
|
|
];
|
|
$expect($controller->audit() === ['code' => 1, 'msg' => $message, 'data' => ['wecom_notify_ok' => false]],
|
|
"$action stays successful and never exposes a WeCom notification error");
|
|
}
|
|
app\adminapi\logic\tcm\PrescriptionLogic::$notice = ['ok' => true];
|
|
$auditRequest->withPost(['id' => 42, 'action' => 'approve']);
|
|
$expect($controller->audit()['data'] === ['wecom_notify_ok' => true], 'successful delivery status remains available');
|
|
app\adminapi\logic\tcm\PrescriptionLogic::$auditOk = false;
|
|
$expect($controller->audit() === ['code' => 0, 'msg' => '无审核权限'], 'actual audit denial is still returned');
|
|
|
|
echo "eSign identity and prescription audit controller/privacy: $checks checks passed (isolated doubles, no DB/network)\n";
|
|
}
|