ProfileController.php 5.8 KB

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