Online.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <?php
  2. namespace App\Http\Controllers\admin;
  3. use App\Constants\HttpStatus;
  4. use App\Http\Controllers\Controller;
  5. use App\Services\NewUserSummaryService;
  6. use App\Services\OnlineUserService;
  7. use Exception;
  8. use Illuminate\Validation\ValidationException;
  9. class Online extends Controller
  10. {
  11. /**
  12. * 在线用户列表
  13. */
  14. public function users()
  15. {
  16. try {
  17. $params = request()->validate([
  18. 'page' => ['nullable', 'integer', 'min:1'],
  19. 'limit' => ['nullable', 'integer', 'min:1', 'max:200'],
  20. 'member_id' => ['nullable', 'string'],
  21. 'first_name' => ['nullable', 'string'],
  22. 'login_ip' => ['nullable', 'string'],
  23. 'platform' => ['nullable', 'string'],
  24. ]);
  25. $result = OnlineUserService::list($params);
  26. } catch (ValidationException $e) {
  27. return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
  28. } catch (Exception $e) {
  29. return $this->error(HttpStatus::CUSTOM_ERROR, $e->getMessage());
  30. }
  31. return $this->success($result);
  32. }
  33. /**
  34. * 新用户汇总(按天)
  35. */
  36. public function newUserSummary()
  37. {
  38. try {
  39. $params = request()->validate([
  40. 'start_time' => ['nullable', 'date', 'date_format:Y-m-d'],
  41. 'end_time' => ['nullable', 'date', 'date_format:Y-m-d', 'after_or_equal:start_time'],
  42. ]);
  43. $endDate = $params['end_time'] ?? date('Y-m-d');
  44. $startDate = $params['start_time'] ?? date('Y-m-d', strtotime($endDate . ' -6 days'));
  45. $result = NewUserSummaryService::summarize($startDate, $endDate);
  46. } catch (ValidationException $e) {
  47. return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
  48. } catch (Exception $e) {
  49. return $this->error(HttpStatus::CUSTOM_ERROR, $e->getMessage());
  50. }
  51. return $this->success($result);
  52. }
  53. }