This commit is contained in:
2026-07-28 15:15:14 +08:00
parent 30cf33c546
commit 9ba53b6145
3 changed files with 69 additions and 0 deletions
+4
View File
@@ -8,6 +8,10 @@ EJ_PHARMACY_BASE_URL = "https://ej.example.com"
EJ_PHARMACY_APP_KEY = "" EJ_PHARMACY_APP_KEY = ""
EJ_PHARMACY_APP_SECRET = "" EJ_PHARMACY_APP_SECRET = ""
EJ_PHARMACY_CALLBACK_SECRET = "" EJ_PHARMACY_CALLBACK_SECRET = ""
# autoNSS cURL 自动使用 PHP OpenSSL;也可显式填写 openssl 或 curl。
EJ_PHARMACY_HTTP_TRANSPORT = "auto"
# CentOS/RHEL 7 可填写 /etc/pki/tls/certs/ca-bundle.crt;留空使用 PHP 默认 CA。
EJ_PHARMACY_CA_FILE = ""
EJ_PHARMACY_SUBMISSION_LEASE_SECONDS = 300 EJ_PHARMACY_SUBMISSION_LEASE_SECONDS = 300
EJ_PHARMACY_CONNECT_TIMEOUT = 5 EJ_PHARMACY_CONNECT_TIMEOUT = 5
EJ_PHARMACY_REQUEST_TIMEOUT = 30 EJ_PHARMACY_REQUEST_TIMEOUT = 30
@@ -104,6 +104,14 @@ final class EjPharmacyClient
return ($this->transport)(strtoupper($method), $pathWithQuery, $body, $headers); return ($this->transport)(strtoupper($method), $pathWithQuery, $body, $headers);
} }
$configuredTransport = strtolower((string) Config::get('ej_pharmacy.http_transport', 'auto'));
$curlSsl = strtoupper((string) ((function_exists('curl_version') ? curl_version() : [])['ssl_version'] ?? ''));
if ($configuredTransport === 'openssl'
|| ($configuredTransport === 'auto' && str_starts_with($curlSsl, 'NSS/'))
) {
return $this->requestWithOpenSsl($method, $pathWithQuery, $body, $headers, $requestId);
}
$ch = curl_init($this->baseUrl . $pathWithQuery); $ch = curl_init($this->baseUrl . $pathWithQuery);
curl_setopt_array($ch, [ curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true, CURLOPT_RETURNTRANSFER => true,
@@ -131,4 +139,56 @@ final class EjPharmacyClient
return ['http_status' => $httpStatus, 'body' => $decoded, 'request_id' => $requestId]; return ['http_status' => $httpStatus, 'body' => $decoded, 'request_id' => $requestId];
} }
/** @param array<int,string> $headers @return array{http_status:int,body:array<string,mixed>,request_id:string} */
private function requestWithOpenSsl(
string $method,
string $path,
string $body,
array $headers,
string $requestId
): array {
$ssl = [
'verify_peer' => true,
'verify_peer_name' => true,
'allow_self_signed' => false,
];
$caFile = trim((string) Config::get('ej_pharmacy.ca_file', ''));
if ($caFile !== '') {
$ssl['cafile'] = $caFile;
}
$context = stream_context_create([
'http' => [
'method' => strtoupper($method),
'header' => implode("\r\n", $headers),
'content' => $body,
'ignore_errors' => true,
'timeout' => (int) Config::get('ej_pharmacy.request_timeout', 30),
'protocol_version' => 1.1,
],
'ssl' => $ssl,
]);
$raw = @file_get_contents($this->baseUrl . $path, false, $context);
if ($raw === false) {
$lastError = error_get_last();
$message = is_array($lastError) ? (string) ($lastError['message'] ?? '') : '';
throw new RuntimeException('恩济药房通信失败:' . ($message !== '' ? $message : 'OpenSSL 请求失败'));
}
$httpStatus = 0;
$responseHeaders = $http_response_header ?? [];
foreach (array_reverse($responseHeaders) as $responseHeader) {
if (preg_match('/^HTTP\/\S+\s+(\d{3})\b/i', $responseHeader, $matches)) {
$httpStatus = (int) $matches[1];
break;
}
}
$decoded = json_decode((string) $raw, true);
if (!is_array($decoded)) {
throw new RuntimeException('恩济药房返回了无效 JSONHTTP ' . $httpStatus);
}
return ['http_status' => $httpStatus, 'body' => $decoded, 'request_id' => $requestId];
}
} }
+5
View File
@@ -13,6 +13,11 @@ return [
'app_key' => trim((string) env('EJ_PHARMACY_APP_KEY', '')), 'app_key' => trim((string) env('EJ_PHARMACY_APP_KEY', '')),
'app_secret' => trim((string) env('EJ_PHARMACY_APP_SECRET', '')), 'app_secret' => trim((string) env('EJ_PHARMACY_APP_SECRET', '')),
'callback_secret' => trim((string) env('EJ_PHARMACY_CALLBACK_SECRET', '')), 'callback_secret' => trim((string) env('EJ_PHARMACY_CALLBACK_SECRET', '')),
// RHEL/CentOS 7 ships cURL with NSS, which rejects some otherwise valid
// server certificate chains. `auto` selects the OpenSSL stream transport
// for NSS builds while keeping cURL for OpenSSL builds.
'http_transport' => strtolower(trim((string) env('EJ_PHARMACY_HTTP_TRANSPORT', 'auto'))),
'ca_file' => trim((string) env('EJ_PHARMACY_CA_FILE', '')),
'connect_timeout' => max((int) env('EJ_PHARMACY_CONNECT_TIMEOUT', 5), 1), 'connect_timeout' => max((int) env('EJ_PHARMACY_CONNECT_TIMEOUT', 5), 1),
'request_timeout' => max((int) env('EJ_PHARMACY_REQUEST_TIMEOUT', 30), 5), 'request_timeout' => max((int) env('EJ_PHARMACY_REQUEST_TIMEOUT', 30), 5),
]; ];