Files
lotteryLaravel/app/Http/Controllers/Api/V1/Wallet/WalletBalanceController.php
kang e6cf94af46 refactor: 使用 ApiMessage 统一错误响应格式
- 在多个控制器中引入 ApiMessage,替换原有的 ApiResponse 错误处理逻辑,确保错误信息的一致性与可读性。
- 更新错误返回信息,使用更具语义的键值,提升 API 的可维护性与用户体验。
- 适配相关控制器的请求参数,确保在处理错误时能够正确返回相应的错误信息。
2026-06-01 14:23:48 +08:00

102 lines
3.7 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
namespace App\Http\Controllers\Api\V1\Wallet;
use App\Models\Player;
use App\Lottery\ErrorCode;
use App\Models\PlayerWallet;
use App\Support\ApiMessage;
use App\Support\ApiResponse;
use Illuminate\Http\Request;
use App\Support\CurrencyResolver;
use Illuminate\Http\JsonResponse;
use App\Support\CurrencyFormatter;
use App\Http\Controllers\Controller;
use App\Services\Wallet\HttpMainSiteWalletBalanceClient;
/**
* 【玩家】查询彩票侧钱包余额。
*
* 路由:`GET /api/v1/wallet/balance`,需 middleware `lottery.player`。
* PRD`01-产品文档.md` §10.1.1;金额单位为最小货币 bigint参见 `docs/04` §8
*
* 【行为要点】
* - 币种:优先 Query `currency`,否则使用玩家 `default_currency`,再回退 `config('lottery.default_currency')`
* - 若尚无 `player_wallets` 记录:按 `wallet_type=lottery` + 币种 **首次开立**一行(余额 0便于新玩家直接进入查询
* - `available_balance``balance - frozen_balance`,表示当前可用于下注的整数最小货币单位(不为负)
* - `main_balance`:已配置 `MAIN_SITE_WALLET_API_URL` 时向主站查询;否则 `null`
*/
final class WalletBalanceController extends Controller
{
private const WALLET_TYPE_LOTTERY = 'lottery';
public function __construct(
private readonly HttpMainSiteWalletBalanceClient $mainSiteBalanceClient,
) {}
public function __invoke(Request $request): JsonResponse
{
$player = $request->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,
],
);
$balance = (int) $wallet->balance;
$frozen = (int) $wallet->frozen_balance;
$available = max(0, $balance - $frozen);
$mainBalance = $this->mainSiteBalanceClient->fetch($player, $currencyCode);
return ApiResponse::success([
'balance' => $balance,
'balance_formatted' => CurrencyFormatter::fromMinor($balance),
'available_balance' => $available,
'available_balance_formatted' => CurrencyFormatter::fromMinor($available),
'main_balance' => $mainBalance,
'main_balance_formatted' => $mainBalance !== null
? CurrencyFormatter::fromMinor($mainBalance)
: null,
'currency_code' => $wallet->currency_code,
'wallet_type' => $wallet->wallet_type,
'frozen_balance' => $frozen,
'frozen_balance_formatted' => CurrencyFormatter::fromMinor($frozen),
]);
}
/**
* @return string|JsonResponse 合法币种码或错误响应({@see ErrorCode::WalletInvalidCurrency}
*/
private function resolveCurrencyCode(Request $request, Player $player): string|JsonResponse
{
$code = CurrencyResolver::resolve($request, $player, 'currency');
if (! CurrencyResolver::isEnabled($code)) {
return ApiResponse::error(
ApiMessage::get($request, 'wallet.invalid_currency'),
ErrorCode::WalletInvalidCurrency->value,
null,
400,
);
}
return $code;
}
}