链街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.

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