ProfileController.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. namespace App\Http\Controllers\agent;
  3. use App\Http\Controllers\AgentApiController;
  4. use App\Models\Agent\AgentLoginLog;
  5. use App\Services\Agent\AgentDomainService;
  6. use App\Services\Agent\AgentEquityAllocation;
  7. use App\Services\Agent\AgentPeriodReportService;
  8. use App\Services\Agent\AgentReportService;
  9. use Carbon\Carbon;
  10. use Illuminate\Http\Request;
  11. use Illuminate\Support\Facades\Hash;
  12. use App\Models\Agent\AgentToken;
  13. use Illuminate\Support\Facades\DB;
  14. class ProfileController extends AgentApiController
  15. {
  16. public function me(\App\Services\Agent\AgentProfileService $profiles)
  17. {
  18. return $this->execute(function () use ($profiles) {
  19. $agent = $this->currentAgent();
  20. return $profiles->summary($agent) + [
  21. 'last_login_at' => $agent->last_login_at?->toDateTimeString(),
  22. 'last_login_ip' => $agent->last_login_ip,
  23. ];
  24. });
  25. }
  26. public function update(Request $request)
  27. {
  28. return $this->execute(function () use ($request) {
  29. $data = $request->validate(['real_name' => ['nullable', 'string', 'max:128'], 'language' => ['sometimes', 'required', 'in:zh,en,vi']]);
  30. $agent = $this->currentAgent();
  31. if (array_key_exists('real_name', $data)) $agent->real_name = trim((string) ($data['real_name'] ?? ''));
  32. if (array_key_exists('language', $data)) $agent->language = $data['language'];
  33. $agent->save();
  34. return [];
  35. });
  36. }
  37. public function password(Request $request)
  38. {
  39. return $this->execute(function () use ($request) {
  40. $data = $request->validate([
  41. 'type' => ['required', 'in:login,withdraw'],
  42. 'old_password' => ['required', 'string'],
  43. 'password' => ['required', 'string', 'min:6', 'max:100', 'confirmed'],
  44. ]);
  45. DB::transaction(function () use ($data) {
  46. $agent = \App\Models\Agent\Agent::query()->lockForUpdate()->findOrFail($this->currentAgent()->id);
  47. if (!Hash::check($data['old_password'], (string) $agent->password)) {
  48. throw new \InvalidArgumentException('当前登录密码错误');
  49. }
  50. if ($data['type'] === 'login') {
  51. $agent->password = Hash::make($data['password']);
  52. AgentToken::query()->where('agent_id', $agent->id)->delete();
  53. } else {
  54. $agent->withdraw_password = Hash::make($data['password']);
  55. }
  56. $agent->save();
  57. });
  58. return [];
  59. });
  60. }
  61. public function domains(AgentDomainService $service)
  62. {
  63. return $this->execute(fn() => $service->format($this->currentAgent()));
  64. }
  65. public function loginLogs(Request $request)
  66. {
  67. return $this->execute(function () use ($request) {
  68. $data = $request->validate([
  69. 'page' => ['nullable', 'integer', 'min:1'], 'limit' => ['nullable', 'integer', 'min:1', 'max:100'],
  70. 'ip' => ['nullable', 'string', 'max:64'], 'device' => ['nullable', 'in:mobile,pc'],
  71. 'start_date' => ['nullable', 'date_format:Y-m-d'], 'end_date' => ['nullable', 'date_format:Y-m-d', 'after_or_equal:start_date'],
  72. ]);
  73. $page = max(1, (int) ($data['page'] ?? 1));
  74. $limit = min(100, max(1, (int) ($data['limit'] ?? 20)));
  75. $query = AgentLoginLog::query()->where('agent_id', $this->currentAgent()->id);
  76. if (!empty($data['ip'])) $query->where('ip', $data['ip']);
  77. if (!empty($data['device'])) $query->where('device', $data['device']);
  78. if (!empty($data['start_date'])) $query->where('login_at', '>=', $data['start_date'] . ' 00:00:00');
  79. if (!empty($data['end_date'])) $query->where('login_at', '<=', $data['end_date'] . ' 23:59:59');
  80. $total = (clone $query)->count();
  81. $list = $query->orderByDesc('id')->forPage($page, $limit)->get();
  82. return compact('total', 'page', 'limit', 'list');
  83. });
  84. }
  85. public function dashboard(Request $request, AgentReportService $reports)
  86. {
  87. return $this->execute(function () use ($request, $reports) {
  88. $data = $request->validate([
  89. 'start_date' => ['nullable', 'date_format:Y-m-d'],
  90. 'end_date' => ['nullable', 'date_format:Y-m-d', 'after_or_equal:start_date'],
  91. ]);
  92. $start = $data['start_date'] ?? now()->toDateString();
  93. $end = $data['end_date'] ?? $start;
  94. return $reports->dashboard($this->currentAgent(), $start, $end);
  95. });
  96. }
  97. public function dashboardCharts(Request $request, AgentPeriodReportService $periods, AgentReportService $reports)
  98. {
  99. return $this->execute(function () use ($request, $periods, $reports) {
  100. $data = $request->validate([
  101. 'period' => ['nullable', 'in:' . implode(',', AgentPeriodReportService::PERIODS)],
  102. 'start_date' => ['nullable', 'date_format:Y-m-d'],
  103. 'end_date' => ['nullable', 'date_format:Y-m-d'],
  104. ]);
  105. $today = now()->startOfDay();
  106. $end = Carbon::parse($data['end_date'] ?? $today->toDateString())->startOfDay();
  107. $start = Carbon::parse($data['start_date'] ?? $end->copy()->subDays(29)->toDateString())->startOfDay();
  108. if ($start->gt($end)) {
  109. throw new \InvalidArgumentException('开始日期不能大于结束日期');
  110. }
  111. if ($end->gt($today)) {
  112. throw new \InvalidArgumentException('不能查询未来日期');
  113. }
  114. if ($start->diffInDays($end) >= 366) {
  115. throw new \InvalidArgumentException('单次查询不能超过366天');
  116. }
  117. $agent = $this->currentAgent();
  118. $period = $data['period'] ?? 'day';
  119. return [
  120. 'scope' => $agent->isEquity() ? 'platform' : 'own',
  121. 'period' => $period,
  122. 'start_date' => $start->toDateString(),
  123. 'end_date' => $end->toDateString(),
  124. 'registrations' => $periods->registrationTrend($agent, $period, $start, $end),
  125. 'daily_deposits' => $reports->dailyDeposits($agent, $start, $end),
  126. 'equity' => AgentEquityAllocation::distribution($agent),
  127. ];
  128. });
  129. }
  130. }