lotteryPlayer(); abort_if($player === null, 500, 'lottery_player missing'); $currencyCode = $this->resolveCurrencyCode($request, $player); if ($currencyCode instanceof JsonResponse) { return $currencyCode; } $wallet = PlayerWallet::query()->firstOrCreate( [ 'player_id' => $player->id, 'wallet_type' => self::WALLET_TYPE_LOTTERY, 'currency_code' => $currencyCode, ], [ 'balance' => 0, 'frozen_balance' => 0, 'status' => 0, 'version' => 0, ], ); return ApiResponse::success([ 'balance' => $wallet->balance, 'main_balance' => null, 'currency_code' => $wallet->currency_code, 'wallet_type' => $wallet->wallet_type, 'frozen_balance' => $wallet->frozen_balance, ]); } /** * @return string|JsonResponse 合法币种码或错误响应({@see ErrorCode::WalletInvalidCurrency}) */ private function resolveCurrencyCode(Request $request, Player $player): string|JsonResponse { $raw = $request->query('currency'); if (is_string($raw) && $raw !== '') { $code = strtoupper(substr(trim($raw), 0, 16)); } else { $fallback = $player->default_currency ?? config('lottery.default_currency', 'NPR'); $code = strtoupper(substr(trim((string) $fallback), 0, 16)); } // 币种码:字母数字,长度 1–16,与 migrations 字段一致 if (! preg_match('/^[A-Z0-9]{1,16}$/', $code)) { return ApiResponse::error( __('wallet.invalid_currency'), ErrorCode::WalletInvalidCurrency->value, null, 400, ); } return $code; } }