Files
lotteryLaravel/app/Providers/AppServiceProvider.php
kang 6ec9634704
Some checks failed
lotterLaravel CI / test (push) Has been cancelled
feat: 添加E2E测试环境配置支持
添加LOTTERY_E2E环境变量来控制E2E测试相关功能,
包括绕过验证码、登录限制和钱包API URL验证,
同时更新composer.json以包含E2E专用的提供者和服务。
2026-06-18 14:42:22 +08:00

86 lines
2.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\Providers;
use App\Models\Player;
use App\Models\AdminUser;
use Illuminate\Http\Request;
use Illuminate\Support\ServiceProvider;
use Illuminate\Cache\RateLimiting\Limit;
use Illuminate\Support\Facades\RateLimiter;
use App\Services\Wallet\MainSiteWalletGateway;
use App\Services\Wallet\HttpMainSiteWalletGateway;
use App\Services\Wallet\StubMainSiteWalletGateway;
final class AppServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*/
public function register(): void
{
$this->app->singleton(MainSiteWalletGateway::class, HttpMainSiteWalletGateway::class);
}
/**
* Bootstrap any application services.
*/
public function boot(): void
{
// 仅在通过 EnsurePlayerApi 后可用;未走中间件时为 null
Request::macro('lotteryPlayer', function (): ?Player {
/** @var Request $this */
return $this->attributes->get('lottery_player');
});
/**
* 【当前请求语言】zh / en / ne与 lottery.locales.supported 对齐。
* 由 NegotiateLotteryLocale 写入 attribute控制台等非 HTTP 或无该中间件时回落到 fallback。
*/
Request::macro('lotteryLocale', function (): string {
/** @var Request $this */
return (string) ($this->attributes->get('lottery_locale')
?? config('lottery.locales.fallback', 'en'));
});
Request::macro('lotteryAdmin', function (): ?AdminUser {
/** @var Request $this */
$v = $this->attributes->get('lottery_admin');
return $v instanceof AdminUser ? $v : null;
});
RateLimiter::for('admin-auth-captcha', function (Request $request) {
if ((bool) env('LOTTERY_E2E', false)) {
return Limit::none();
}
return Limit::perMinute(45)->by($request->ip());
});
RateLimiter::for('admin-auth-login', function (Request $request) {
if ((bool) env('LOTTERY_E2E', false)) {
return Limit::none();
}
return Limit::perMinute(15)->by($request->ip());
});
RateLimiter::for('player-auth-captcha', function (Request $request) {
if ((bool) env('LOTTERY_E2E', false)) {
return Limit::none();
}
return Limit::perMinute(45)->by($request->ip());
});
RateLimiter::for('player-auth-login', function (Request $request) {
if ((bool) env('LOTTERY_E2E', false)) {
return Limit::none();
}
return Limit::perMinute(15)->by($request->ip());
});
}
}