feat: 增强代理和玩家管理功能
- 在多个控制器中更新权限检查逻辑,确保管理员能够更灵活地管理代理和玩家。 - 在 AdminPlayerStoreController 中引入对玩家创建能力的验证,确保只有具备相应权限的管理员能够创建玩家。 - 更新请求验证逻辑,新增 credit_limit、rebate_rate 和 extra_rebate_rate 字段,以支持更细粒度的玩家管理。 - 在 AgentNodeProfileController 中添加对父代理能力授予的验证,确保子代理的权限在父代理范围内。 - 引入 AgentProfileFieldRules 以简化代理资料更新请求的规则定义,提升代码复用性。
This commit is contained in:
@@ -3,6 +3,9 @@
|
||||
namespace App\Services\Player;
|
||||
|
||||
use App\Models\Player;
|
||||
use App\Support\AgentOverdueGuard;
|
||||
use App\Support\CreditAmountScale;
|
||||
use App\Support\PlayerFundingMode;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
|
||||
@@ -14,19 +17,33 @@ final class PlayerCreditService
|
||||
public function upsertAccount(Player $player, array $payload): void
|
||||
{
|
||||
$limit = max(0, (int) ($payload['credit_limit'] ?? 0));
|
||||
$now = now();
|
||||
$exists = DB::table('player_credit_accounts')
|
||||
->where('player_id', $player->id)
|
||||
->exists();
|
||||
|
||||
DB::table('player_credit_accounts')->updateOrInsert(
|
||||
['player_id' => $player->id],
|
||||
[
|
||||
'credit_limit' => $limit,
|
||||
'used_credit' => DB::raw('COALESCE(used_credit, 0)'),
|
||||
'frozen_credit' => DB::raw('COALESCE(frozen_credit, 0)'),
|
||||
'updated_at' => now(),
|
||||
'created_at' => now(),
|
||||
],
|
||||
);
|
||||
if ($exists) {
|
||||
DB::table('player_credit_accounts')
|
||||
->where('player_id', $player->id)
|
||||
->update([
|
||||
'credit_limit' => $limit,
|
||||
'updated_at' => $now,
|
||||
]);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
DB::table('player_credit_accounts')->insert([
|
||||
'player_id' => $player->id,
|
||||
'credit_limit' => $limit,
|
||||
'used_credit' => 0,
|
||||
'frozen_credit' => 0,
|
||||
'created_at' => $now,
|
||||
'updated_at' => $now,
|
||||
]);
|
||||
}
|
||||
|
||||
/** 可用授信(主货币整数,与后台「授信额度」一致)。 */
|
||||
public function availableCredit(Player $player): int
|
||||
{
|
||||
$row = DB::table('player_credit_accounts')->where('player_id', $player->id)->first();
|
||||
@@ -37,34 +54,45 @@ final class PlayerCreditService
|
||||
return max(0, (int) $row->credit_limit - (int) $row->used_credit - (int) $row->frozen_credit);
|
||||
}
|
||||
|
||||
public function holdForBet(Player $player, int $amount): void
|
||||
/** 可用授信(最小货币单位,供玩家端钱包/下注与钱包余额 API 对齐)。 */
|
||||
public function availableCreditMinor(Player $player, ?string $currencyCode = null): int
|
||||
{
|
||||
if ($amount <= 0) {
|
||||
$currency = $currencyCode ?? (string) $player->default_currency;
|
||||
|
||||
return CreditAmountScale::majorToMinor($this->availableCredit($player), $currency);
|
||||
}
|
||||
|
||||
public function holdForBet(Player $player, int $amountMinor): void
|
||||
{
|
||||
if ($amountMinor <= 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (! \App\Support\CreditLineMode::isEnabledForSiteCode((string) $player->site_code)) {
|
||||
if (! PlayerFundingMode::usesCredit($player)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$available = $this->availableCredit($player);
|
||||
if ($amount > $available) {
|
||||
$currency = (string) $player->default_currency;
|
||||
$availableMinor = $this->availableCreditMinor($player, $currency);
|
||||
if ($amountMinor > $availableMinor) {
|
||||
throw ValidationException::withMessages([
|
||||
'credit' => ['insufficient'],
|
||||
]);
|
||||
}
|
||||
|
||||
$majorDelta = CreditAmountScale::minorToMajor($amountMinor, $currency);
|
||||
|
||||
DB::table('player_credit_accounts')
|
||||
->where('player_id', $player->id)
|
||||
->update([
|
||||
'used_credit' => DB::raw('used_credit + '.$amount),
|
||||
'used_credit' => DB::raw('used_credit + '.$majorDelta),
|
||||
'updated_at' => now(),
|
||||
]);
|
||||
|
||||
DB::table('credit_ledger')->insert([
|
||||
'owner_type' => 'player',
|
||||
'owner_id' => $player->id,
|
||||
'amount' => -$amount,
|
||||
'amount' => -$amountMinor,
|
||||
'reason' => 'bet_hold',
|
||||
'ref_type' => 'bet',
|
||||
'ref_id' => null,
|
||||
@@ -73,23 +101,97 @@ final class PlayerCreditService
|
||||
]);
|
||||
}
|
||||
|
||||
public function releaseFromSettlement(Player $player, int $amount, int $billId): void
|
||||
public function applySettledLoss(Player $player, int $amountMinor, int $ticketItemId): void
|
||||
{
|
||||
if ($amount <= 0) {
|
||||
if ($amountMinor <= 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (! PlayerFundingMode::usesCredit($player)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$currency = (string) $player->default_currency;
|
||||
$majorDelta = CreditAmountScale::minorToMajor($amountMinor, $currency);
|
||||
|
||||
DB::table('player_credit_accounts')
|
||||
->where('player_id', $player->id)
|
||||
->update([
|
||||
'used_credit' => DB::raw('GREATEST(0, used_credit - '.$amount.')'),
|
||||
'used_credit' => DB::raw('used_credit + '.$majorDelta),
|
||||
'updated_at' => now(),
|
||||
]);
|
||||
|
||||
DB::table('credit_ledger')->insert([
|
||||
'owner_type' => 'player',
|
||||
'owner_id' => $player->id,
|
||||
'amount' => $amount,
|
||||
'amount' => -$amountMinor,
|
||||
'reason' => 'game_settlement_loss',
|
||||
'ref_type' => 'ticket_item',
|
||||
'ref_id' => $ticketItemId,
|
||||
'created_at' => now(),
|
||||
'updated_at' => now(),
|
||||
]);
|
||||
}
|
||||
|
||||
public function assertMayPlaceBet(Player $player, int $amountMinor): void
|
||||
{
|
||||
if (! PlayerFundingMode::usesCredit($player)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$overdue = DB::table('settlement_bills')
|
||||
->where('owner_type', 'player')
|
||||
->where('owner_id', $player->id)
|
||||
->where('status', 'overdue')
|
||||
->where('unpaid_amount', '>', 0)
|
||||
->exists();
|
||||
|
||||
if ($overdue) {
|
||||
throw ValidationException::withMessages([
|
||||
'credit' => ['overdue'],
|
||||
]);
|
||||
}
|
||||
|
||||
$agentNodeId = (int) ($player->agent_node_id ?? 0);
|
||||
if ($agentNodeId > 0) {
|
||||
AgentOverdueGuard::assertAgentMayGrantCredit($agentNodeId);
|
||||
}
|
||||
|
||||
$this->holdForBet($player, $amountMinor);
|
||||
}
|
||||
|
||||
public function releaseBetHold(Player $player, int $amountMinor, int $ticketItemId): void
|
||||
{
|
||||
if ($amountMinor <= 0 || ! PlayerFundingMode::usesCredit($player)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->decreaseUsedCredit($player, $amountMinor);
|
||||
|
||||
DB::table('credit_ledger')->insert([
|
||||
'owner_type' => 'player',
|
||||
'owner_id' => $player->id,
|
||||
'amount' => $amountMinor,
|
||||
'reason' => 'bet_hold_release',
|
||||
'ref_type' => 'ticket_item',
|
||||
'ref_id' => $ticketItemId,
|
||||
'created_at' => now(),
|
||||
'updated_at' => now(),
|
||||
]);
|
||||
}
|
||||
|
||||
public function releaseFromSettlement(Player $player, int $amountMinor, int $billId): void
|
||||
{
|
||||
if ($amountMinor <= 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->decreaseUsedCredit($player, $amountMinor);
|
||||
|
||||
DB::table('credit_ledger')->insert([
|
||||
'owner_type' => 'player',
|
||||
'owner_id' => $player->id,
|
||||
'amount' => $amountMinor,
|
||||
'reason' => 'settlement_confirm',
|
||||
'ref_type' => 'settlement_bill',
|
||||
'ref_id' => $billId,
|
||||
@@ -97,4 +199,26 @@ final class PlayerCreditService
|
||||
'updated_at' => now(),
|
||||
]);
|
||||
}
|
||||
|
||||
private function decreaseUsedCredit(Player $player, int $amountMinor): void
|
||||
{
|
||||
if ($amountMinor <= 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
$playerId = (int) $player->id;
|
||||
$row = DB::table('player_credit_accounts')->where('player_id', $playerId)->first();
|
||||
if ($row === null) {
|
||||
return;
|
||||
}
|
||||
|
||||
$majorDelta = CreditAmountScale::minorToMajor($amountMinor, (string) $player->default_currency);
|
||||
$next = max(0, (int) $row->used_credit - $majorDelta);
|
||||
DB::table('player_credit_accounts')
|
||||
->where('player_id', $playerId)
|
||||
->update([
|
||||
'used_credit' => $next,
|
||||
'updated_at' => now(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
131
app/Services/Player/PlayerNativeAuthService.php
Normal file
131
app/Services/Player/PlayerNativeAuthService.php
Normal file
@@ -0,0 +1,131 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Player;
|
||||
|
||||
use App\Lottery\ErrorCode;
|
||||
use App\Models\Player;
|
||||
use App\Support\PlayerAuthSource;
|
||||
use Firebase\JWT\JWT;
|
||||
use Illuminate\Support\Facades\Hash;
|
||||
use App\Exceptions\PlayerAuthenticationException;
|
||||
|
||||
final class PlayerNativeAuthService
|
||||
{
|
||||
/**
|
||||
* @return array{access_token: string, expires_in: int, token_type: string, player: array<string, mixed>}
|
||||
*/
|
||||
public function login(string $siteCode, string $username, string $password): array
|
||||
{
|
||||
$username = trim($username);
|
||||
$siteCode = trim($siteCode);
|
||||
if ($siteCode === '' || $username === '' || $password === '') {
|
||||
throw new PlayerAuthenticationException(
|
||||
'账号或密码错误',
|
||||
ErrorCode::PlayerCredentialsInvalid->value,
|
||||
);
|
||||
}
|
||||
|
||||
$player = Player::query()
|
||||
->where('site_code', $siteCode)
|
||||
->where('username', $username)
|
||||
->where('auth_source', PlayerAuthSource::LOTTERY_NATIVE)
|
||||
->first();
|
||||
|
||||
if ($player === null || ! is_string($player->password_hash) || $player->password_hash === '') {
|
||||
throw new PlayerAuthenticationException(
|
||||
'账号或密码错误',
|
||||
ErrorCode::PlayerCredentialsInvalid->value,
|
||||
);
|
||||
}
|
||||
|
||||
if ($player->login_locked_until !== null && $player->login_locked_until->isFuture()) {
|
||||
throw new PlayerAuthenticationException(
|
||||
'登录已锁定',
|
||||
ErrorCode::PlayerLoginLocked->value,
|
||||
403,
|
||||
);
|
||||
}
|
||||
|
||||
if ((int) $player->status !== 0) {
|
||||
throw new PlayerAuthenticationException(
|
||||
'账号已冻结',
|
||||
ErrorCode::PlayerAccountSuspended->value,
|
||||
403,
|
||||
);
|
||||
}
|
||||
|
||||
if (! Hash::check($password, $player->password_hash)) {
|
||||
$this->recordFailedLogin($player);
|
||||
|
||||
throw new PlayerAuthenticationException(
|
||||
'账号或密码错误',
|
||||
ErrorCode::PlayerCredentialsInvalid->value,
|
||||
);
|
||||
}
|
||||
|
||||
$player->forceFill([
|
||||
'login_failed_count' => 0,
|
||||
'login_locked_until' => null,
|
||||
'last_login_at' => now(),
|
||||
])->save();
|
||||
|
||||
$ttl = (int) config('lottery.player_auth.native.ttl_seconds', 28800);
|
||||
$token = $this->issueToken($player, $ttl);
|
||||
|
||||
return [
|
||||
'access_token' => $token,
|
||||
'expires_in' => $ttl,
|
||||
'token_type' => 'Bearer',
|
||||
'player' => [
|
||||
'id' => (int) $player->id,
|
||||
'site_code' => $player->site_code,
|
||||
'username' => $player->username,
|
||||
'nickname' => $player->nickname,
|
||||
'funding_mode' => $player->funding_mode,
|
||||
'auth_source' => $player->auth_source,
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
public function issueToken(Player $player, ?int $ttlSeconds = null): string
|
||||
{
|
||||
$secret = (string) config('lottery.player_auth.native.secret', '');
|
||||
if ($secret === '') {
|
||||
throw new PlayerAuthenticationException(
|
||||
'原生登录未配置',
|
||||
ErrorCode::PlayerSsoSecretNotConfigured->value,
|
||||
503,
|
||||
);
|
||||
}
|
||||
|
||||
$ttl = $ttlSeconds ?? (int) config('lottery.player_auth.native.ttl_seconds', 28800);
|
||||
$now = time();
|
||||
$playerIdKey = (string) config('lottery.player_auth.native.claim_player_id', 'player_id');
|
||||
$authKey = (string) config('lottery.player_auth.native.claim_auth_source', 'auth_source');
|
||||
|
||||
$payload = [
|
||||
$playerIdKey => (int) $player->id,
|
||||
$authKey => PlayerAuthSource::LOTTERY_NATIVE,
|
||||
'site_code' => (string) $player->site_code,
|
||||
'iat' => $now,
|
||||
'exp' => $now + $ttl,
|
||||
];
|
||||
|
||||
return JWT::encode($payload, $secret, (string) config('lottery.player_auth.jwt.algorithm', 'HS256'));
|
||||
}
|
||||
|
||||
private function recordFailedLogin(Player $player): void
|
||||
{
|
||||
$max = (int) config('lottery.player_auth.native.max_login_attempts', 8);
|
||||
$lockMinutes = (int) config('lottery.player_auth.native.lock_minutes', 15);
|
||||
$count = (int) $player->login_failed_count + 1;
|
||||
|
||||
$updates = ['login_failed_count' => $count];
|
||||
if ($count >= $max) {
|
||||
$updates['login_locked_until'] = now()->addMinutes($lockMinutes);
|
||||
$updates['login_failed_count'] = 0;
|
||||
}
|
||||
|
||||
$player->forceFill($updates)->save();
|
||||
}
|
||||
}
|
||||
65
app/Services/Player/PlayerRebateProfileService.php
Normal file
65
app/Services/Player/PlayerRebateProfileService.php
Normal file
@@ -0,0 +1,65 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Player;
|
||||
|
||||
use App\Models\AgentNode;
|
||||
use App\Services\Agent\RebateLimitValidator;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
final class PlayerRebateProfileService
|
||||
{
|
||||
public function __construct(
|
||||
private readonly RebateLimitValidator $rebateLimitValidator,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* @param list<array{game_type: string, rebate_rate?: float, extra_rebate_rate?: float, inherit_from_agent?: bool}> $profiles
|
||||
*/
|
||||
public function syncProfiles(int $playerId, AgentNode $agent, array $profiles): void
|
||||
{
|
||||
if ($profiles === []) {
|
||||
return;
|
||||
}
|
||||
|
||||
$now = now();
|
||||
foreach ($profiles as $row) {
|
||||
$gameType = trim((string) ($row['game_type'] ?? '*')) ?: '*';
|
||||
$inherit = (bool) ($row['inherit_from_agent'] ?? false);
|
||||
$rebateRate = (float) ($row['rebate_rate'] ?? 0);
|
||||
$extraRate = (float) ($row['extra_rebate_rate'] ?? 0);
|
||||
|
||||
if (! $inherit) {
|
||||
$this->rebateLimitValidator->assertPlayerRebateWithinAgent($agent, $rebateRate, $extraRate);
|
||||
}
|
||||
|
||||
DB::table('player_rebate_profiles')->updateOrInsert(
|
||||
['player_id' => $playerId, 'game_type' => $gameType],
|
||||
[
|
||||
'inherit_from_agent' => $inherit,
|
||||
'rebate_rate' => $inherit ? 0 : $rebateRate,
|
||||
'extra_rebate_rate' => $inherit ? 0 : $extraRate,
|
||||
'updated_at' => $now,
|
||||
'created_at' => $now,
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return list<array{game_type: string, rebate_rate: float, extra_rebate_rate: float, inherit_from_agent: bool}>
|
||||
*/
|
||||
public function listForPlayer(int $playerId): array
|
||||
{
|
||||
return DB::table('player_rebate_profiles')
|
||||
->where('player_id', $playerId)
|
||||
->orderBy('game_type')
|
||||
->get()
|
||||
->map(static fn (object $row): array => [
|
||||
'game_type' => (string) $row->game_type,
|
||||
'rebate_rate' => (float) $row->rebate_rate,
|
||||
'extra_rebate_rate' => (float) $row->extra_rebate_rate,
|
||||
'inherit_from_agent' => (bool) $row->inherit_from_agent,
|
||||
])
|
||||
->all();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user