TotalUsers.php 3.1 KB

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