链街Dcat后台
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

120 lines
3.8 KiB

  1. <?php
  2. namespace App\Admin\Actions\Grid\v3;
  3. use Dcat\Admin\Actions\Response;
  4. use Dcat\Admin\Admin;
  5. use Dcat\Admin\Tree\AbstractTool;
  6. use Illuminate\Http\Request;
  7. class DataReportOption extends AbstractTool
  8. {
  9. protected $url;
  10. protected $option;
  11. protected $title = '';
  12. protected $moreOption = [];
  13. public function __construct($option = '', $url = '', $title = '', $more = [])
  14. {
  15. $this->option = $option;
  16. $this->url = $url;
  17. $this->title = $title;
  18. $this->moreOption = $more;
  19. }
  20. /**
  21. * Handle the action request.
  22. *
  23. * @param Request $request
  24. *
  25. * @return Response
  26. */
  27. public function handle(Request $request)
  28. {
  29. // $date = $this->getDataByOption($this->option);
  30. // $startTime = $date['start'] ?? '';
  31. // $endTime = $date['end'] ?? '';
  32. return $this->response()
  33. ->success('查询中~');
  34. // ->redirect('/'.$this->url.'?start_time='.$startTime.'&end_time='.$endTime);
  35. }
  36. /**
  37. * @return string
  38. */
  39. protected function html()
  40. {
  41. $date = $this->getDataByOption($this->option);
  42. $startTime = $date['start'] ?? '';
  43. $endTime = $date['end'] ?? '';
  44. $url = 'admin/'.$this->url.'?start_time='.$startTime.'&end_time='.$endTime;
  45. if(!empty($this->moreOption) && is_array($this->moreOption)){
  46. foreach($this->moreOption as $key => $value){
  47. $url .= '&'. $key .'='. $value;
  48. }
  49. }
  50. $this->defaultHtmlAttribute('href', url($url));
  51. return <<<HTML
  52. <a {$this->formatHtmlAttributes()}>{$this->title()}</a>
  53. HTML;
  54. }
  55. public function getDataByOption($option)
  56. {
  57. $date = ['start','end'];
  58. $today = date('Y-m-d');
  59. switch($option){
  60. case 'today':
  61. $date['start'] = $today;
  62. $date['end'] = $today;
  63. break;
  64. case 'yesterday':
  65. $yesterday = date("Y-m-d",strtotime("-1 days",time()));
  66. $date['start'] = $yesterday;
  67. $date['end'] = $yesterday;
  68. break;
  69. case 'this_week':
  70. $first=1;
  71. //获取当前周的第几天 周日是 0 周一到周六是 1 - 6
  72. $w=date('w',strtotime($today));
  73. //获取本周开始日期,如果$w是0,则表示周日,减去 6 天
  74. $week_start=date('Y-m-d',strtotime("$today -".($w ? $w - $first : 6).' days'));
  75. //本周结束日期
  76. $week_end=date('Y-m-d',strtotime("$week_start +6 days"));
  77. $date['start'] = $week_start;
  78. $date['end'] = $week_end;
  79. break;
  80. case 'last_week':
  81. // 上周日
  82. $lastSunday = date('Y-m-d', strtotime('-1 sunday', time()));
  83. // 上周一
  84. $lastMonday = date('Y-m-d', strtotime('-1 monday', strtotime($lastSunday)));
  85. $date['start'] = $lastMonday;
  86. $date['end'] = $lastSunday;
  87. break;
  88. case 'this_month':
  89. $thisMonthStart = date('Y-m-01', strtotime($today));
  90. $thisMonthEnd = date('Y-m-d', strtotime($today));
  91. $date['start'] = $thisMonthStart;
  92. $date['end'] = $thisMonthEnd;
  93. break;
  94. case 'last_month':
  95. //上月初
  96. $lastMonthStart = date('Y-m-d', strtotime('-1 month', strtotime(date('Y-m', time()) . '-01')));
  97. // 上月底
  98. $lastMonthEnd = date('Y-m-d', strtotime(date('Y-m', time()) . '-01') - 86400);
  99. $date['start'] = $lastMonthStart;
  100. $date['end'] = $lastMonthEnd;
  101. break;
  102. default:
  103. break;
  104. }
  105. return $date;
  106. }
  107. }