69 lines
2.2 KiB
PHP
69 lines
2.2 KiB
PHP
<?php
|
||
|
||
namespace App\Providers;
|
||
|
||
use App\Models\AdminUser;
|
||
use App\Models\Player;
|
||
use App\Services\Wallet\HttpMainSiteWalletGateway;
|
||
use App\Services\Wallet\MainSiteWalletGateway;
|
||
use App\Services\Wallet\StubMainSiteWalletGateway;
|
||
use Illuminate\Cache\RateLimiting\Limit;
|
||
use Illuminate\Http\Request;
|
||
use Illuminate\Support\Facades\RateLimiter;
|
||
use Illuminate\Support\ServiceProvider;
|
||
|
||
class AppServiceProvider extends ServiceProvider
|
||
{
|
||
/**
|
||
* Register any application services.
|
||
*/
|
||
public function register(): void
|
||
{
|
||
$this->app->singleton(MainSiteWalletGateway::class, function (): MainSiteWalletGateway {
|
||
$url = config('lottery.main_site.wallet_api_url');
|
||
if (! is_string($url) || trim($url) === '') {
|
||
return new StubMainSiteWalletGateway;
|
||
}
|
||
|
||
return new HttpMainSiteWalletGateway;
|
||
});
|
||
}
|
||
|
||
/**
|
||
* 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) {
|
||
return Limit::perMinute(45)->by($request->ip());
|
||
});
|
||
|
||
RateLimiter::for('admin-auth-login', function (Request $request) {
|
||
return Limit::perMinute(15)->by($request->ip());
|
||
});
|
||
}
|
||
}
|