| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113 |
- <?php
- namespace UCore\DcatAdmin\Metrics\User;
- use App\Module\User\Model\User;
- use Carbon\Carbon;
- use Dcat\Admin\Widgets\Metrics\Line;
- use UCore\Helper\Cache;
- use Illuminate\Http\Request;
- class NewUsers extends Line
- {
- /**
- * 初始化卡片内容
- *
- * @return void
- */
- protected function init()
- {
- parent::init();
- $this->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(
- <<<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
- );
- }
- }
|