TotalMember.php 4.7 KB

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