From 5fffcf0fc321cb0a061d2cb7d424a6312572a755 Mon Sep 17 00:00:00 2001 From: vine_liutk <961510893@qq.com> Date: Fri, 31 Dec 2021 15:48:48 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B0=83=E6=95=B4=E6=96=B0=E4=BC=9A=E5=91=98?= =?UTF-8?q?=E7=BB=9F=E8=AE=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Admin/Metrics/NewUsers.php | 51 +++++++++++++++++++++++++--------- 1 file changed, 38 insertions(+), 13 deletions(-) diff --git a/app/Admin/Metrics/NewUsers.php b/app/Admin/Metrics/NewUsers.php index 0ba45129..c442107f 100644 --- a/app/Admin/Metrics/NewUsers.php +++ b/app/Admin/Metrics/NewUsers.php @@ -2,6 +2,8 @@ namespace App\Admin\Metrics; +use App\Models\User; +use Carbon\Carbon; use Dcat\Admin\Widgets\Metrics\Line; use Illuminate\Http\Request; @@ -40,30 +42,30 @@ class NewUsers extends Line }; switch ($request->get('option')) { - case '365': + case '90': // 卡片内容 - $this->withContent(mt_rand(1000, 5000).'k'); + $start_time = Carbon::now()->subDays(89)->startOfDay(); + $end_time = Carbon::now()->endOfDay(); + $this->withContent($this->getUserCount($start_time, $end_time).'人'); // 图表数据 - $this->withChart(collect($generator(30))->toArray()); + $this->withChart($this->getUserGenerator($start_time, $end_time, 90)); break; case '30': // 卡片内容 - $this->withContent(mt_rand(400, 1000).'k'); + $start_time = Carbon::now()->subDays(29)->startOfDay(); + $end_time = Carbon::now()->endOfDay(); + $this->withContent($this->getUserCount($start_time, $end_time).'人'); // 图表数据 - $this->withChart(collect($generator(30))->toArray()); - break; - case '28': - // 卡片内容 - $this->withContent(mt_rand(400, 1000).'k'); - // 图表数据 - $this->withChart(collect($generator(28))->toArray()); + $this->withChart($this->getUserGenerator($start_time, $end_time, 30)); break; case '7': default: // 卡片内容 - $this->withContent('89.2k'); + $start_time = Carbon::now()->subDays(7)->startOfDay(); + $end_time = Carbon::now()->endOfDay(); + $this->withContent($this->getUserCount($start_time, $end_time).'人'); // 图表数据 - $this->withChart([28, 40, 36, 52, 38, 60, 55]); + $this->withChart($this->getUserGenerator($start_time, $end_time, 7)); } } @@ -86,6 +88,29 @@ class NewUsers extends Line ]); } + /** + * 获取指定时间段用户数量 + */ + public function getUserCount($start_time, $end_time) + { + return User::whereBetween('created_at', [$start_time, $end_time])->count(); + } + + public function getUserGenerator($start_time, $end_time, $days, $groupBy='d') + { + $list = User::selectRaw("COUNT(DISTINCT id) AS userNum, DATE_FORMAT(created_at,'%Y-%m-%d') dDay") + ->whereBetween('created_at', [$start_time, $end_time]) + ->groupBy('dDay')->pluck('userNum', 'dDay')->toArray(); + for ($i = 0; $i<=$days; $i++) { + $time = Carbon::now()->subDay($i)->toDateString(); + if (!isset($list[Carbon::now()->subDay($i)->toDateString()])) { + $list[$time] = 0; + } + } + ksort($list); + return array_values($list); + } + /** * 设置卡片内容. *