TotalUsers.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. namespace App\Admin\Metrics\Examples;
  3. use Dcat\Admin\Widgets\Metrics\Card;
  4. use Illuminate\Contracts\Support\Renderable;
  5. use Illuminate\Http\Request;
  6. class TotalUsers extends Card
  7. {
  8. /**
  9. * 卡片底部内容.
  10. *
  11. * @var string|Renderable|\Closure
  12. */
  13. protected $footer;
  14. /**
  15. * 初始化卡片.
  16. */
  17. protected function init()
  18. {
  19. parent::init();
  20. $this->title('Total Users');
  21. $this->dropdown([
  22. '7' => 'Last 7 Days',
  23. '28' => 'Last 28 Days',
  24. '30' => 'Last Month',
  25. '365' => 'Last Year',
  26. ]);
  27. }
  28. /**
  29. * 处理请求.
  30. *
  31. * @param Request $request
  32. *
  33. * @return void
  34. */
  35. public function handle(Request $request)
  36. {
  37. switch ($request->get('option')) {
  38. case '365':
  39. $this->content(mt_rand(600, 1500));
  40. $this->down(mt_rand(1, 30));
  41. break;
  42. case '30':
  43. $this->content(mt_rand(170, 250));
  44. $this->up(mt_rand(12, 50));
  45. break;
  46. case '28':
  47. $this->content(mt_rand(155, 200));
  48. $this->up(mt_rand(5, 50));
  49. break;
  50. case '7':
  51. default:
  52. $this->content(143);
  53. $this->up(15);
  54. }
  55. }
  56. /**
  57. * @param int $percent
  58. *
  59. * @return $this
  60. */
  61. public function up($percent)
  62. {
  63. return $this->footer(
  64. "<i class=\"feather icon-trending-up text-success\"></i> {$percent}% Increase"
  65. );
  66. }
  67. /**
  68. * @param int $percent
  69. *
  70. * @return $this
  71. */
  72. public function down($percent)
  73. {
  74. return $this->footer(
  75. "<i class=\"feather icon-trending-down text-danger\"></i> {$percent}% Decrease"
  76. );
  77. }
  78. /**
  79. * 设置卡片底部内容.
  80. *
  81. * @param string|Renderable|\Closure $footer
  82. *
  83. * @return $this
  84. */
  85. public function footer($footer)
  86. {
  87. $this->footer = $footer;
  88. return $this;
  89. }
  90. /**
  91. * 渲染卡片内容.
  92. *
  93. * @return string
  94. */
  95. public function renderContent()
  96. {
  97. $content = parent::renderContent();
  98. return <<<HTML
  99. <div class="d-flex justify-content-between align-items-center mt-1" style="margin-bottom: 2px">
  100. <h2 class="ml-1 font-lg-1">{$content}</h2>
  101. </div>
  102. <div class="ml-1 mt-1 font-weight-bold text-80">
  103. {$this->renderFooter()}
  104. </div>
  105. HTML;
  106. }
  107. /**
  108. * 渲染卡片底部内容.
  109. *
  110. * @return string
  111. */
  112. public function renderFooter()
  113. {
  114. return $this->toString($this->footer);
  115. }
  116. }