feat: 添加货币格式化功能,更新多个控制器以支持金额和余额的格式化显示,增强 API 响应的可读性
This commit is contained in:
@@ -9,6 +9,7 @@ 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;
|
||||
|
||||
/**
|
||||
@@ -53,14 +54,19 @@ final class WalletBalanceController extends Controller
|
||||
|
||||
$balance = (int) $wallet->balance;
|
||||
$frozen = (int) $wallet->frozen_balance;
|
||||
$available = max(0, $balance - $frozen);
|
||||
|
||||
return ApiResponse::success([
|
||||
'balance' => $balance,
|
||||
'available_balance' => max(0, $balance - $frozen),
|
||||
'balance_formatted' => CurrencyFormatter::fromMinor($balance),
|
||||
'available_balance' => $available,
|
||||
'available_balance_formatted' => CurrencyFormatter::fromMinor($available),
|
||||
'main_balance' => null,
|
||||
'main_balance_formatted' => null,
|
||||
'currency_code' => $wallet->currency_code,
|
||||
'wallet_type' => $wallet->wallet_type,
|
||||
'frozen_balance' => $frozen,
|
||||
'frozen_balance_formatted' => CurrencyFormatter::fromMinor($frozen),
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
@@ -9,6 +9,7 @@ use Illuminate\Http\Request;
|
||||
use App\Models\TransferOrder;
|
||||
use App\Support\PaginationTrait;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use App\Support\CurrencyFormatter;
|
||||
use App\Http\Controllers\Controller;
|
||||
|
||||
/**
|
||||
@@ -123,16 +124,21 @@ final class WalletLogsController extends Controller
|
||||
private function formatTxn(WalletTxn $txn): array
|
||||
{
|
||||
$currency = $txn->wallet?->currency_code ?? '';
|
||||
$amount = (int) $txn->amount;
|
||||
$balanceAfter = (int) $txn->balance_after;
|
||||
|
||||
return [
|
||||
'log_id' => $txn->txn_no,
|
||||
'type' => $this->bizToPublicType((string) $txn->biz_type),
|
||||
'biz_type' => $txn->biz_type,
|
||||
'amount' => $this->signedAmount($txn),
|
||||
'amount_abs' => (int) $txn->amount,
|
||||
'amount_formatted' => CurrencyFormatter::fromMinor($amount),
|
||||
'amount_abs' => $amount,
|
||||
'amount_abs_formatted' => CurrencyFormatter::fromMinor($amount),
|
||||
'direction' => (int) $txn->direction === 1 ? 'in' : 'out',
|
||||
'currency_code' => $currency,
|
||||
'balance_after' => (int) $txn->balance_after,
|
||||
'balance_after' => $balanceAfter,
|
||||
'balance_after_formatted' => CurrencyFormatter::fromMinor($balanceAfter),
|
||||
'ref_id' => $txn->biz_no,
|
||||
'idempotent_key' => $txn->idempotent_key,
|
||||
'external_ref_no' => $txn->external_ref_no,
|
||||
@@ -162,12 +168,15 @@ final class WalletLogsController extends Controller
|
||||
*/
|
||||
private function formatPendingOrder(TransferOrder $order): array
|
||||
{
|
||||
$amount = (int) $order->amount;
|
||||
|
||||
return [
|
||||
'transfer_no' => $order->transfer_no,
|
||||
'direction' => $order->direction,
|
||||
'type' => $order->direction === 'in' ? 'transfer_in' : 'transfer_out',
|
||||
'currency_code' => $order->currency_code,
|
||||
'amount' => (int) $order->amount,
|
||||
'amount' => $amount,
|
||||
'amount_formatted' => CurrencyFormatter::fromMinor($amount),
|
||||
'status' => $order->status,
|
||||
'fail_reason' => $order->fail_reason,
|
||||
'idempotent_key' => $order->idempotent_key,
|
||||
|
||||
Reference in New Issue
Block a user