execute(function () use ($domains) { $agent = $this->currentAgent()->load('parent:id,username'); return array_merge([ 'id' => (int) $agent->id, 'username' => $agent->username, 'real_name' => $agent->real_name, 'parent_id' => $agent->parent_id, 'parent_username' => $agent->parent?->username, 'invitation_code' => $agent->invitation_code, 'balance' => (string) $agent->balance, 'frozen_balance' => (string) $agent->frozen_balance, 'deposit_fee_rate' => (string) $agent->deposit_fee_rate, 'withdraw_fee_rate' => (string) $agent->withdraw_fee_rate, 'flow_commission_rate' => (string) $agent->flow_commission_rate, 'profit_commission_rate' => (string) $agent->profit_commission_rate, 'last_login_at' => $agent->last_login_at?->toDateTimeString(), 'last_login_ip' => $agent->last_login_ip, ], $domains->format($agent)); }); } public function update(Request $request) { return $this->execute(function () use ($request) { $data = $request->validate(['real_name' => ['nullable', 'string', 'max:128']]); $agent = $this->currentAgent(); $agent->real_name = trim((string) ($data['real_name'] ?? '')); $agent->save(); return []; }); } public function password(Request $request) { return $this->execute(function () use ($request) { $data = $request->validate([ 'type' => ['required', 'in:login,withdraw'], 'old_password' => ['required', 'string'], 'password' => ['required', 'string', 'min:6', 'max:100', 'confirmed'], ]); DB::transaction(function () use ($data) { $agent = \App\Models\Agent\Agent::query()->lockForUpdate()->findOrFail($this->currentAgent()->id); if (!Hash::check($data['old_password'], (string) $agent->password)) { throw new \InvalidArgumentException('当前登录密码错误'); } if ($data['type'] === 'login') { $agent->password = Hash::make($data['password']); AgentToken::query()->where('agent_id', $agent->id)->delete(); } else { $agent->withdraw_password = Hash::make($data['password']); } $agent->save(); }); return []; }); } public function domains(AgentDomainService $service) { return $this->execute(fn() => $service->format($this->currentAgent())); } 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'], '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'], ]); $page = max(1, (int) ($data['page'] ?? 1)); $limit = min(100, max(1, (int) ($data['limit'] ?? 20))); $query = AgentLoginLog::query()->where('agent_id', $this->currentAgent()->id); if (!empty($data['ip'])) $query->where('ip', $data['ip']); if (!empty($data['device'])) $query->where('device', $data['device']); if (!empty($data['start_date'])) $query->where('login_at', '>=', $data['start_date'] . ' 00:00:00'); if (!empty($data['end_date'])) $query->where('login_at', '<=', $data['end_date'] . ' 23:59:59'); $total = (clone $query)->count(); $list = $query->orderByDesc('id')->forPage($page, $limit)->get(); return compact('total', 'page', 'limit', 'list'); }); } public function dashboard(Request $request, AgentReportService $reports) { return $this->execute(function () use ($request, $reports) { $data = $request->validate([ 'start_date' => ['nullable', 'date_format:Y-m-d'], 'end_date' => ['nullable', 'date_format:Y-m-d', 'after_or_equal:start_date'], ]); $start = $data['start_date'] ?? now()->toDateString(); $end = $data['end_date'] ?? $start; return $reports->summaryForTree($this->currentAgent(), $start, $end); }); } }