feat: 更新玩法配置管理,简化字段并增强功能
- 将玩法相关的显示名称字段统一为 `display_name`,移除多语言字段。 - 在 `PlayTypePatchController` 中新增即时切换玩法开关的功能,并推送大厅更新。 - 优化多个控制器和服务中的权限检查与数据处理逻辑,提升代码可读性与维护性。
This commit is contained in:
@@ -40,9 +40,7 @@ final class PlayConfigItemsReplaceController extends Controller
|
||||
'items.*.category' => ['required', 'string', 'max:16'],
|
||||
'items.*.dimension' => ['nullable', 'integer', 'min:0', 'max:255'],
|
||||
'items.*.bet_mode' => ['nullable', 'string', 'max:32'],
|
||||
'items.*.display_name_zh' => ['required', 'string', 'max:64'],
|
||||
'items.*.display_name_en' => ['nullable', 'string', 'max:64'],
|
||||
'items.*.display_name_ne' => ['nullable', 'string', 'max:64'],
|
||||
'items.*.display_name' => ['required', 'string', 'max:64'],
|
||||
'items.*.is_enabled' => ['sometimes', 'boolean'],
|
||||
'items.*.min_bet_amount' => ['required', 'integer', 'min:0'],
|
||||
'items.*.max_bet_amount' => ['required', 'integer', 'min:0'],
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api\V1\Admin\Dashboard;
|
||||
|
||||
use App\Models\AdminUser;
|
||||
use App\Lottery\ErrorCode;
|
||||
use App\Support\ApiResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Requests\Admin\DashboardAnalyticsRequest;
|
||||
use App\Services\Admin\AdminDashboardAnalyticsBuilder;
|
||||
|
||||
/**
|
||||
* GET /api/v1/admin/dashboard/analytics — 可筛选区间的财务趋势与玩法拆解。
|
||||
*/
|
||||
final class AdminDashboardAnalyticsController extends Controller
|
||||
{
|
||||
public function __construct(
|
||||
private readonly AdminDashboardAnalyticsBuilder $analytics,
|
||||
) {}
|
||||
|
||||
public function __invoke(DashboardAnalyticsRequest $request): JsonResponse
|
||||
{
|
||||
$admin = $request->lotteryAdmin();
|
||||
if (! $admin instanceof AdminUser) {
|
||||
return ApiResponse::error(
|
||||
trans('admin.unauthenticated', [], $request->lotteryLocale()),
|
||||
ErrorCode::AdminUnauthenticated->value,
|
||||
null,
|
||||
401,
|
||||
);
|
||||
}
|
||||
|
||||
$payload = $this->analytics->build($admin, $request->filters());
|
||||
if ($payload === null) {
|
||||
return ApiResponse::error(
|
||||
trans('admin.forbidden', [], $request->lotteryLocale()),
|
||||
ErrorCode::AdminForbidden->value,
|
||||
null,
|
||||
403,
|
||||
);
|
||||
}
|
||||
|
||||
return ApiResponse::success($payload);
|
||||
}
|
||||
}
|
||||
@@ -2,62 +2,56 @@
|
||||
|
||||
namespace App\Http\Controllers\Api\V1\Admin\Jackpot;
|
||||
|
||||
use App\Models\AdminUser;
|
||||
use App\Models\JackpotPool;
|
||||
use App\Support\ApiResponse;
|
||||
use App\Lottery\ErrorCode;
|
||||
use Illuminate\Http\Request;
|
||||
use App\Models\JackpotPayoutLog;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Services\Jackpot\JackpotManualBurstService;
|
||||
|
||||
final class AdminJackpotPoolManualBurstController extends Controller
|
||||
{
|
||||
public function __construct(
|
||||
private readonly JackpotManualBurstService $service,
|
||||
) {}
|
||||
|
||||
public function __invoke(Request $request, JackpotPool $pool): JsonResponse
|
||||
{
|
||||
$admin = $request->user();
|
||||
if (! $admin instanceof AdminUser) {
|
||||
return ApiResponse::error(
|
||||
trans('admin.unauthenticated', [], $request->lotteryLocale()),
|
||||
ErrorCode::AdminUnauthenticated->value,
|
||||
null,
|
||||
401,
|
||||
);
|
||||
}
|
||||
|
||||
if (! $admin->isSuperAdmin()) {
|
||||
return ApiResponse::error(
|
||||
trans('admin.permission_denied', [], $request->lotteryLocale()),
|
||||
ErrorCode::AdminForbidden->value,
|
||||
['required_any' => [AdminUser::ROLE_SUPER_ADMIN]],
|
||||
403,
|
||||
);
|
||||
}
|
||||
|
||||
$data = $request->validate([
|
||||
'draw_id' => 'required|integer|exists:draws,id',
|
||||
'amount' => 'nullable|integer|min:1',
|
||||
]);
|
||||
|
||||
$payload = DB::transaction(function () use ($pool, $data): array {
|
||||
/** @var JackpotPool $locked */
|
||||
$locked = JackpotPool::query()->whereKey($pool->id)->lockForUpdate()->firstOrFail();
|
||||
$poolBefore = (int) $locked->current_amount;
|
||||
$amount = isset($data['amount']) ? min((int) $data['amount'], $poolBefore) : $poolBefore;
|
||||
|
||||
if ($amount <= 0) {
|
||||
return [
|
||||
'current_amount' => $poolBefore,
|
||||
'burst_amount' => 0,
|
||||
'log_id' => null,
|
||||
];
|
||||
}
|
||||
|
||||
$drawId = (int) $data['draw_id'];
|
||||
|
||||
$locked->forceFill([
|
||||
'current_amount' => $poolBefore - $amount,
|
||||
'last_trigger_draw_id' => $drawId,
|
||||
])->save();
|
||||
|
||||
$log = JackpotPayoutLog::query()->create([
|
||||
'draw_id' => $drawId,
|
||||
'jackpot_pool_id' => $locked->id,
|
||||
'trigger_type' => 'manual',
|
||||
'total_payout_amount' => $amount,
|
||||
'winner_count' => 0,
|
||||
'trigger_snapshot_json' => [
|
||||
'pool_amount_before' => $poolBefore,
|
||||
'manual' => true,
|
||||
],
|
||||
]);
|
||||
|
||||
return [
|
||||
'current_amount' => (int) $locked->current_amount,
|
||||
'burst_amount' => $amount,
|
||||
'log_id' => (int) $log->id,
|
||||
];
|
||||
});
|
||||
try {
|
||||
$payload = $this->service->execute($pool, (int) $data['draw_id']);
|
||||
} catch (\RuntimeException $e) {
|
||||
return ApiResponse::error(
|
||||
trans('api.jackpot_manual_burst_failed', ['reason' => $e->getMessage()], $request->lotteryLocale()),
|
||||
ErrorCode::ClientHttpError->value,
|
||||
['reason' => $e->getMessage()],
|
||||
409,
|
||||
);
|
||||
}
|
||||
|
||||
return ApiResponse::success($payload);
|
||||
}
|
||||
|
||||
@@ -3,30 +3,46 @@
|
||||
namespace App\Http\Controllers\Api\V1\Admin;
|
||||
|
||||
use App\Models\PlayType;
|
||||
use App\Models\AdminUser;
|
||||
use App\Support\ApiResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Support\AdminConfigPresenter;
|
||||
use App\Services\Config\PlayConfigStreamService;
|
||||
|
||||
/** PATCH /api/v1/admin/play-types/{play_code} — 主目录层开关与展示名(不等同于版本化 items)。 */
|
||||
/**
|
||||
* PATCH /api/v1/admin/play-types/{play_code}
|
||||
*
|
||||
* is_enabled 写入当前生效玩法配置并即时推送大厅;其余字段仅更新主目录 play_types。
|
||||
*/
|
||||
final class PlayTypePatchController extends Controller
|
||||
{
|
||||
public function __construct(
|
||||
private readonly PlayConfigStreamService $playConfig,
|
||||
) {}
|
||||
|
||||
public function __invoke(Request $request, string $play_code): JsonResponse
|
||||
{
|
||||
/** @var AdminUser $admin */
|
||||
$admin = $request->lotteryAdmin();
|
||||
|
||||
/** @var PlayType $type */
|
||||
$type = PlayType::query()->where('play_code', $play_code)->firstOrFail();
|
||||
|
||||
$data = $request->validate([
|
||||
'is_enabled' => ['sometimes', 'boolean'],
|
||||
'sort_order' => ['sometimes', 'integer'],
|
||||
'display_name_zh' => ['sometimes', 'nullable', 'string', 'max:64'],
|
||||
'display_name_en' => ['sometimes', 'nullable', 'string', 'max:64'],
|
||||
'display_name_ne' => ['sometimes', 'nullable', 'string', 'max:64'],
|
||||
'display_name' => ['sometimes', 'nullable', 'string', 'max:64'],
|
||||
'supports_multi_number' => ['sometimes', 'boolean'],
|
||||
'reserved_rule_json' => ['sometimes', 'nullable', 'array'],
|
||||
]);
|
||||
|
||||
if (array_key_exists('is_enabled', $data)) {
|
||||
$this->playConfig->patchActivePlayToggle($admin, $play_code, (bool) $data['is_enabled'], $request);
|
||||
unset($data['is_enabled']);
|
||||
}
|
||||
|
||||
if ($data !== []) {
|
||||
$type->fill($data);
|
||||
$type->save();
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Http\Controllers\Api\V1\Admin\Wallet;
|
||||
|
||||
use App\Models\AdminUser;
|
||||
use App\Support\ApiResponse;
|
||||
use App\Models\TransferOrder;
|
||||
use App\Support\PaginationTrait;
|
||||
@@ -80,8 +81,11 @@ final class TransferOrderListController extends Controller
|
||||
}
|
||||
}
|
||||
|
||||
$admin = $request->lotteryAdmin();
|
||||
$paginator = $query->paginate($perPage, ['*'], 'page', $page);
|
||||
$items = $paginator->getCollection()->map(fn (TransferOrder $o) => $this->formatRow($o));
|
||||
$items = $paginator->getCollection()->map(
|
||||
fn (TransferOrder $o) => $this->formatRow($o, $admin instanceof AdminUser ? $admin : null),
|
||||
);
|
||||
|
||||
return ApiResponse::success([
|
||||
'items' => $items,
|
||||
@@ -94,10 +98,14 @@ final class TransferOrderListController extends Controller
|
||||
/**
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
private function formatRow(TransferOrder $o): array
|
||||
private function formatRow(TransferOrder $o, ?AdminUser $admin): array
|
||||
{
|
||||
$p = $o->player;
|
||||
$amount = (int) $o->amount;
|
||||
$canWriteWallet = $admin !== null && (
|
||||
$admin->hasAdminPermission('prd.wallet_adjust.manage')
|
||||
|| $admin->hasAdminPermission('prd.wallet_reconcile.manage')
|
||||
);
|
||||
|
||||
return [
|
||||
'id' => $o->id,
|
||||
@@ -113,8 +121,8 @@ final class TransferOrderListController extends Controller
|
||||
'amount_formatted' => CurrencyFormatter::fromMinor($amount),
|
||||
'idempotent_key' => $o->idempotent_key,
|
||||
'status' => $o->status,
|
||||
'can_reverse' => $o->status === 'pending_reconcile',
|
||||
'can_manually_process' => in_array($o->status, ['processing', 'failed', 'pending_reconcile'], true),
|
||||
'can_reverse' => $canWriteWallet && $o->status === 'pending_reconcile',
|
||||
'can_manually_process' => $canWriteWallet && in_array($o->status, ['processing', 'failed', 'pending_reconcile'], true),
|
||||
'external_ref_no' => $o->external_ref_no,
|
||||
'external_request_payload' => $o->external_request_payload,
|
||||
'external_response_payload' => $o->external_response_payload,
|
||||
|
||||
143
app/Http/Middleware/RecordAdminApiAudit.php
Normal file
143
app/Http/Middleware/RecordAdminApiAudit.php
Normal file
@@ -0,0 +1,143 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Middleware;
|
||||
|
||||
use Closure;
|
||||
use App\Models\AdminUser;
|
||||
use App\Services\AuditLogger;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
|
||||
/**
|
||||
* 对 admin_api_resources.is_audit_required=true 的变更类请求,在成功响应后写入审计日志。
|
||||
*
|
||||
* 若请求已设置 {@see Request::ATTRIBUTE_AUDIT_RECORDED}(业务层已写更细快照),则跳过。
|
||||
*/
|
||||
final class RecordAdminApiAudit
|
||||
{
|
||||
public const ATTRIBUTE_AUDIT_RECORDED = 'admin_audit_recorded';
|
||||
|
||||
private const MUTATING_METHODS = ['POST', 'PUT', 'PATCH', 'DELETE'];
|
||||
|
||||
public function handle(Request $request, Closure $next): Response
|
||||
{
|
||||
$response = $next($request);
|
||||
|
||||
if (! $this->shouldRecord($request, $response)) {
|
||||
return $response;
|
||||
}
|
||||
|
||||
$admin = $request->user();
|
||||
if (! $admin instanceof AdminUser) {
|
||||
return $response;
|
||||
}
|
||||
|
||||
$resource = $this->resolveResource($request);
|
||||
if ($resource === null || ! (bool) $resource->is_audit_required) {
|
||||
return $response;
|
||||
}
|
||||
|
||||
$targetId = $this->resolveTargetId($request);
|
||||
$actionCode = $this->resolveActionCode((string) $resource->code);
|
||||
|
||||
AuditLogger::recordForAdmin(
|
||||
$admin,
|
||||
$request,
|
||||
moduleCode: (string) $resource->module_code,
|
||||
actionCode: $actionCode,
|
||||
targetType: (string) $resource->code,
|
||||
targetId: $targetId,
|
||||
beforeJson: null,
|
||||
afterJson: [
|
||||
'http_method' => $request->method(),
|
||||
'route_name' => $this->normalizeRouteName((string) ($request->route()?->getName() ?? '')),
|
||||
'status' => $response->getStatusCode(),
|
||||
'payload' => $this->sanitizedPayload($request),
|
||||
],
|
||||
);
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
private function shouldRecord(Request $request, Response $response): bool
|
||||
{
|
||||
if ($request->attributes->get(self::ATTRIBUTE_AUDIT_RECORDED) === true) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (! in_array(strtoupper($request->method()), self::MUTATING_METHODS, true)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$status = $response->getStatusCode();
|
||||
|
||||
return $status >= 200 && $status < 300;
|
||||
}
|
||||
|
||||
private function resolveResource(Request $request): ?object
|
||||
{
|
||||
$routeName = $request->route()?->getName();
|
||||
if (! is_string($routeName) || $routeName === '') {
|
||||
return null;
|
||||
}
|
||||
|
||||
return DB::table('admin_api_resources')
|
||||
->where('route_name', $this->normalizeRouteName($routeName))
|
||||
->where('status', 1)
|
||||
->first(['code', 'module_code', 'is_audit_required']);
|
||||
}
|
||||
|
||||
private function normalizeRouteName(string $routeName): string
|
||||
{
|
||||
return preg_replace('/^(api\.v1\.admin\.)+/', 'api.v1.admin.', $routeName) ?? $routeName;
|
||||
}
|
||||
|
||||
private function resolveActionCode(string $resourceCode): string
|
||||
{
|
||||
$pos = strrpos($resourceCode, '.');
|
||||
if ($pos === false) {
|
||||
return $resourceCode;
|
||||
}
|
||||
|
||||
return substr($resourceCode, $pos + 1);
|
||||
}
|
||||
|
||||
private function resolveTargetId(Request $request): ?string
|
||||
{
|
||||
$route = $request->route();
|
||||
if ($route === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
foreach (['batch', 'draw', 'transfer_no', 'player', 'admin_user', 'admin_role', 'id', 'play_code', 'number_4d', 'key'] as $key) {
|
||||
$value = $route->parameter($key);
|
||||
if ($value === null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (is_object($value) && method_exists($value, 'getKey')) {
|
||||
return (string) $value->getKey();
|
||||
}
|
||||
|
||||
return (string) $value;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, mixed>|null
|
||||
*/
|
||||
private function sanitizedPayload(Request $request): ?array
|
||||
{
|
||||
$data = $request->except([
|
||||
'password',
|
||||
'password_confirmation',
|
||||
'current_password',
|
||||
'token',
|
||||
]);
|
||||
|
||||
return $data === [] ? null : $data;
|
||||
}
|
||||
}
|
||||
45
app/Http/Requests/Admin/DashboardAnalyticsRequest.php
Normal file
45
app/Http/Requests/Admin/DashboardAnalyticsRequest.php
Normal file
@@ -0,0 +1,45 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Requests\Admin;
|
||||
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
use Illuminate\Validation\Rule;
|
||||
|
||||
final class DashboardAnalyticsRequest extends FormRequest
|
||||
{
|
||||
public function authorize(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/** @return array<string, mixed> */
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'period' => ['sometimes', 'string', Rule::in([
|
||||
'today',
|
||||
'last_7_days',
|
||||
'last_30_days',
|
||||
'this_month',
|
||||
'lifetime',
|
||||
'custom',
|
||||
])],
|
||||
'date_from' => ['nullable', 'date_format:Y-m-d', 'required_if:period,custom'],
|
||||
'date_to' => ['nullable', 'date_format:Y-m-d', 'required_if:period,custom', 'after_or_equal:date_from'],
|
||||
'metric' => ['sometimes', 'string', Rule::in(['overview', 'bet', 'payout', 'profit'])],
|
||||
'play_code' => ['nullable', 'string', 'max:64'],
|
||||
];
|
||||
}
|
||||
|
||||
/** @return array<string, mixed> */
|
||||
public function filters(): array
|
||||
{
|
||||
return [
|
||||
'period' => (string) $this->input('period', 'last_7_days'),
|
||||
'date_from' => $this->input('date_from'),
|
||||
'date_to' => $this->input('date_to'),
|
||||
'metric' => (string) $this->input('metric', 'overview'),
|
||||
'play_code' => $this->input('play_code'),
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user