NewUsers.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. namespace App\Admin\Metrics\Examples;
  3. use App\Models\User;
  4. use Dcat\Admin\Widgets\Metrics\Line;
  5. use Illuminate\Http\Request;
  6. class NewUsers extends Line
  7. {
  8. /**
  9. * 初始化卡片内容
  10. *
  11. * @return void
  12. */
  13. protected function init()
  14. {
  15. parent::init();
  16. $this->title(trans('admin-home.New_Users'));
  17. $this->dropdown([
  18. '7' => trans('admin-home.Last_7_days'),
  19. '30' => trans('admin-home.Last_month'),
  20. '180' => trans('admin-home.Last_half_year'),
  21. '365' => trans('admin-home.Last_year'),
  22. ]);
  23. // dd($this->new_user("365"));
  24. }
  25. /**
  26. * 处理请求
  27. *
  28. * @param Request $request
  29. *
  30. * @return mixed|void
  31. */
  32. public function handle(Request $request)
  33. {
  34. $generator = function ($len, $min = 10, $max = 300) {
  35. for ($i = 0; $i <= $len; $i++) {
  36. yield mt_rand($min, $max);
  37. }
  38. };
  39. switch ($request->get('option')) {
  40. case '365':
  41. $user = $this->new_user("365");
  42. // 卡片内容
  43. $this->withContent($user['num']);
  44. // 图表数据
  45. $this->withChart($user['num_arr']);
  46. break;
  47. case '180':
  48. $user = $this->new_user("180");
  49. // 卡片内容
  50. $this->withContent($user['num']);
  51. // 图表数据
  52. $this->withChart($user['num_arr']);
  53. break;
  54. case '30':
  55. $user = $this->new_user("30");
  56. // 卡片内容
  57. $this->withContent($user['num']);
  58. // 图表数据
  59. $this->withChart($user['num_arr']);
  60. break;
  61. case '7':
  62. default:
  63. $user = $this->new_user("7");
  64. // 卡片内容
  65. $this->withContent($user['num']);
  66. // 图表数据
  67. $this->withChart($user['num_arr']);
  68. }
  69. }
  70. /**
  71. * 设置图表数据.
  72. *
  73. * @param array $data
  74. *
  75. * @return $this
  76. */
  77. public function withChart(array $data)
  78. {
  79. return $this->chart([
  80. 'series' => [
  81. [
  82. 'name' => $this->title,
  83. 'data' => $data,
  84. ],
  85. ],
  86. ]);
  87. }
  88. /**
  89. * 设置卡片内容.
  90. *
  91. * @param string $content
  92. *
  93. * @return $this
  94. */
  95. public function withContent($content)
  96. {
  97. return $this->content(
  98. <<<HTML
  99. <div class="d-flex justify-content-between align-items-center mt-1" style="margin-bottom: 2px">
  100. <h2 class="ml-1 font-lg-1">{$content}</h2>
  101. <span class="mb-0 mr-1 text-80">{$this->title}</span>
  102. </div>
  103. HTML
  104. );
  105. }
  106. /**
  107. * 新用户 一年
  108. * write by wanglas
  109. */
  110. public function new_user($kind){
  111. if ($kind=='365'){
  112. $t_last = date('Y-m-d H:i:s',strtotime('-1 year'));
  113. $t_time = 86400*12;
  114. $count = 31;
  115. }elseif ($kind=='180'){
  116. $t_last = date('Y-m-d H:i:s',strtotime('-180 day'));
  117. $t_time = 86400*6;
  118. $count = 31;
  119. }elseif ($kind=='30'){
  120. $t_last = date('Y-m-d H:i:s',strtotime('-30 day'));
  121. $t_time = 86400;
  122. $count = 31;
  123. }elseif ($kind=='7'){
  124. $t_last = date('Y-m-d H:i:s',strtotime('-7 day'));
  125. $t_time = 86400;
  126. $count = 8;
  127. }
  128. //统计会员总数
  129. $t_now = date('Y-m-d H:i:s');
  130. $num=User::query()->whereBetween('created_at',array($t_last,$t_now))->whereNull('deleted_at')->count();
  131. //计算会员增长数量
  132. for ($i=1 ; $i < $count; $i++) {
  133. $today=strtotime(date('Ymd'));
  134. $now = date('Y-m-d H:i:s',$today-$t_time*($i-1));
  135. $time = date('Y-m-d H:i:s',$today-$t_time*$i);
  136. $inc_num=User::query()->whereBetween('created_at',array($time,$now))->count();
  137. //$arr_t[]=$now;
  138. $arr_n[]=$inc_num;
  139. }
  140. //逆序输出数组
  141. //$arr_t=array_reverse($arr_t); //时间
  142. $arr_n=array_reverse($arr_n); //数量
  143. return array('num'=>$num,'num_arr'=>$arr_n);
  144. }
  145. }