ProfileController.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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(AgentDomainService $domains)
  14. {
  15. return $this->execute(function () use ($domains) {
  16. $agent = $this->currentAgent()->load('parent:id,username');
  17. return array_merge([
  18. 'id' => (int) $agent->id,
  19. 'username' => $agent->username,
  20. 'real_name' => $agent->real_name,
  21. 'parent_id' => $agent->parent_id,
  22. 'parent_username' => $agent->parent?->username,
  23. 'level' => (int) $agent->level,
  24. 'level_text' => $agent->level_text,
  25. 'can_create_sub_agent' => $agent->can_create_sub_agent,
  26. 'invitation_code' => $agent->invitation_code,
  27. 'balance' => (string) $agent->balance,
  28. 'frozen_balance' => (string) $agent->frozen_balance,
  29. 'deposit_fee_rate' => (string) $agent->deposit_fee_rate,
  30. 'withdraw_fee_rate' => (string) $agent->withdraw_fee_rate,
  31. 'flow_commission_rate' => (string) $agent->flow_commission_rate,
  32. 'profit_commission_rate' => (string) $agent->profit_commission_rate,
  33. 'last_login_at' => $agent->last_login_at?->toDateTimeString(),
  34. 'last_login_ip' => $agent->last_login_ip,
  35. ], $domains->format($agent));
  36. });
  37. }
  38. public function update(Request $request)
  39. {
  40. return $this->execute(function () use ($request) {
  41. $data = $request->validate(['real_name' => ['nullable', 'string', 'max:128']]);
  42. $agent = $this->currentAgent();
  43. $agent->real_name = trim((string) ($data['real_name'] ?? ''));
  44. $agent->save();
  45. return [];
  46. });
  47. }
  48. public function password(Request $request)
  49. {
  50. return $this->execute(function () use ($request) {
  51. $data = $request->validate([
  52. 'type' => ['required', 'in:login,withdraw'],
  53. 'old_password' => ['required', 'string'],
  54. 'password' => ['required', 'string', 'min:6', 'max:100', 'confirmed'],
  55. ]);
  56. DB::transaction(function () use ($data) {
  57. $agent = \App\Models\Agent\Agent::query()->lockForUpdate()->findOrFail($this->currentAgent()->id);
  58. if (!Hash::check($data['old_password'], (string) $agent->password)) {
  59. throw new \InvalidArgumentException('当前登录密码错误');
  60. }
  61. if ($data['type'] === 'login') {
  62. $agent->password = Hash::make($data['password']);
  63. AgentToken::query()->where('agent_id', $agent->id)->delete();
  64. } else {
  65. $agent->withdraw_password = Hash::make($data['password']);
  66. }
  67. $agent->save();
  68. });
  69. return [];
  70. });
  71. }
  72. public function domains(AgentDomainService $service)
  73. {
  74. return $this->execute(fn() => $service->format($this->currentAgent()));
  75. }
  76. public function loginLogs(Request $request)
  77. {
  78. return $this->execute(function () use ($request) {
  79. $data = $request->validate([
  80. 'page' => ['nullable', 'integer', 'min:1'], 'limit' => ['nullable', 'integer', 'min:1', 'max:100'],
  81. 'ip' => ['nullable', 'string', 'max:64'], 'device' => ['nullable', 'in:mobile,pc'],
  82. 'start_date' => ['nullable', 'date_format:Y-m-d'], 'end_date' => ['nullable', 'date_format:Y-m-d', 'after_or_equal:start_date'],
  83. ]);
  84. $page = max(1, (int) ($data['page'] ?? 1));
  85. $limit = min(100, max(1, (int) ($data['limit'] ?? 20)));
  86. $query = AgentLoginLog::query()->where('agent_id', $this->currentAgent()->id);
  87. if (!empty($data['ip'])) $query->where('ip', $data['ip']);
  88. if (!empty($data['device'])) $query->where('device', $data['device']);
  89. if (!empty($data['start_date'])) $query->where('login_at', '>=', $data['start_date'] . ' 00:00:00');
  90. if (!empty($data['end_date'])) $query->where('login_at', '<=', $data['end_date'] . ' 23:59:59');
  91. $total = (clone $query)->count();
  92. $list = $query->orderByDesc('id')->forPage($page, $limit)->get();
  93. return compact('total', 'page', 'limit', 'list');
  94. });
  95. }
  96. public function dashboard(Request $request, AgentReportService $reports)
  97. {
  98. return $this->execute(function () use ($request, $reports) {
  99. $data = $request->validate([
  100. 'start_date' => ['nullable', 'date_format:Y-m-d'],
  101. 'end_date' => ['nullable', 'date_format:Y-m-d', 'after_or_equal:start_date'],
  102. ]);
  103. $start = $data['start_date'] ?? now()->toDateString();
  104. $end = $data['end_date'] ?? $start;
  105. return $reports->summaryForTree($this->currentAgent(), $start, $end);
  106. });
  107. }
  108. }