NewLock.php 4.3 KB

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