CalendarService.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. namespace App\Service;
  3. use App\Model\CalendarYear;
  4. use App\Model\CalendarYearDays;
  5. use Illuminate\Support\Facades\DB;
  6. class CalendarService extends Service
  7. {
  8. public function createCalendar($year = 0){
  9. if(empty($year)) $year = date("Y");
  10. try {
  11. DB::beginTransaction();
  12. $year_model = CalendarYear::where('year',$year)->first();
  13. if(empty($year_model)){
  14. $year_model = new CalendarYear();
  15. list($status, $msg) = $this->getHoliday($year);
  16. $days_stamp = $this->generateTimestampsForYear($year,$msg);
  17. if(! empty($days_stamp)){
  18. //子表更新
  19. CalendarYearDays::insert($days_stamp);
  20. $year_model->is_update_holidays = 1;
  21. }
  22. //主表更新
  23. $year_model->year = $year;
  24. $year_model->save();
  25. }else{
  26. if($year_model->is_update_holidays > 0) return [false, '已更新' . $year . '年的节假日信息'];
  27. list($status, $msg) = $this->getHoliday($year);
  28. if(empty($msg)) return [false, '未获取到'. $year . '年的节假日信息'];
  29. CalendarYearDays::whereIn('time_stamp', array_keys($msg))->delete();
  30. CalendarYearDays::insert(array_values($msg));
  31. }
  32. DB::commit();
  33. }catch (\Throwable $exception){dd($exception->getMessage(),$exception->getLine());
  34. DB::rollBack();
  35. return [false, $exception->getMessage()];
  36. }
  37. return [true, ''];
  38. }
  39. function generateTimestampsForYear($year, $holidays){
  40. // 创建一个 DateTime 对象,设置为当年的第一天
  41. $date = new \DateTime("$year-01-01");
  42. //设置时间到午夜
  43. $date->setTime(0, 0, 0);
  44. // 获取当年最后一天
  45. $endOfYear = new \DateTime("$year-12-31");
  46. // 创建一个空数组来保存时间戳
  47. $timestamps = []; // 循环直到到达当年的最后一天
  48. while ($date <= $endOfYear){
  49. if(isset($holidays[$date->getTimestamp()])){
  50. $t = $holidays[$date->getTimestamp()] ?? [];
  51. $timestamps[] = [
  52. 'time_stamp' => $date->getTimestamp(),
  53. 'is_holiday' => $t['is_holiday'],
  54. 'holiday_title' => $t['holiday_title']
  55. ];
  56. }else{
  57. $timestamps[] = [
  58. 'time_stamp' => $date->getTimestamp(),
  59. 'is_holiday' => 0,
  60. 'holiday_title' => ''
  61. ];
  62. }
  63. $date->modify('+1 day');
  64. }
  65. return $timestamps;
  66. }
  67. function getHoliday($year){
  68. $return = [];
  69. $url = 'http://api.jiejiariapi.com/v1/holidays/' . $year;
  70. list($status, $msg) = $this->get_helper($url);
  71. if(! $status) return [false, $return];
  72. if(! is_array($msg) || $msg == 'Year not found') return [false, $return];
  73. foreach ($msg as $value){
  74. $time = strtotime($value['date'] . "00:00:00");
  75. $return[$time] = [
  76. 'time_stamp' => strtotime($value['date'] . "00:00:00"),
  77. 'is_holiday' => $value['isOffDay'] ? 1 : 0,
  78. 'holiday_title' => $value['name']
  79. ];
  80. }
  81. return [true, $return];
  82. }
  83. public function get_helper($url, $header=[], $timeout = 20){
  84. $ch = curl_init();
  85. curl_setopt_array($ch, array(
  86. CURLOPT_URL => $url,
  87. CURLOPT_RETURNTRANSFER => true,
  88. CURLOPT_ENCODING => '',
  89. CURLOPT_MAXREDIRS => 10,
  90. CURLOPT_TIMEOUT => $timeout,
  91. CURLOPT_FOLLOWLOCATION => true,
  92. CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  93. CURLOPT_CUSTOMREQUEST => 'GET',
  94. CURLOPT_SSL_VERIFYPEER => false,
  95. CURLOPT_HTTPHEADER => $header,
  96. ));
  97. $r = curl_exec($ch);
  98. if ($r === false) {
  99. // 获取错误号
  100. $errorNumber = curl_errno($ch);
  101. // 获取错误信息
  102. $errorMessage = curl_error($ch);
  103. $message = "cURL Error #{$errorNumber}: {$errorMessage}";
  104. return [false, $message];
  105. }
  106. curl_close($ch);
  107. $return = json_decode($r, true);
  108. return [true, $return];
  109. }
  110. }