| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148 |
- <?php
- namespace App\Module\OpenAPI\Handlers\Fund;
- use App\Module\OpenAPI\Handlers\BaseHandler;
- use App\Module\OpenAPI\Services\ScopeService;
- use App\Module\OpenAPI\Enums\SCOPE_TYPE;
- use Illuminate\Http\JsonResponse;
- /**
- * 资金余额Handler
- *
- * 处理获取用户资金余额的业务逻辑
- */
- class FundBalanceHandler extends BaseHandler
- {
- public function __construct(ScopeService $scopeService)
- {
- parent::__construct($scopeService);
- }
- /**
- * 获取所需的权限范围
- *
- * @return SCOPE_TYPE[]
- */
- public function getRequiredScopes(): array
- {
- return [SCOPE_TYPE::FUND_READ];
- }
- /**
- * 处理获取资金余额请求
- *
- * @param array $data 请求数据
- * @param array $context 上下文信息
- * @return JsonResponse
- */
- protected function process(array $data, array $context = []): JsonResponse
- {
- // 验证请求参数
- $validationErrors = $this->validateData($data, [
- 'user_id' => 'required|integer|min:1',
- 'currency_types' => 'array',
- 'currency_types.*' => 'string|max:20',
- 'include_frozen' => 'boolean',
- ]);
- if ($validationErrors) {
- return $this->errorResponse('参数验证失败', $validationErrors, 422);
- }
- // 获取参数
- $userId = $data['user_id'];
- $currencyTypes = $data['currency_types'] ?? [];
- $includeFrozen = $data['include_frozen'] ?? false;
- // 调用Fund模块服务获取余额信息
- $balanceInfo = $this->getFundBalance($userId, $currencyTypes, $includeFrozen);
- if (!$balanceInfo) {
- return $this->errorResponse('用户不存在或无资金账户', null, 404);
- }
- // 记录操作日志
- $this->logAction('fund.balance.get', [
- 'user_id' => $userId,
- 'currency_types' => $currencyTypes,
- 'include_frozen' => $includeFrozen,
- ], $context);
- return $this->successResponse('获取资金余额成功', $balanceInfo);
- }
- /**
- * 获取用户资金余额
- *
- * 这里应该调用Fund模块的实际服务
- *
- * @param int $userId
- * @param array $currencyTypes
- * @param bool $includeFrozen
- * @return array|null
- */
- protected function getFundBalance(int $userId, array $currencyTypes, bool $includeFrozen): ?array
- {
- // TODO: 这里应该调用Fund模块的服务
- // 例如: return app(FundService::class)->getUserBalance($userId, $currencyTypes, $includeFrozen);
-
- // 暂时返回示例数据,等待与Fund模块集成
- $allCurrencies = [
- 'GOLD' => '金币',
- 'DIAMOND' => '钻石',
- 'SILVER' => '银币',
- 'ENERGY' => '能量',
- 'EXP' => '经验值',
- ];
- // 如果指定了货币类型,只返回指定的
- $currencies = empty($currencyTypes) ? array_keys($allCurrencies) : $currencyTypes;
-
- $balances = [];
- $totalValue = 0;
- foreach ($currencies as $currencyType) {
- if (!isset($allCurrencies[$currencyType])) {
- continue;
- }
- $available = rand(1000, 50000) / 100; // 随机余额
- $frozen = $includeFrozen ? rand(0, 1000) / 100 : 0;
- $total = $available + $frozen;
- $balances[] = [
- 'currency_type' => $currencyType,
- 'currency_name' => $allCurrencies[$currencyType],
- 'available' => number_format($available, 2, '.', ''),
- 'frozen' => number_format($frozen, 2, '.', ''),
- 'total' => number_format($total, 2, '.', ''),
- 'last_updated' => now()->subMinutes(rand(1, 60))->toISOString(),
- ];
- // 简单的价值计算(假设汇率)
- $rate = ['GOLD' => 1, 'DIAMOND' => 10, 'SILVER' => 0.1, 'ENERGY' => 0.01, 'EXP' => 0.001][$currencyType] ?? 1;
- $totalValue += $total * $rate;
- }
- return [
- 'user_id' => $userId,
- 'balances' => $balances,
- 'summary' => [
- 'total_currencies' => count($balances),
- 'estimated_value' => number_format($totalValue, 2, '.', ''),
- 'value_currency' => 'GOLD',
- ],
- 'account_info' => [
- 'account_status' => 'active',
- 'account_level' => rand(1, 10),
- 'created_at' => now()->subDays(rand(30, 365))->toISOString(),
- 'last_transaction' => now()->subHours(rand(1, 24))->toISOString(),
- ],
- 'settings' => [
- 'include_frozen' => $includeFrozen,
- 'currency_filter' => $currencyTypes,
- ],
- ];
- }
- }
|