136 lines
3.7 KiB
PHP
136 lines
3.7 KiB
PHP
<?php
|
|
|
|
namespace App\Admin\Metrics;
|
|
|
|
use App\Models\User;
|
|
use Carbon\Carbon;
|
|
use Dcat\Admin\Widgets\Metrics\Line;
|
|
use Illuminate\Http\Request;
|
|
|
|
class NewUsers extends Line
|
|
{
|
|
/**
|
|
* 初始化卡片内容
|
|
*
|
|
* @return void
|
|
*/
|
|
protected function init()
|
|
{
|
|
parent::init();
|
|
|
|
$this->title('新注册');
|
|
$this->dropdown([
|
|
'1' => '当天',
|
|
'7' => '最近7天',
|
|
'30' => '最近30天',
|
|
'90' => '最近3个月',
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* 处理请求
|
|
*
|
|
* @param Request $request
|
|
*
|
|
* @return mixed|void
|
|
*/
|
|
public function handle(Request $request)
|
|
{
|
|
switch ($request->get('option')) {
|
|
case '90':
|
|
// 卡片内容
|
|
$start_time = Carbon::now()->subDays(89)->startOfDay();
|
|
$end_time = Carbon::now()->endOfDay();
|
|
$this->withContent($this->getUserCount($start_time, $end_time).'人');
|
|
// 图表数据
|
|
$this->withChart($this->getUserGenerator($start_time, $end_time, 90));
|
|
break;
|
|
case '30':
|
|
// 卡片内容
|
|
$start_time = Carbon::now()->subDays(29)->startOfDay();
|
|
$end_time = Carbon::now()->endOfDay();
|
|
$this->withContent($this->getUserCount($start_time, $end_time).'人');
|
|
// 图表数据
|
|
$this->withChart($this->getUserGenerator($start_time, $end_time, 30));
|
|
break;
|
|
case '7':
|
|
default:
|
|
// 卡片内容
|
|
$start_time = Carbon::now()->startOfDay();
|
|
$end_time = Carbon::now()->endOfDay();
|
|
$this->withContent($this->getUserCount($start_time, $end_time).'人');
|
|
// 图表数据
|
|
$this->withChart($this->getUserGenerator($start_time, $end_time, 1));
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 设置图表数据.
|
|
*
|
|
* @param array $data
|
|
*
|
|
* @return $this
|
|
*/
|
|
public function withChart(array $data)
|
|
{
|
|
return $this->chart([
|
|
'series' => [
|
|
[
|
|
'name' => $this->title,
|
|
'data' => array_values($data),
|
|
],
|
|
],
|
|
'xaxis' => [
|
|
'categories'=> array_keys($data),
|
|
],
|
|
'tooltip' => [
|
|
'x' => [
|
|
'show' => true,
|
|
],
|
|
],
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* 获取指定时间段用户数量
|
|
*/
|
|
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 $list;
|
|
}
|
|
|
|
/**
|
|
* 设置卡片内容.
|
|
*
|
|
* @param string $content
|
|
*
|
|
* @return $this
|
|
*/
|
|
public function withContent($content)
|
|
{
|
|
return $this->content(
|
|
<<<HTML
|
|
<div class="d-flex justify-content-between align-items-center mt-1" style="margin-bottom: 2px">
|
|
<h2 class="ml-1 font-lg-1">{$content}</h2>
|
|
<span class="mb-0 mr-1 text-80">{$this->title}</span>
|
|
</div>
|
|
HTML
|
|
);
|
|
}
|
|
}
|