| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113 |
- <?php
- namespace App\Http\Controllers\agent;
- use App\Http\Controllers\AgentApiController;
- use App\Models\Agent\AgentLoginLog;
- use App\Services\Agent\AgentDomainService;
- use App\Services\Agent\AgentReportService;
- use Illuminate\Http\Request;
- use Illuminate\Support\Facades\Hash;
- use App\Models\Agent\AgentToken;
- use Illuminate\Support\Facades\DB;
- class ProfileController extends AgentApiController
- {
- public function me(AgentDomainService $domains)
- {
- return $this->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);
- });
- }
- }
|