TotalMember.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <?php
  2. namespace App\Admin\Metrics;
  3. use App\Models\UserInfo;
  4. use Carbon\Carbon;
  5. use Dcat\Admin\Widgets\Metrics\Card;
  6. use Illuminate\Contracts\Support\Renderable;
  7. use Illuminate\Http\Request;
  8. class TotalMember 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 UserInfo::where('is_vip', 1)
  86. ->when($startAt, function ($query) use ($startAt) {
  87. $query->where('created_at', '>', $startAt);
  88. })->when($endAt, function ($query) use ($endAt) {
  89. $query->where('created_at', '<=', $endAt);
  90. })->count();
  91. }
  92. /**
  93. * @param int $percent
  94. *
  95. * @return $this
  96. */
  97. public function up($percent)
  98. {
  99. return $this->footer(
  100. "<i class=\"feather icon-trending-up text-success\"></i> {$percent}% 增加"
  101. );
  102. }
  103. /**
  104. * @param int $percent
  105. *
  106. * @return $this
  107. */
  108. public function down($percent)
  109. {
  110. return $this->footer(
  111. "<i class=\"feather icon-trending-down text-danger\"></i> {$percent}% 减少"
  112. );
  113. }
  114. /**
  115. * 设置卡片底部内容.
  116. *
  117. * @param string|Renderable|\Closure $footer
  118. *
  119. * @return $this
  120. */
  121. public function footer($footer)
  122. {
  123. $this->footer = $footer;
  124. return $this;
  125. }
  126. /**
  127. * 渲染卡片内容.
  128. *
  129. * @return string
  130. */
  131. public function renderContent()
  132. {
  133. $content = parent::renderContent();
  134. return <<<HTML
  135. <div class="d-flex justify-content-between align-items-center mt-1" style="margin-bottom: 2px">
  136. <h2 class="ml-1 font-lg-1">{$content}</h2>
  137. </div>
  138. <div class="ml-1 mt-1 font-weight-bold text-80">
  139. {$this->renderFooter()}
  140. </div>
  141. HTML;
  142. }
  143. /**
  144. * 渲染卡片底部内容.
  145. *
  146. * @return string
  147. */
  148. public function renderFooter()
  149. {
  150. return $this->toString($this->footer);
  151. }
  152. }