FundBalanceHandler.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. namespace App\Module\OpenAPI\Handlers\Fund;
  3. use App\Module\OpenAPI\Handlers\BaseHandler;
  4. use App\Module\OpenAPI\Services\ScopeService;
  5. use App\Module\OpenAPI\Enums\SCOPE_TYPE;
  6. use Illuminate\Http\JsonResponse;
  7. /**
  8. * 资金余额Handler
  9. *
  10. * 处理获取用户资金余额的业务逻辑
  11. */
  12. class FundBalanceHandler extends BaseHandler
  13. {
  14. public function __construct(ScopeService $scopeService)
  15. {
  16. parent::__construct($scopeService);
  17. }
  18. /**
  19. * 获取所需的权限范围
  20. *
  21. * @return SCOPE_TYPE[]
  22. */
  23. public function getRequiredScopes(): array
  24. {
  25. return [SCOPE_TYPE::FUND_READ];
  26. }
  27. /**
  28. * 处理获取资金余额请求
  29. *
  30. * @param array $data 请求数据
  31. * @param array $context 上下文信息
  32. * @return JsonResponse
  33. */
  34. protected function process(array $data, array $context = []): JsonResponse
  35. {
  36. // 验证请求参数
  37. $validationErrors = $this->validateData($data, [
  38. 'user_id' => 'required|integer|min:1',
  39. 'currency_types' => 'array',
  40. 'currency_types.*' => 'string|max:20',
  41. 'include_frozen' => 'boolean',
  42. ]);
  43. if ($validationErrors) {
  44. return $this->errorResponse('参数验证失败', $validationErrors, 422);
  45. }
  46. // 获取参数
  47. $userId = $data['user_id'];
  48. $currencyTypes = $data['currency_types'] ?? [];
  49. $includeFrozen = $data['include_frozen'] ?? false;
  50. // 调用Fund模块服务获取余额信息
  51. $balanceInfo = $this->getFundBalance($userId, $currencyTypes, $includeFrozen);
  52. if (!$balanceInfo) {
  53. return $this->errorResponse('用户不存在或无资金账户', null, 404);
  54. }
  55. // 记录操作日志
  56. $this->logAction('fund.balance.get', [
  57. 'user_id' => $userId,
  58. 'currency_types' => $currencyTypes,
  59. 'include_frozen' => $includeFrozen,
  60. ], $context);
  61. return $this->successResponse('获取资金余额成功', $balanceInfo);
  62. }
  63. /**
  64. * 获取用户资金余额
  65. *
  66. * 这里应该调用Fund模块的实际服务
  67. *
  68. * @param int $userId
  69. * @param array $currencyTypes
  70. * @param bool $includeFrozen
  71. * @return array|null
  72. */
  73. protected function getFundBalance(int $userId, array $currencyTypes, bool $includeFrozen): ?array
  74. {
  75. // TODO: 这里应该调用Fund模块的服务
  76. // 例如: return app(FundService::class)->getUserBalance($userId, $currencyTypes, $includeFrozen);
  77. // 暂时返回示例数据,等待与Fund模块集成
  78. $allCurrencies = [
  79. 'GOLD' => '金币',
  80. 'DIAMOND' => '钻石',
  81. 'SILVER' => '银币',
  82. 'ENERGY' => '能量',
  83. 'EXP' => '经验值',
  84. ];
  85. // 如果指定了货币类型,只返回指定的
  86. $currencies = empty($currencyTypes) ? array_keys($allCurrencies) : $currencyTypes;
  87. $balances = [];
  88. $totalValue = 0;
  89. foreach ($currencies as $currencyType) {
  90. if (!isset($allCurrencies[$currencyType])) {
  91. continue;
  92. }
  93. $available = rand(1000, 50000) / 100; // 随机余额
  94. $frozen = $includeFrozen ? rand(0, 1000) / 100 : 0;
  95. $total = $available + $frozen;
  96. $balances[] = [
  97. 'currency_type' => $currencyType,
  98. 'currency_name' => $allCurrencies[$currencyType],
  99. 'available' => number_format($available, 2, '.', ''),
  100. 'frozen' => number_format($frozen, 2, '.', ''),
  101. 'total' => number_format($total, 2, '.', ''),
  102. 'last_updated' => now()->subMinutes(rand(1, 60))->toISOString(),
  103. ];
  104. // 简单的价值计算(假设汇率)
  105. $rate = ['GOLD' => 1, 'DIAMOND' => 10, 'SILVER' => 0.1, 'ENERGY' => 0.01, 'EXP' => 0.001][$currencyType] ?? 1;
  106. $totalValue += $total * $rate;
  107. }
  108. return [
  109. 'user_id' => $userId,
  110. 'balances' => $balances,
  111. 'summary' => [
  112. 'total_currencies' => count($balances),
  113. 'estimated_value' => number_format($totalValue, 2, '.', ''),
  114. 'value_currency' => 'GOLD',
  115. ],
  116. 'account_info' => [
  117. 'account_status' => 'active',
  118. 'account_level' => rand(1, 10),
  119. 'created_at' => now()->subDays(rand(30, 365))->toISOString(),
  120. 'last_transaction' => now()->subHours(rand(1, 24))->toISOString(),
  121. ],
  122. 'settings' => [
  123. 'include_frozen' => $includeFrozen,
  124. 'currency_filter' => $currencyTypes,
  125. ],
  126. ];
  127. }
  128. }