TotalUsers.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. namespace App\Admin\Metrics\User;
  3. use App\Models\PaymentLogModel;
  4. use Dcat\Admin\Widgets\Metrics\Card;
  5. use Illuminate\Contracts\Support\Renderable;
  6. use Illuminate\Http\Request;
  7. class TotalUsers extends Card
  8. {
  9. /**
  10. * 卡片底部内容.
  11. *
  12. * @var string|Renderable|\Closure
  13. */
  14. protected $footer;
  15. /**
  16. * 初始化卡片.
  17. */
  18. protected function init()
  19. {
  20. parent::init();
  21. $this->title('累计金额');
  22. $this->dropdown([
  23. '7' => '最近7天',
  24. '30' => '最近一个月',
  25. '365' => '最近一年',
  26. ]);
  27. $this->style(
  28. <<<STYLE
  29. .App_Admin_Metrics_User_TotalUsers{min-height: 120px!important;}
  30. STYLE
  31. );
  32. }
  33. /**
  34. * 处理请求.
  35. *
  36. * @param Request $request
  37. *
  38. * @return void
  39. */
  40. public function handle(Request $request)
  41. {
  42. switch ($request->get('option')) {
  43. case '365':
  44. $this->content(mt_rand(600, 1500));
  45. $this->down(mt_rand(1, 30));
  46. break;
  47. case '30':
  48. $this->content(mt_rand(170, 250));
  49. $this->up(mt_rand(12, 50));
  50. break;
  51. case '7':
  52. default:
  53. $start_time = date("Y-m-d 00:00:00",(time()-86400*7));
  54. $end_time = date("Y-m-d 23:59:59",time());
  55. $total = PaymentLogModel::query()
  56. ->where(['status'=>1,'type'=>1])
  57. ->whereBetween('created_at',[$start_time,$end_time])
  58. ->sum('price');
  59. $this->content($total);
  60. }
  61. }
  62. /**
  63. * @param int $percent
  64. *
  65. * @return $this
  66. */
  67. public function up($percent)
  68. {
  69. return $this->footer(
  70. "<i class=\"feather icon-trending-up text-success\"></i> {$percent}% Increase"
  71. );
  72. }
  73. /**
  74. * @param int $percent
  75. *
  76. * @return $this
  77. */
  78. public function down($percent)
  79. {
  80. return $this->footer(
  81. "<i class=\"feather icon-trending-down text-danger\"></i> {$percent}% Decrease"
  82. );
  83. }
  84. /**
  85. * 设置卡片底部内容.
  86. *
  87. * @param string|Renderable|\Closure $footer
  88. *
  89. * @return $this
  90. */
  91. public function footer($footer)
  92. {
  93. $this->footer = $footer;
  94. return $this;
  95. }
  96. /**
  97. * 渲染卡片内容.
  98. *
  99. * @return string
  100. */
  101. public function renderContent()
  102. {
  103. $content = parent::renderContent();
  104. return <<<HTML
  105. <div class="d-flex justify-content-between align-items-center mt-1" style="margin-bottom: 2px">
  106. <h2 class="ml-1 font-lg-1">{$content}</h2>
  107. </div>
  108. HTML;
  109. }
  110. /**
  111. * 渲染卡片底部内容.
  112. *
  113. * @return string
  114. */
  115. public function renderFooter()
  116. {
  117. return $this->toString($this->footer);
  118. }
  119. }