diff --git a/app/AdminAgent/Metrics/Examples/NewUsers.php b/app/AdminAgent/Metrics/Examples/NewUsers.php index e9b7ced..ca07a63 100644 --- a/app/AdminAgent/Metrics/Examples/NewUsers.php +++ b/app/AdminAgent/Metrics/Examples/NewUsers.php @@ -2,107 +2,118 @@ namespace App\AdminAgent\Metrics\Examples; +use App\Common\DataTime; +use App\Models\User; +use Carbon\Carbon; +use Dcat\Admin\Admin; use Dcat\Admin\Widgets\Metrics\Line; use Illuminate\Http\Request; +use Illuminate\Support\Arr; +use Illuminate\Support\Facades\DB; class NewUsers extends Line { - /** - * 初始化卡片内容 - * - * @return void - */ - protected function init() - { - parent::init(); + /** + * 初始化卡片内容 + * + * @return void + */ + protected function init() + { + parent::init(); - $this->title('New Users'); - $this->dropdown([ - '7' => 'Last 7 Days', - '28' => 'Last 28 Days', - '30' => 'Last Month', - '365' => 'Last Year', - ]); - } + $this->title('新增用户'); + $this->dropdown([ + //'day' => '本日', + 'week' => '本周', + 'month' => '本月', + //'year' => '今年', + ]); + } - /** - * 处理请求 - * - * @param Request $request - * - * @return mixed|void - */ - public function handle(Request $request) - { - $generator = function ($len, $min = 10, $max = 300) { - for ($i = 0; $i <= $len; $i++) { - yield mt_rand($min, $max); - } - }; + /** + * 处理请求 + * + * @param Request $request + * + * @return mixed|void + */ + public function handle(Request $request) + { + $count = User::query()->where('agent_id', Admin::user()->id); + $user = User::query() + ->select('*') + ->addSelect(DB::raw("COUNT(id) as count_id,CONCAT(YEAR(created_at),'-',MONTH(created_at),'-',DAY(created_at)) AS statistics_time")); + switch ($request->get('option')) { + case 'day': + $count->whereDate('created_at', Carbon::today()); + $user->whereDate('created_at', Carbon::today()); + break; + case 'week': + $time = DataTime::beginAndEnd('week'); + $count->whereBetween('created_at', $time); + $user->whereBetween('created_at', $time); + break; + case 'month': + $count->whereMonth('created_at', Carbon::now()->month); + $user->whereMonth('created_at', Carbon::now()->month); + break; + case 'year': + $count->whereYear('created_at', Carbon::now()->year); + $user->whereYear('created_at', Carbon::now()->year); + break; + default: + $time = DataTime::beginAndEnd('week'); + $count->whereBetween('created_at', $time); + $user->whereBetween('created_at', $time); + } + $user = $user->where('agent_id', Admin::user()->id) + ->limit(30) + ->groupBy('statistics_time') + ->get() + ->toArray(); + $count = $count->count(); + $this->withContent($count); + // 图表数据 + $this->chartLabels(Arr::pluck($user, 'statistics_time')); + $this->withChart(Arr::pluck($user, 'count_id')); + } - switch ($request->get('option')) { - case '365': - // 卡片内容 - $this->withContent(mt_rand(1000, 5000).'k'); - // 图表数据 - $this->withChart(collect($generator(30))->toArray()); - break; - case '30': - // 卡片内容 - $this->withContent(mt_rand(400, 1000).'k'); - // 图表数据 - $this->withChart(collect($generator(30))->toArray()); - break; - case '28': - // 卡片内容 - $this->withContent(mt_rand(400, 1000).'k'); - // 图表数据 - $this->withChart(collect($generator(28))->toArray()); - break; - case '7': - default: - // 卡片内容 - $this->withContent('89.2k'); - // 图表数据 - $this->withChart([28, 40, 36, 52, 38, 60, 55,]); - } - } + /** + * 设置图表数据. + * + * @param array $data + * + * @return $this + */ + public function withChart(array $data) + { + return $this->chart([ + 'series' => [ + [ + 'name' => $this->title, + 'data' => $data, + ], + ], + ]); + } - /** - * 设置图表数据. - * - * @param array $data - * - * @return $this - */ - public function withChart(array $data) - { - return $this->chart([ - 'series' => [ - [ - 'name' => $this->title, - 'data' => $data, - ], - ], - ]); - } - - /** - * 设置卡片内容. - * - * @param string $content - * - * @return $this - */ - public function withContent($content) - { - return $this->content( - <<content( + <<

{$content}

{$this->title} HTML - ); - } + ); + } } diff --git a/app/Common/DataTime.php b/app/Common/DataTime.php new file mode 100644 index 0000000..b0462a7 --- /dev/null +++ b/app/Common/DataTime.php @@ -0,0 +1,78 @@ +0) { + return date('Y-m-d H:i', $time); + } elseif (empty($time) || $time->format('Y-m-d') == '1970-01-01') { + return '-'; + } + return $time->format('Y-m-d H:i'); + } + + /** + * 查询时间范围 + * @param $option + * @return array + */ + public static function beginAndEnd($option) + { + $t = time(); + switch ($option) { + case 'today': + $beginTime = date('Y-m-d H:i:s', mktime(0, 0, 0, date("m", $t), date("d", $t), date("Y", $t))); + $endTime = date('Y-m-d H:i:s',mktime(0, 0, 0, date('m'), date('d') + 1, date('Y')) - 1); + break; + case 'yesterday': + $beginTime = date('Y-m-d H:i:s',mktime(0, 0, 0, date('m'), date('d') - 1, date('Y'))); + $endTime = date('Y-m-d H:i:s',mktime(0, 0, 0, date('m'), date('d'), date('Y')) - 1); + break; + case '7day': + $beginTime = date('Y-m-d H:i:s',mktime(0, 0, 0, date('m'), date('d') - 7, date('Y'))); + $endTime = date('Y-m-d H:i:s',mktime(0, 0, 0, date('m'), date('d') + 1, date('Y')) - 1); + break; + case 'week': + $timestamp = mktime(0, 0, 0, date('m'), date('d') - date('w') + 1, date('Y')); + $beginTime = date('Y-m-d H:i:s', $timestamp); + + //本周结束日期 + $timestamp = mktime(23, 59, 59, date('m'), date('d') - date('w') + 7, date('Y')); + $endTime= date('Y-m-d H:i:s',$timestamp); + break; + case 'last_week': + $beginTime = date('Y-m-d H:i:s',mktime(0, 0, 0, date("m"), date("d") - date("w") + 1 - 7, date("Y"))); + $endTime = date('Y-m-d H:i:s',mktime(23, 59, 59, date("m"), date("d") - date("w") + 7 - 7, date("Y"))); + break; + case 'month': + $beginTime = date('Y-m-d H:i:s',mktime(0, 0, 0, date('m'), 1, date('Y'))); + $endTime = date('Y-m-d H:i:s',mktime(23, 59, 59, date('m'), date('t'), date('Y'))); + break; + case 'last_month': + $beginTime = date('Y-m-d H:i:s',mktime(0, 0, 0, date('m') - 1, 1, date('Y'))); + $endTime = date('Y-m-d H:i:s',strtotime(date("Y-m-d H:i:s", mktime(23, 59, 59, date("m"), 0, date("Y"))))); + break; + default: + $searchTime = request()->input('created_at'); + if ($searchTime){ + $beginTime = date('Y-m-d H:i:s',strtotime($searchTime['start'])); + $endTime = date('Y-m-d H:i:s',strtotime($searchTime['end'])); + }else{ + $beginTime = 0; + $endTime = 999999999999; + } + break; + } + return [$beginTime,$endTime]; + } +}