HolidayController.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. namespace App\Http\Controllers\Api\Third;
  3. use App\Http\Controllers\Api\ApiBaseController;
  4. use App\Services\Common\UploadService;
  5. use App\Validators\UploadValidatorRequest;
  6. use Illuminate\Support\Facades\Cache;
  7. use Illuminate\Support\Facades\DB;
  8. class HolidayController extends ApiBaseController
  9. {
  10. /**
  11. * 法定节假日
  12. */
  13. public function index($date)
  14. {
  15. $year = 0;
  16. $month = 0;
  17. $day = 0;
  18. if (strpos($date, '-') !== false) {
  19. $arr = explode('-', $date);
  20. $year = $arr[0];
  21. $month = $arr[1];
  22. $day = array_get($arr,2,0);
  23. }
  24. $where = ['year' => $year];
  25. if (!empty($month)) {
  26. $where['month'] = $month;
  27. }
  28. if (!empty($day)) {
  29. $where['day'] = $day;
  30. }
  31. $list = DB::table('holiday')->where($where)->get();
  32. $data = [];
  33. if (empty($day)) {
  34. foreach ($list as $v) {
  35. $data[] = [
  36. 'holiday' => $v->is_holiday == 1,
  37. 'date' => "{$v->year}-{$v->month}-{$v->day}",
  38. ];
  39. }
  40. } else {
  41. if ($list->isEmpty()) {
  42. $week = date('w', strtotime($date));
  43. $data['holiday'] = ($week == 0 || $week == 6);
  44. } else {
  45. $data['holiday'] = $list[0]->is_holiday == 1;
  46. }
  47. }
  48. return $this->sendSuccessResponse($data);
  49. }
  50. public function get($year)
  51. {
  52. if (strlen($year) != 4) {
  53. return $this->sendErrorResponse('请输入四位数的年份');
  54. }
  55. $info = DB::table('holiday')->where('year', $year)->first();
  56. if (!empty($info)) {
  57. return $this->sendErrorResponse('请勿重复拉数据!');
  58. }
  59. $data = file_get_contents('http://timor.tech/api/holiday/year/' . $year);
  60. $data = json_decode($data, true);
  61. $list = [];
  62. foreach ($data['holiday'] as $v) {
  63. $date = explode('-', $v['date']);
  64. $list[] = [
  65. 'is_holiday' => $v['holiday'] ? 1 : 2,
  66. 'year' => $date[0],
  67. 'month' => $date[1],
  68. 'day' => $date[2],
  69. ];
  70. }
  71. DB::table('holiday')->insert($list);
  72. return $this->sendSuccessResponse();
  73. }
  74. }