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) { return $this->execute(function () use ($request) { $data = $request->validate(['agent_id' => ['required', 'integer', 'exists:agents,id']]); $agent = AgentModel::query()->findOrFail($data['agent_id']); $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) { $platform->logo = Util::ensureUrl($platform->logo); return $platform; }); return [ 'balance' => (string) $agent->balance, 'frozen_balance' => (string) $agent->frozen_balance, 'quota_mode' => 'internal', 'provider_wallet_supported' => false, '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(), ]); } }