ProfileController.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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\AgentReportService;
  7. use Illuminate\Http\Request;
  8. use Illuminate\Support\Facades\Hash;
  9. use App\Models\Agent\AgentToken;
  10. use Illuminate\Support\Facades\DB;
  11. class ProfileController extends AgentApiController
  12. {
  13. public function me(\App\Services\Agent\AgentProfileService $profiles)
  14. {
  15. return $this->execute(function () use ($profiles) {
  16. $agent = $this->currentAgent();
  17. return $profiles->summary($agent) + [
  18. 'last_login_at' => $agent->last_login_at?->toDateTimeString(),
  19. 'last_login_ip' => $agent->last_login_ip,
  20. ];
  21. });
  22. }
  23. public function update(Request $request)
  24. {
  25. return $this->execute(function () use ($request) {
  26. $data = $request->validate(['real_name' => ['nullable', 'string', 'max:128'], 'language' => ['sometimes', 'required', 'in:zh,en,vi']]);
  27. $agent = $this->currentAgent();
  28. if (array_key_exists('real_name', $data)) $agent->real_name = trim((string) ($data['real_name'] ?? ''));
  29. if (array_key_exists('language', $data)) $agent->language = $data['language'];
  30. $agent->save();
  31. return [];
  32. });
  33. }
  34. public function password(Request $request)
  35. {
  36. return $this->execute(function () use ($request) {
  37. $data = $request->validate([
  38. 'type' => ['required', 'in:login,withdraw'],
  39. 'old_password' => ['required', 'string'],
  40. 'password' => ['required', 'string', 'min:6', 'max:100', 'confirmed'],
  41. ]);
  42. DB::transaction(function () use ($data) {
  43. $agent = \App\Models\Agent\Agent::query()->lockForUpdate()->findOrFail($this->currentAgent()->id);
  44. if (!Hash::check($data['old_password'], (string) $agent->password)) {
  45. throw new \InvalidArgumentException('当前登录密码错误');
  46. }
  47. if ($data['type'] === 'login') {
  48. $agent->password = Hash::make($data['password']);
  49. AgentToken::query()->where('agent_id', $agent->id)->delete();
  50. } else {
  51. $agent->withdraw_password = Hash::make($data['password']);
  52. }
  53. $agent->save();
  54. });
  55. return [];
  56. });
  57. }
  58. public function domains(AgentDomainService $service)
  59. {
  60. return $this->execute(fn() => $service->format($this->currentAgent()));
  61. }
  62. public function loginLogs(Request $request)
  63. {
  64. return $this->execute(function () use ($request) {
  65. $data = $request->validate([
  66. 'page' => ['nullable', 'integer', 'min:1'], 'limit' => ['nullable', 'integer', 'min:1', 'max:100'],
  67. 'ip' => ['nullable', 'string', 'max:64'], 'device' => ['nullable', 'in:mobile,pc'],
  68. 'start_date' => ['nullable', 'date_format:Y-m-d'], 'end_date' => ['nullable', 'date_format:Y-m-d', 'after_or_equal:start_date'],
  69. ]);
  70. $page = max(1, (int) ($data['page'] ?? 1));
  71. $limit = min(100, max(1, (int) ($data['limit'] ?? 20)));
  72. $query = AgentLoginLog::query()->where('agent_id', $this->currentAgent()->id);
  73. if (!empty($data['ip'])) $query->where('ip', $data['ip']);
  74. if (!empty($data['device'])) $query->where('device', $data['device']);
  75. if (!empty($data['start_date'])) $query->where('login_at', '>=', $data['start_date'] . ' 00:00:00');
  76. if (!empty($data['end_date'])) $query->where('login_at', '<=', $data['end_date'] . ' 23:59:59');
  77. $total = (clone $query)->count();
  78. $list = $query->orderByDesc('id')->forPage($page, $limit)->get();
  79. return compact('total', 'page', 'limit', 'list');
  80. });
  81. }
  82. public function dashboard(Request $request, AgentReportService $reports)
  83. {
  84. return $this->execute(function () use ($request, $reports) {
  85. $data = $request->validate([
  86. 'start_date' => ['nullable', 'date_format:Y-m-d'],
  87. 'end_date' => ['nullable', 'date_format:Y-m-d', 'after_or_equal:start_date'],
  88. ]);
  89. $start = $data['start_date'] ?? now()->toDateString();
  90. $end = $data['end_date'] ?? $start;
  91. return $reports->summaryForTree($this->currentAgent(), $start, $end);
  92. });
  93. }
  94. }