ProfileController.php 5.1 KB

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