- 在多个控制器中将权限检查从 hasAdminPermission 更新为 hasPermissionCode,以增强权限管理的灵活性。 - 引入 AdminScopePolicy,优化基于代理节点的权限和数据过滤逻辑,确保管理员能够更精确地控制访问权限。 - 在请求验证中添加 agent_node_id 字段,确保 API 接口支持代理节点的相关操作。 - 更新 AdminUser 模型,新增 hasPermissionCode 方法,以支持更细粒度的权限检查。 - 优化审计日志记录逻辑,确保在处理请求时能够准确记录管理员的操作。
188 lines
6.3 KiB
PHP
188 lines
6.3 KiB
PHP
<?php
|
||
|
||
namespace App\Services\Wallet;
|
||
|
||
use App\Models\Player;
|
||
use Illuminate\Support\Facades\Http;
|
||
use App\Services\Integration\PartnerSiteConfigResolver;
|
||
use App\Support\Integration\WalletApiUrlSanitizer;
|
||
|
||
/**
|
||
* 查询主站钱包余额(供玩家端余额接口填充 main_balance)。
|
||
*/
|
||
final class HttpMainSiteWalletBalanceClient
|
||
{
|
||
public function __construct(
|
||
private readonly PartnerSiteConfigResolver $partnerSiteConfigResolver,
|
||
) {}
|
||
|
||
public function fetch(Player $player, string $currencyCode): ?int
|
||
{
|
||
$probe = $this->probe($player, $currencyCode);
|
||
|
||
return $probe->success ? $probe->mainBalanceMinor : null;
|
||
}
|
||
|
||
public function probe(Player $player, string $currencyCode): MainSiteWalletBalanceProbeResult
|
||
{
|
||
$config = $this->partnerSiteConfigResolver->resolveForPlayer($player);
|
||
$currencyCode = trim($currencyCode) !== '' ? trim($currencyCode) : 'NPR';
|
||
|
||
if (! $config->enabled) {
|
||
return new MainSiteWalletBalanceProbeResult(
|
||
success: false,
|
||
mainBalanceMinor: null,
|
||
currencyCode: $currencyCode,
|
||
requestUrl: '',
|
||
httpStatus: null,
|
||
message: '接入站点已停用或未配置',
|
||
responseBody: null,
|
||
);
|
||
}
|
||
|
||
if (! $config->hasWalletApi()) {
|
||
return new MainSiteWalletBalanceProbeResult(
|
||
success: false,
|
||
mainBalanceMinor: null,
|
||
currencyCode: $currencyCode,
|
||
requestUrl: '',
|
||
httpStatus: null,
|
||
message: '未配置主站钱包 API URL',
|
||
responseBody: null,
|
||
);
|
||
}
|
||
|
||
$base = WalletApiUrlSanitizer::normalizeAndValidate($config->walletApiUrl);
|
||
if ($base === null) {
|
||
return new MainSiteWalletBalanceProbeResult(
|
||
success: false,
|
||
mainBalanceMinor: null,
|
||
currencyCode: $currencyCode,
|
||
requestUrl: '',
|
||
httpStatus: null,
|
||
message: 'wallet_api_url 无效(拒绝以防 SSRF)',
|
||
responseBody: null,
|
||
);
|
||
}
|
||
|
||
$path = $config->walletBalancePath;
|
||
$url = $base.'/'.ltrim($path, '/');
|
||
$timeout = $config->walletTimeoutSeconds;
|
||
$apiKey = $config->walletApiKey;
|
||
|
||
if (app()->environment(['production'])
|
||
&& $config->source === \App\Services\Integration\PartnerSiteConfig::SOURCE_LEGACY_ENV
|
||
&& (! is_string($apiKey) || trim($apiKey) === '')
|
||
) {
|
||
return new MainSiteWalletBalanceProbeResult(
|
||
success: false,
|
||
mainBalanceMinor: null,
|
||
currencyCode: $currencyCode,
|
||
requestUrl: '',
|
||
httpStatus: null,
|
||
message: 'MAIN_SITE_WALLET_API_KEY 未配置',
|
||
responseBody: null,
|
||
);
|
||
}
|
||
|
||
$headers = ['Accept' => 'application/json'];
|
||
if (is_string($apiKey) && $apiKey !== '') {
|
||
$headers['Authorization'] = 'Bearer '.$apiKey;
|
||
}
|
||
|
||
$query = [
|
||
'site_code' => $player->site_code,
|
||
'site_player_id' => $player->site_player_id,
|
||
'currency_code' => $currencyCode,
|
||
];
|
||
|
||
try {
|
||
$response = Http::withHeaders($headers)
|
||
->timeout($timeout)
|
||
->acceptJson()
|
||
->get($url, $query);
|
||
} catch (\Throwable $e) {
|
||
return new MainSiteWalletBalanceProbeResult(
|
||
success: false,
|
||
mainBalanceMinor: null,
|
||
currencyCode: $currencyCode,
|
||
requestUrl: $url.'?'.http_build_query($query),
|
||
httpStatus: null,
|
||
message: '请求失败: '.$e->getMessage(),
|
||
responseBody: null,
|
||
);
|
||
}
|
||
|
||
$httpStatus = $response->status();
|
||
$payload = $response->json();
|
||
$preview = is_array($payload) ? self::truncateResponsePreview($payload) : null;
|
||
|
||
if (! $response->successful()) {
|
||
$message = is_array($payload) && is_string($payload['message'] ?? null)
|
||
? (string) $payload['message']
|
||
: 'HTTP '.$httpStatus;
|
||
|
||
return new MainSiteWalletBalanceProbeResult(
|
||
success: false,
|
||
mainBalanceMinor: null,
|
||
currencyCode: $currencyCode,
|
||
requestUrl: $url.'?'.http_build_query($query),
|
||
httpStatus: $httpStatus,
|
||
message: $message,
|
||
responseBody: $preview,
|
||
);
|
||
}
|
||
|
||
if (! is_array($payload)) {
|
||
return new MainSiteWalletBalanceProbeResult(
|
||
success: false,
|
||
mainBalanceMinor: null,
|
||
currencyCode: $currencyCode,
|
||
requestUrl: $url.'?'.http_build_query($query),
|
||
httpStatus: $httpStatus,
|
||
message: '响应不是 JSON 对象',
|
||
responseBody: null,
|
||
);
|
||
}
|
||
|
||
$raw = data_get($payload, 'data.main_balance')
|
||
?? data_get($payload, 'main_balance');
|
||
|
||
if (! is_numeric($raw)) {
|
||
return new MainSiteWalletBalanceProbeResult(
|
||
success: false,
|
||
mainBalanceMinor: null,
|
||
currencyCode: $currencyCode,
|
||
requestUrl: $url.'?'.http_build_query($query),
|
||
httpStatus: $httpStatus,
|
||
message: '响应缺少 main_balance 数值',
|
||
responseBody: $preview,
|
||
);
|
||
}
|
||
|
||
return new MainSiteWalletBalanceProbeResult(
|
||
success: true,
|
||
mainBalanceMinor: max(0, (int) $raw),
|
||
currencyCode: (string) (data_get($payload, 'data.currency_code') ?? $currencyCode),
|
||
requestUrl: $url.'?'.http_build_query($query),
|
||
httpStatus: $httpStatus,
|
||
message: null,
|
||
responseBody: $preview,
|
||
);
|
||
}
|
||
|
||
/**
|
||
* @param array<string, mixed> $payload
|
||
* @return array<string, mixed>
|
||
*/
|
||
private static function truncateResponsePreview(array $payload): array
|
||
{
|
||
$json = json_encode($payload, JSON_UNESCAPED_UNICODE);
|
||
if (is_string($json) && strlen($json) > 512) {
|
||
return ['_truncated' => true, 'preview' => substr($json, 0, 512)];
|
||
}
|
||
|
||
return $payload;
|
||
}
|
||
}
|