NewProduct.php 4.3 KB

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