| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312 |
- <?php
- namespace App\Http\Controllers\admin;
- use App\Http\Controllers\AgentApiController;
- use App\Models\Agent\Agent as AgentModel;
- use App\Models\Agent\AgentCommission;
- use App\Models\Agent\AgentCommissionRule;
- use App\Models\Agent\AgentLoginLog;
- use App\Models\Agent\AgentWithdrawal;
- use App\Models\EgameItem;
- use App\Models\User;
- use App\Services\Agent\AgentCommissionService;
- use App\Services\Agent\AgentDomainService;
- use App\Services\Agent\AgentReportService;
- use App\Services\Agent\AgentService;
- use App\Services\Agent\AgentWalletService;
- use App\Services\Agent\AgentWithdrawalService;
- use Illuminate\Http\Request;
- use Illuminate\Validation\Rule;
- use Illuminate\Support\Facades\DB;
- use App\Rules\DecimalNumber;
- use App\Constants\Util;
- use App\Services\ThirdGameBalanceService;
- class Agent extends AgentApiController
- {
- public function index(Request $request, AgentDomainService $domains)
- {
- return $this->execute(function () use ($request, $domains) {
- $data = $request->validate([
- 'page' => ['nullable', 'integer', 'min:1'], 'limit' => ['nullable', 'integer', 'min:1', 'max:100'],
- 'username' => ['nullable', 'string', 'max:64'], 'real_name' => ['nullable', 'string', 'max:128'],
- 'parent_id' => ['nullable', 'integer'], 'status' => ['nullable', 'integer', 'in:0,1'],
- 'start_date' => ['nullable', 'date_format:Y-m-d'], 'end_date' => ['nullable', 'date_format:Y-m-d', 'after_or_equal:start_date'],
- ]);
- $page = max(1, (int) ($data['page'] ?? 1));
- $limit = min(100, max(1, (int) ($data['limit'] ?? 20)));
- $query = AgentModel::query()->with(['parent:id,username', 'token:id,agent_id,expires_at,last_used_at'])->withCount(['children', 'members']);
- if (!empty($data['username'])) $query->where('username', 'like', '%' . $data['username'] . '%');
- if (!empty($data['real_name'])) $query->where('real_name', 'like', '%' . $data['real_name'] . '%');
- if (!empty($data['parent_id'])) $query->where('parent_id', $data['parent_id']);
- if (isset($data['status'])) $query->where('status', $data['status']);
- if (!empty($data['start_date'])) $query->where('created_at', '>=', $data['start_date'] . ' 00:00:00');
- if (!empty($data['end_date'])) $query->where('created_at', '<=', $data['end_date'] . ' 23:59:59');
- $total = (clone $query)->count();
- $list = $query->orderByDesc('id')->forPage($page, $limit)->get()->map(fn(AgentModel $agent) => $this->formatAgent($agent, $domains));
- return compact('total', 'page', 'limit', 'list');
- });
- }
- public function show(Request $request, AgentDomainService $domains)
- {
- return $this->execute(function () use ($request, $domains) {
- $data = $request->validate(['id' => ['required', 'integer', 'exists:agents,id']]);
- $agent = AgentModel::query()->with(['parent:id,username', 'token:id,agent_id,expires_at,last_used_at'])->withCount(['children', 'members'])->findOrFail($data['id']);
- return $this->formatAgent($agent, $domains);
- });
- }
- public function store(Request $request, AgentService $service)
- {
- return $this->execute(function () use ($request, $service) {
- $data = $request->validate($this->rules());
- return DB::transaction(function () use ($data, $service) {
- $agent = $service->create($data, (int) request()->user->id);
- if (bccomp((string) ($data['initial_balance'] ?? 0), '0', 4) > 0) {
- app(AgentWalletService::class)->adjust(
- (int) $agent->id,
- (string) $data['initial_balance'],
- 'credit',
- '新增代理初始额度',
- 'agent-initial:' . $agent->id,
- 'admin',
- (int) request()->user->id
- );
- $agent = $agent->fresh();
- }
- return $agent;
- });
- });
- }
- public function update(Request $request, AgentService $service)
- {
- return $this->execute(function () use ($request, $service) {
- $data = $request->validate(array_merge(['id' => ['required', 'integer', 'exists:agents,id']], $this->rules(true)));
- return $service->update(AgentModel::query()->findOrFail($data['id']), $data);
- });
- }
- public function domains(Request $request, AgentDomainService $service)
- {
- return $this->execute(function () use ($request, $service) {
- $data = $request->validate([
- 'id' => ['required', 'integer', 'exists:agents,id'],
- 'pc' => ['nullable', 'string', 'max:255'], 'h5' => ['nullable', 'string', 'max:255'],
- ]);
- return $service->save(AgentModel::query()->findOrFail($data['id']), $data);
- });
- }
- public function adjust(Request $request, AgentWalletService $wallets)
- {
- return $this->execute(function () use ($request, $wallets) {
- $data = $request->validate([
- 'agent_id' => ['required', 'integer', 'exists:agents,id'], 'type' => ['required', 'in:credit,debit'],
- 'amount' => ['required', 'numeric', 'min:0.0001', new DecimalNumber()], 'remark' => ['required', 'string', 'max:500'],
- 'request_id' => ['required', 'string', 'max:64'],
- ]);
- return $wallets->adjust(
- (int) $data['agent_id'], (string) $data['amount'], $data['type'], $data['remark'],
- 'admin:' . request()->user->id . ':' . $data['request_id'], 'admin', (int) request()->user->id
- );
- });
- }
- public function quota(Request $request, ThirdGameBalanceService $thirdGame)
- {
- return $this->execute(function () use ($request, $thirdGame) {
- $data = $request->validate([
- 'agent_id' => ['required', 'integer', 'exists:agents,id'],
- 'refresh' => ['nullable', 'boolean'],
- ]);
- $agent = AgentModel::query()->findOrFail($data['agent_id']);
- $provider = $thirdGame->agentBalances((int) $agent->id, $request->boolean('refresh'));
- if (empty($provider['ok'])) {
- throw new \RuntimeException((string) ($provider['msg'] ?? '三方平台余额查询失败'));
- }
- $balances = is_array($provider['balances'] ?? null) ? $provider['balances'] : [];
- $platforms = EgameItem::query()->where('item_type', EgameItem::TYPE_PLATFORM)->where('game_type', 0)
- ->where('status', EgameItem::STATUS_ENABLED)->orderByDesc('sort')->get(['plat_type', 'name', 'logo'])
- ->map(function (EgameItem $platform) use ($balances) {
- $key = strtolower((string) $platform->plat_type);
- $platform->logo = Util::ensureUrl($platform->logo);
- $platform->setAttribute('balance', $balances[$key] ?? '0.0000000000');
- $platform->setAttribute('account_opened', array_key_exists($key, $balances) ? 1 : 0);
- return $platform;
- });
- return [
- 'agent_id' => (int) $agent->id,
- 'player_id' => (string) $provider['player_id'],
- 'currency' => (string) $provider['currency'],
- 'balance' => (string) $agent->balance,
- 'frozen_balance' => (string) $agent->frozen_balance,
- 'quota_mode' => 'provider',
- 'provider_wallet_supported' => true,
- 'platforms' => $platforms,
- ];
- });
- }
- public function bindMember(Request $request)
- {
- return $this->execute(function () use ($request) {
- $data = $request->validate([
- 'user_id' => ['required', 'integer', 'exists:users,id'],
- 'agent_id' => ['nullable', 'integer', 'exists:agents,id'],
- ]);
- $user = User::query()->findOrFail($data['user_id']);
- $user->agent_id = $data['agent_id'] ?? null;
- $user->save();
- return ['user_id' => (int) $user->id, 'agent_id' => $user->agent_id === null ? null : (int) $user->agent_id];
- });
- }
- public function loginLogs(Request $request)
- {
- return $this->execute(function () use ($request) {
- $data = $request->validate([
- 'page' => ['nullable', 'integer', 'min:1'], 'limit' => ['nullable', 'integer', 'min:1', 'max:100'],
- 'agent_id' => ['nullable', 'integer'], 'username' => ['nullable', 'string', 'max:64'],
- 'ip' => ['nullable', 'string', 'max:64'], 'device' => ['nullable', 'in:mobile,pc'],
- 'start_date' => ['nullable', 'date_format:Y-m-d'], 'end_date' => ['nullable', 'date_format:Y-m-d', 'after_or_equal:start_date'],
- ]);
- $query = AgentLoginLog::query();
- foreach (['agent_id', 'username', 'ip', 'device'] as $field) if (!empty($data[$field])) $query->where($field, $data[$field]);
- return $this->paginate($query, $data, 'login_at');
- });
- }
- public function withdrawals(Request $request)
- {
- return $this->execute(function () use ($request) {
- $data = $request->validate([
- 'page' => ['nullable', 'integer', 'min:1'], 'limit' => ['nullable', 'integer', 'min:1', 'max:100'],
- 'agent_id' => ['nullable', 'integer'], 'order_no' => ['nullable', 'string', 'max:40'],
- 'status' => ['nullable', 'in:pending,approved,rejected,paid'],
- 'start_date' => ['nullable', 'date_format:Y-m-d'], 'end_date' => ['nullable', 'date_format:Y-m-d', 'after_or_equal:start_date'],
- ]);
- $query = AgentWithdrawal::query()->with('agent:id,username,real_name');
- foreach (['agent_id', 'order_no', 'status'] as $field) if (!empty($data[$field])) $query->where($field, $data[$field]);
- return $this->paginate($query, $data);
- });
- }
- public function auditWithdrawal(Request $request, AgentWithdrawalService $service)
- {
- return $this->execute(function () use ($request, $service) {
- $data = $request->validate([
- 'id' => ['required', 'integer', 'exists:agent_withdrawals,id'],
- 'action' => ['required', 'in:approve,reject,paid'], 'remark' => ['nullable', 'string', 'max:500'],
- ]);
- return $service->audit((int) $data['id'], $data['action'], (int) request()->user->id, (string) ($data['remark'] ?? ''));
- });
- }
- public function reports(Request $request, AgentReportService $reports)
- {
- return $this->execute(function () use ($request, $reports) {
- $data = $request->validate([
- 'page' => ['nullable', 'integer', 'min:1'], 'limit' => ['nullable', 'integer', 'min:1', 'max:20'],
- 'username' => ['nullable', 'string', 'max:64'], 'start_date' => ['required', 'date_format:Y-m-d'],
- 'end_date' => ['required', 'date_format:Y-m-d', 'after_or_equal:start_date'],
- ]);
- return $reports->reportRows(null, $data);
- });
- }
- public function commissionRules(Request $request)
- {
- return $this->execute(function () use ($request) {
- $data = $request->validate(['agent_id' => ['nullable', 'integer']]);
- return AgentCommissionRule::query()->with('agent:id,username')->when(!empty($data['agent_id']), fn($q) => $q->where('agent_id', $data['agent_id']))
- ->orderByDesc('effective_from')->get();
- });
- }
- public function saveFlowCommissionRule(Request $request, AgentCommissionService $commissions)
- {
- return $this->execute(function () use ($request, $commissions) {
- $data = $request->validate([
- 'agent_id' => ['required', 'integer', 'exists:agents,id'],
- 'flow_rate' => ['required', 'numeric', 'between:0,100', new DecimalNumber(3, 4)],
- 'effective_from' => ['required', 'date_format:Y-m-d', 'date_equals:' . now()->toDateString()],
- ]);
- return $commissions->saveFlowRate(AgentModel::query()->findOrFail($data['agent_id']), (string) $data['flow_rate'], $data['effective_from'], (int) request()->user->id);
- });
- }
- public function saveProfitCommissionRule(Request $request, AgentCommissionService $commissions)
- {
- return $this->execute(function () use ($request, $commissions) {
- $data = $request->validate([
- 'agent_id' => ['required', 'integer', 'exists:agents,id'],
- 'profit_rate' => ['required', 'numeric', 'between:0,100', new DecimalNumber(3, 4)],
- 'effective_from' => ['required', 'date_format:Y-m-d', 'date_equals:' . now()->toDateString()],
- ]);
- return $commissions->saveProfitRate(AgentModel::query()->findOrFail($data['agent_id']), (string) $data['profit_rate'], $data['effective_from'], (int) request()->user->id);
- });
- }
- public function commissions(Request $request)
- {
- return $this->execute(function () use ($request) {
- $data = $request->validate([
- 'page' => ['nullable', 'integer', 'min:1'], 'limit' => ['nullable', 'integer', 'min:1', 'max:100'],
- 'agent_id' => ['nullable', 'integer'], 'type' => ['nullable', 'in:flow,profit'], 'status' => ['nullable', 'in:pending,credited'],
- 'start_date' => ['nullable', 'date_format:Y-m-d'], 'end_date' => ['nullable', 'date_format:Y-m-d', 'after_or_equal:start_date'],
- ]);
- $query = AgentCommission::query()->with('agent:id,username');
- foreach (['agent_id', 'type', 'status'] as $field) if (!empty($data[$field])) $query->where($field, $data[$field]);
- if (!empty($data['start_date'])) $query->where('settlement_date', '>=', $data['start_date']);
- if (!empty($data['end_date'])) $query->where('settlement_date', '<=', $data['end_date']);
- return $this->paginate($query, $data, 'settlement_date', false);
- });
- }
- private function rules(bool $update = false): array
- {
- return [
- 'username' => $update
- ? ['sometimes', 'string', 'min:4', 'max:64', 'alpha_dash', Rule::unique('agents', 'username')->ignore((int) request()->input('id'))]
- : ['required', 'string', 'min:4', 'max:64', 'alpha_dash', 'unique:agents,username'],
- 'password' => [$update ? 'nullable' : 'required', 'string', 'min:6', 'max:100'],
- 'withdraw_password' => ['nullable', 'string', 'min:6', 'max:100'], 'real_name' => ['nullable', 'string', 'max:128'],
- 'parent_id' => ['nullable', 'integer', 'exists:agents,id'], 'deposit_fee_rate' => ['nullable', 'numeric', 'between:0,100', new DecimalNumber(3, 4)],
- 'withdraw_fee_rate' => ['nullable', 'numeric', 'between:0,100', new DecimalNumber(3, 4)],
- 'flow_commission_rate' => $update ? ['prohibited'] : ['nullable', 'numeric', 'between:0,100', new DecimalNumber(3, 4)],
- 'profit_commission_rate' => $update ? ['prohibited'] : ['nullable', 'numeric', 'between:0,100', new DecimalNumber(3, 4)],
- 'status' => ['nullable', 'integer', 'in:0,1'],
- 'initial_balance' => $update ? ['prohibited'] : ['nullable', 'numeric', 'min:0', new DecimalNumber()],
- ];
- }
- private function paginate($query, array $data, string $orderBy = 'id', bool $filterCreatedAt = true): array
- {
- $page = max(1, (int) ($data['page'] ?? 1));
- $limit = min(100, max(1, (int) ($data['limit'] ?? 20)));
- if ($filterCreatedAt && !empty($data['start_date'])) $query->where('created_at', '>=', $data['start_date'] . ' 00:00:00');
- if ($filterCreatedAt && !empty($data['end_date'])) $query->where('created_at', '<=', $data['end_date'] . ' 23:59:59');
- $total = (clone $query)->count();
- $list = $query->orderByDesc($orderBy)->orderByDesc('id')->forPage($page, $limit)->get();
- return compact('total', 'page', 'limit', 'list');
- }
- private function formatAgent(AgentModel $agent, AgentDomainService $domains): array
- {
- $token = $agent->token;
- $online = $token
- && $token->expires_at->isFuture()
- && $token->last_used_at
- && $token->last_used_at->gte(now()->subMinutes(config('agent.online_minutes', 5)));
- $data = $agent->toArray();
- unset($data['token']);
- return array_merge($data, $domains->format($agent), [
- 'login_status' => $online ? 1 : 0,
- 'login_status_text' => $online ? '在线' : '离线',
- 'last_active_at' => $token?->last_used_at?->toDateTimeString(),
- ]);
- }
- }
|