TotalUsers.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. * @return void
  32. */
  33. public function handle(Request $request)
  34. {
  35. switch ($request->get('option')) {
  36. case '365':
  37. $this->content(mt_rand(600, 1500));
  38. $this->down(mt_rand(1, 30));
  39. break;
  40. case '30':
  41. $this->content(mt_rand(170, 250));
  42. $this->up(mt_rand(12, 50));
  43. break;
  44. case '28':
  45. $this->content(mt_rand(155, 200));
  46. $this->up(mt_rand(5, 50));
  47. break;
  48. case '7':
  49. default:
  50. $this->content(143);
  51. $this->up(15);
  52. }
  53. }
  54. /**
  55. * @param int $percent
  56. *
  57. * @return $this
  58. */
  59. public function up($percent)
  60. {
  61. return $this->footer(
  62. "<i class=\"feather icon-trending-up text-success\"></i> {$percent}% Increase"
  63. );
  64. }
  65. /**
  66. * @param int $percent
  67. *
  68. * @return $this
  69. */
  70. public function down($percent)
  71. {
  72. return $this->footer(
  73. "<i class=\"feather icon-trending-down text-danger\"></i> {$percent}% Decrease"
  74. );
  75. }
  76. /**
  77. * 设置卡片底部内容.
  78. *
  79. * @param string|Renderable|\Closure $footer
  80. *
  81. * @return $this
  82. */
  83. public function footer($footer)
  84. {
  85. $this->footer = $footer;
  86. return $this;
  87. }
  88. /**
  89. * 渲染卡片内容.
  90. *
  91. * @return string
  92. */
  93. public function renderContent()
  94. {
  95. $content = parent::renderContent();
  96. return <<<HTML
  97. <div class="d-flex justify-content-between align-items-center mt-1" style="margin-bottom: 2px">
  98. <h2 class="ml-1 font-lg-1">{$content}</h2>
  99. </div>
  100. <div class="ml-1 mt-1 font-weight-bold text-80">
  101. {$this->renderFooter()}
  102. </div>
  103. HTML;
  104. }
  105. /**
  106. * 渲染卡片底部内容.
  107. *
  108. * @return string
  109. */
  110. public function renderFooter()
  111. {
  112. return $this->toString($this->footer);
  113. }
  114. }