ProfileController.php 5.4 KB

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