height(300); $this->chartHeight(180); $this->title('新用户'); $this->dropdown([ '7' => '最近 7 天', '14' => '最近 14 天', '28' => '最近 28 天', '90' => '最近 90 天', ]); } /** * 处理请求 * * @param Request $request * * @return mixed|void */ public function handle(Request $request) { $data = $this->getData($request->get('option', 7)); // 卡片内容 $this->withContent($data['count']); // 图表数据 $this->withChart($data['list']); } protected function getData($day) { return Cache::cacheCall([ __CLASS__, __FUNCTION__, $day ], function ($day) { $data = [ ]; $data['count'] = User::query() ->where('created_at','>',Carbon::now()->subDays($day)) ->count(); foreach (range($day,1) as $item){ $sub = $item - 1; $data['list'][] = User::query() ->whereBetween('created_at',[ Carbon::now()->subDays($sub)->startOfDay(), Carbon::now()->subDays($sub)->endOfDay() ]) ->count(); } return $data; }, [ $day ], 30); } /** * 设置图表数据. * * @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}

{$this->title} HTML ); } }