123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169 |
- <?php
- namespace App\Admin\Metrics;
- use App\Models\UserInfo;
- use Carbon\Carbon;
- use Dcat\Admin\Widgets\Metrics\Card;
- use Illuminate\Contracts\Support\Renderable;
- use Illuminate\Http\Request;
- class TotalMember extends Card
- {
- /**
- * 卡片底部内容.
- *
- * @var string|Renderable|\Closure
- */
- protected $footer;
- /**
- * 初始化卡片.
- */
- protected function init()
- {
- parent::init();
- $this->title('会员');
- $this->dropdown([
- '1' => '当日新增会员',
- '7' => '7天新增会员',
- '30' => '当月新增会员',
- '-1' => '总会员',
- ]);
- }
- /**
- * 处理请求.
- *
- * @return void
- */
- public function handle(Request $request)
- {
- switch ($request->get('option')) {
- case '-1':
- $this->content($this->getUsers());
- $this->up(100);
- break;
- case '7':
- $startAt = Carbon::now()->subDays(7)->toDateString() . ' 23:59:59';
- $endAt = Carbon::now()->toDateString() . ' 23:59:59';
- $today = $this->getUsers($startAt, $endAt);
- $this->content($today);
- // 计算上7天
- $startAt = Carbon::now()->subDays(14)->toDateString() . ' 23:59:59';
- $endAt = Carbon::now()->subDays(7)->toDateString() . ' 23:59:59';
- $this->calcPercent($today, $startAt, $endAt);
- break;
- case '30':
- $startAt = Carbon::now()->subDays(30)->toDateString() . ' 23:59:59';
- $endAt = Carbon::now()->toDateString() . ' 23:59:59';
- $today = $this->getUsers($startAt, $endAt);
- $this->content($today);
- // 计算上个月
- $startAt = Carbon::now()->subDays(60)->toDateString() . ' 23:59:59';
- $endAt = Carbon::now()->subDays(30)->toDateString() . ' 23:59:59';
- $this->calcPercent($today, $startAt, $endAt);
- break;
- case '1':
- default:
- $startAt = Carbon::now()->toDateString() . ' 00:00:00';
- $endAt = Carbon::now()->toDateString() . ' 23:59:59';
- $today = $this->getUsers($startAt, $endAt);
- $this->content($today);
- // 计算前天
- $startAt = Carbon::now()->subDays()->toDateString() . ' 00:00:00';
- $endAt = Carbon::now()->subDay()->toDateString() . ' 23:59:59';
- $this->calcPercent($today, $startAt, $endAt);
- break;
- }
- }
- private function calcPercent($today, $startAt, $endAt)
- {
- $yesterday = $this->getUsers($startAt, $endAt);
- // 百分比
- $percent = $yesterday ? ($today - $yesterday) / $yesterday : 0;
- $percent = round($percent, 2) * 100;
- $percent > 0 ? $this->up($percent) : $this->down($percent < 0 ? -$percent : $percent);
- }
- private function getUsers($startAt = '', $endAt = ''): int
- {
- return UserInfo::where('is_vip', 1)
- ->when($startAt, function ($query) use ($startAt) {
- $query->where('created_at', '>', $startAt);
- })->when($endAt, function ($query) use ($endAt) {
- $query->where('created_at', '<=', $endAt);
- })->count();
- }
- /**
- * @param int $percent
- *
- * @return $this
- */
- public function up($percent)
- {
- return $this->footer(
- "<i class=\"feather icon-trending-up text-success\"></i> {$percent}% 增加"
- );
- }
- /**
- * @param int $percent
- *
- * @return $this
- */
- public function down($percent)
- {
- return $this->footer(
- "<i class=\"feather icon-trending-down text-danger\"></i> {$percent}% 减少"
- );
- }
- /**
- * 设置卡片底部内容.
- *
- * @param string|Renderable|\Closure $footer
- *
- * @return $this
- */
- public function footer($footer)
- {
- $this->footer = $footer;
- return $this;
- }
- /**
- * 渲染卡片内容.
- *
- * @return string
- */
- public function renderContent()
- {
- $content = parent::renderContent();
- return <<<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>
- </div>
- <div class="ml-1 mt-1 font-weight-bold text-80">
- {$this->renderFooter()}
- </div>
- HTML;
- }
- /**
- * 渲染卡片底部内容.
- *
- * @return string
- */
- public function renderFooter()
- {
- return $this->toString($this->footer);
- }
- }
|