UrsActiveUsersCard.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. namespace App\Module\UrsPromotion\AdminControllers\Metrics;
  3. use App\Module\UrsPromotion\Models\UrsUserMapping;
  4. use Dcat\Admin\Widgets\Metrics\Card;
  5. use Illuminate\Http\Request;
  6. /**
  7. * URS活跃用户数字卡片
  8. * 显示当前活跃用户数量
  9. */
  10. class UrsActiveUsersCard extends Card
  11. {
  12. /**
  13. * 卡片高度
  14. */
  15. protected $height = 140;
  16. /**
  17. * 卡片标题
  18. */
  19. protected $title = 'URS活跃用户';
  20. /**
  21. * 初始化卡片
  22. */
  23. public function __construct()
  24. {
  25. if ($options = $this->defaultChartOptions()) {
  26. $this->chartOptions = $options;
  27. }
  28. $this->init();
  29. }
  30. /**
  31. * 处理请求
  32. *
  33. * @param Request $request
  34. * @return mixed|void
  35. */
  36. public function handle(Request $request)
  37. {
  38. $activeCount = $this->getActiveUsersCount();
  39. $this->withContent($activeCount);
  40. }
  41. /**
  42. * 获取活跃用户数量
  43. * 这里统计所有有效状态的URS用户映射数量
  44. *
  45. * @return int
  46. */
  47. private function getActiveUsersCount(): int
  48. {
  49. return UrsUserMapping::where('status', UrsUserMapping::STATUS_VALID)
  50. ->count();
  51. }
  52. /**
  53. * 设置卡片内容
  54. *
  55. * @param int $content 活跃用户数量
  56. * @return $this
  57. */
  58. public function withContent($content)
  59. {
  60. return $this->content(
  61. <<<HTML
  62. <div class="d-flex flex-column flex-wrap text-center">
  63. <h1 class="font-lg-2 mt-2 mb-0">{$content}</h1>
  64. <small class="text-muted">当前活跃用户数</small>
  65. </div>
  66. HTML
  67. );
  68. }
  69. }