feat: 增强环境配置与开发服务,支持局域网访问及币种管理

This commit is contained in:
2026-05-21 16:24:41 +08:00
parent 699d43fbd4
commit 7a6048de10
60 changed files with 1321 additions and 443 deletions

View File

@@ -0,0 +1,69 @@
<?php
namespace App\Http\Controllers\Api\V1\Admin\Currency;
use App\Models\Currency;
use App\Lottery\ErrorCode;
use App\Support\ApiResponse;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Facades\DB;
use App\Http\Controllers\Controller;
final class AdminCurrencyDestroyController extends Controller
{
public function __invoke(Currency $currency): JsonResponse
{
$code = strtoupper((string) $currency->code);
if ($code === strtoupper((string) config('lottery.default_currency', 'NPR'))) {
return ApiResponse::error(
'默认币种不可删除',
ErrorCode::ValidationFailed->value,
null,
422,
);
}
$references = $this->referenceSummary($code);
if ($references !== []) {
return ApiResponse::error(
'该币种已被业务数据引用,暂不可删除:'.implode('、', $references),
ErrorCode::ValidationFailed->value,
['references' => $references],
422,
);
}
$id = (int) $currency->id;
$currency->delete();
return ApiResponse::success([
'deleted' => true,
'id' => $id,
'code' => $code,
]);
}
/** @return list<string> */
private function referenceSummary(string $code): array
{
$checks = [
'玩家默认币种' => DB::table('players')->where('default_currency', $code),
'玩家钱包' => DB::table('player_wallets')->where('currency_code', $code),
'转账单' => DB::table('transfer_orders')->where('currency_code', $code),
'注单' => DB::table('ticket_orders')->where('currency_code', $code),
'赔率配置' => DB::table('odds_items')->where('currency_code', $code),
'奖池' => DB::table('jackpot_pools')->where('currency_code', $code),
'Jackpot 贡献记录' => DB::table('jackpot_contributions')->where('currency_code', $code),
];
$references = [];
foreach ($checks as $label => $query) {
if ($query->exists()) {
$references[] = $label;
}
}
return $references;
}
}

View File

@@ -0,0 +1,39 @@
<?php
namespace App\Http\Controllers\Api\V1\Admin\Currency;
use App\Models\Currency;
use App\Support\ApiResponse;
use Illuminate\Http\JsonResponse;
use App\Http\Controllers\Controller;
final class AdminCurrencyIndexController extends Controller
{
public function __invoke(): JsonResponse
{
$items = Currency::query()
->orderByDesc('is_enabled')
->orderByDesc('is_bettable')
->orderBy('code')
->get()
->map(fn (Currency $currency): array => $this->serializeCurrency($currency))
->all();
return ApiResponse::success(['items' => $items]);
}
/** @return array<string, mixed> */
private function serializeCurrency(Currency $currency): array
{
return [
'id' => (int) $currency->id,
'code' => $currency->code,
'name' => $currency->name,
'decimal_places' => (int) $currency->decimal_places,
'is_enabled' => (bool) $currency->is_enabled,
'is_bettable' => (bool) $currency->is_bettable,
'created_at' => $currency->created_at?->toIso8601String(),
'updated_at' => $currency->updated_at?->toIso8601String(),
];
}
}

View File

@@ -0,0 +1,54 @@
<?php
namespace App\Http\Controllers\Api\V1\Admin\Currency;
use App\Models\Currency;
use App\Support\ApiResponse;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Facades\DB;
use App\Http\Controllers\Controller;
use App\Services\Currency\CurrencyActivationService;
use App\Http\Requests\Admin\AdminCurrencyStoreRequest;
final class AdminCurrencyStoreController extends Controller
{
public function __invoke(
AdminCurrencyStoreRequest $request,
CurrencyActivationService $activationService,
): JsonResponse {
$data = $request->validated();
$enabled = (bool) ($data['is_enabled'] ?? true);
$bettable = $enabled && (bool) ($data['is_bettable'] ?? false);
$currency = DB::transaction(function () use ($data, $enabled, $bettable, $activationService): Currency {
$currency = Currency::query()->create([
'code' => $data['code'],
'name' => $data['name'],
'decimal_places' => (int) ($data['decimal_places'] ?? 2),
'is_enabled' => $enabled,
'is_bettable' => $bettable,
]);
$activationService->ensureBettableCurrencyReady($currency);
return $currency->fresh();
});
return ApiResponse::success($this->serializeCurrency($currency))->setStatusCode(201);
}
/** @return array<string, mixed> */
private function serializeCurrency(Currency $currency): array
{
return [
'id' => (int) $currency->id,
'code' => $currency->code,
'name' => $currency->name,
'decimal_places' => (int) $currency->decimal_places,
'is_enabled' => (bool) $currency->is_enabled,
'is_bettable' => (bool) $currency->is_bettable,
'created_at' => $currency->created_at?->toIso8601String(),
'updated_at' => $currency->updated_at?->toIso8601String(),
];
}
}

View File

@@ -0,0 +1,62 @@
<?php
namespace App\Http\Controllers\Api\V1\Admin\Currency;
use App\Models\Currency;
use App\Support\ApiResponse;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Facades\DB;
use App\Http\Controllers\Controller;
use App\Services\Currency\CurrencyActivationService;
use App\Http\Requests\Admin\AdminCurrencyUpdateRequest;
final class AdminCurrencyUpdateController extends Controller
{
public function __invoke(
AdminCurrencyUpdateRequest $request,
Currency $currency,
CurrencyActivationService $activationService,
): JsonResponse {
$data = $request->validated();
if (array_key_exists('is_enabled', $data) && ! (bool) $data['is_enabled']) {
$data['is_bettable'] = false;
} elseif (array_key_exists('is_bettable', $data)) {
$data['is_bettable'] = (bool) $data['is_bettable'];
}
if (array_key_exists('decimal_places', $data)) {
$data['decimal_places'] = (int) $data['decimal_places'];
}
if (array_key_exists('is_enabled', $data)) {
$data['is_enabled'] = (bool) $data['is_enabled'];
}
$currency = DB::transaction(function () use ($currency, $data, $activationService): Currency {
$currency->fill($data);
$currency->save();
$activationService->ensureBettableCurrencyReady($currency);
return $currency->fresh();
});
return ApiResponse::success($this->serializeCurrency($currency));
}
/** @return array<string, mixed> */
private function serializeCurrency(Currency $currency): array
{
return [
'id' => (int) $currency->id,
'code' => $currency->code,
'name' => $currency->name,
'decimal_places' => (int) $currency->decimal_places,
'is_enabled' => (bool) $currency->is_enabled,
'is_bettable' => (bool) $currency->is_bettable,
'created_at' => $currency->created_at?->toIso8601String(),
'updated_at' => $currency->updated_at?->toIso8601String(),
];
}
}