TotalUsers.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <?php
  2. namespace App\Admin\Metrics;
  3. use App\Models\User;
  4. use Carbon\Carbon;
  5. use Dcat\Admin\Widgets\Metrics\Card;
  6. use Illuminate\Contracts\Support\Renderable;
  7. use Illuminate\Http\Request;
  8. class TotalUsers extends Card
  9. {
  10. /**
  11. * 卡片底部内容.
  12. *
  13. * @var string|Renderable|\Closure
  14. */
  15. protected $footer;
  16. /**
  17. * 初始化卡片.
  18. */
  19. protected function init()
  20. {
  21. parent::init();
  22. $this->title('用户');
  23. $this->dropdown([
  24. '1' => '当日新增用户',
  25. '7' => '7天新增用户',
  26. '30' => '当月新增用户',
  27. '-1' => '总用户',
  28. ]);
  29. }
  30. /**
  31. * 处理请求.
  32. *
  33. * @return void
  34. */
  35. public function handle(Request $request)
  36. {
  37. switch ($request->get('option')) {
  38. case '-1':
  39. $this->content($this->getUsers());
  40. $this->up(100);
  41. break;
  42. case '7':
  43. $startAt = Carbon::now()->subDays(7)->toDateString() . ' 23:59:59';
  44. $endAt = Carbon::now()->toDateString() . ' 23:59:59';
  45. $today = $this->getUsers($startAt, $endAt);
  46. $this->content($today);
  47. // 计算上7天
  48. $startAt = Carbon::now()->subDays(14)->toDateString() . ' 23:59:59';
  49. $endAt = Carbon::now()->subDays(7)->toDateString() . ' 23:59:59';
  50. $this->calcPercent($today, $startAt, $endAt);
  51. break;
  52. case '30':
  53. $startAt = Carbon::now()->subDays(30)->toDateString() . ' 23:59:59';
  54. $endAt = Carbon::now()->toDateString() . ' 23:59:59';
  55. $today = $this->getUsers($startAt, $endAt);
  56. $this->content($today);
  57. // 计算上个月
  58. $startAt = Carbon::now()->subDays(60)->toDateString() . ' 23:59:59';
  59. $endAt = Carbon::now()->subDays(30)->toDateString() . ' 23:59:59';
  60. $this->calcPercent($today, $startAt, $endAt);
  61. break;
  62. case '1':
  63. default:
  64. $startAt = Carbon::now()->toDateString() . ' 00:00:00';
  65. $endAt = Carbon::now()->toDateString() . ' 23:59:59';
  66. $today = $this->getUsers($startAt, $endAt);
  67. $this->content($today);
  68. // 计算昨天
  69. $startAt = Carbon::now()->subDays()->toDateString() . ' 00:00:00';
  70. $endAt = Carbon::now()->subDay()->toDateString() . ' 23:59:59';
  71. $this->calcPercent($today, $startAt, $endAt);
  72. break;
  73. }
  74. }
  75. private function calcPercent($today, $startAt, $endAt)
  76. {
  77. $yesterday = $this->getUsers($startAt, $endAt);
  78. // 百分比
  79. $percent = $yesterday ? ($today - $yesterday) / $yesterday : 0;
  80. $percent = round($percent, 2) * 100;
  81. $percent > 0 ? $this->up($percent) : $this->down($percent < 0 ? -$percent : $percent);
  82. }
  83. private function getUsers($startAt = '', $endAt = ''): int
  84. {
  85. return User::when($startAt, function ($query) use ($startAt) {
  86. $query->where('created_at', '>', $startAt);
  87. })->when($endAt, function ($query) use ($endAt) {
  88. $query->where('created_at', '<=', $endAt);
  89. })->count();
  90. }
  91. /**
  92. * @param int $percent
  93. *
  94. * @return $this
  95. */
  96. public function up($percent)
  97. {
  98. return $this->footer(
  99. "<i class=\"feather icon-trending-up text-success\"></i> {$percent}% 增加"
  100. );
  101. }
  102. /**
  103. * @param int $percent
  104. *
  105. * @return $this
  106. */
  107. public function down($percent)
  108. {
  109. return $this->footer(
  110. "<i class=\"feather icon-trending-down text-danger\"></i> {$percent}% 减少"
  111. );
  112. }
  113. /**
  114. * 设置卡片底部内容.
  115. *
  116. * @param string|Renderable|\Closure $footer
  117. *
  118. * @return $this
  119. */
  120. public function footer($footer)
  121. {
  122. $this->footer = $footer;
  123. return $this;
  124. }
  125. /**
  126. * 渲染卡片内容.
  127. *
  128. * @return string
  129. */
  130. public function renderContent()
  131. {
  132. $content = parent::renderContent();
  133. return <<<HTML
  134. <div class="d-flex justify-content-between align-items-center mt-1" style="margin-bottom: 2px">
  135. <h2 class="ml-1 font-lg-1">{$content}</h2>
  136. </div>
  137. <div class="ml-1 mt-1 font-weight-bold text-80">
  138. {$this->renderFooter()}
  139. </div>
  140. HTML;
  141. }
  142. /**
  143. * 渲染卡片底部内容.
  144. *
  145. * @return string
  146. */
  147. public function renderFooter()
  148. {
  149. return $this->toString($this->footer);
  150. }
  151. }