Access.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <?php
  2. namespace app\shop\controller\statistics;
  3. use app\shop\controller\Controller;
  4. use app\shop\service\statistics\AccessService;
  5. /**
  6. * 访问数据控制器
  7. */
  8. class Access extends Controller
  9. {
  10. /**
  11. * 会员数据统计
  12. */
  13. public function index()
  14. {
  15. return $this->renderSuccess('', [
  16. // 访问统计
  17. 'access' => (new AccessService())->getData(),
  18. ]);
  19. }
  20. /**
  21. * 通过时间段查询
  22. * $type类型:new refund
  23. */
  24. public function data($search_time, $type = 'visit')
  25. {
  26. $days = $this->getDays($search_time);
  27. $data = [];
  28. if($type == 'visit'){
  29. $data = (new AccessService())->getVisitDataByDate($days);
  30. }else if($type == 'fav'){
  31. $data = (new AccessService())->getFavDataByDate($days);
  32. }
  33. return $this->renderSuccess('', [
  34. // 日期
  35. 'days' => $days,
  36. // 数据
  37. 'data' => $data,
  38. ]);
  39. }
  40. /**
  41. * 获取具体日期数组
  42. */
  43. private function getDays($search_time)
  44. {
  45. //搜索时间段
  46. if(!isset($search_time) || empty($search_time)){
  47. //没有传,则默认为最近7天
  48. $end_time = date('Y-m-d', time());
  49. $start_time = date('Y-m-d', strtotime('-7 day',time()));
  50. }else{
  51. $start_time = array_shift($search_time);
  52. $end_time = array_pop($search_time);
  53. }
  54. $dt_start = strtotime($start_time);
  55. $dt_end = strtotime($end_time);
  56. $date = [];
  57. $date[] = date('Y-m-d', strtotime($start_time));
  58. while($dt_start < $dt_end) {
  59. $date[] = date('Y-m-d', strtotime('+1 day',$dt_start));
  60. $dt_start = strtotime('+1 day',$dt_start);
  61. }
  62. return $date;
  63. }
  64. }