From 5aa95121eadb7321a18c0c988a23bcacc12ae6ae Mon Sep 17 00:00:00 2001 From: xiaogg Date: Thu, 13 Feb 2025 17:48:01 +0800 Subject: [PATCH 1/2] =?UTF-8?q?deepseek=20=E4=BC=98=E5=8C=96=E4=BA=86?= =?UTF-8?q?=E4=B8=80=E4=B8=8B=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- include/dateapi.class.php | 188 ++++++++++++++++++++++++++------------ index.php | 8 +- 2 files changed, 133 insertions(+), 63 deletions(-) diff --git a/include/dateapi.class.php b/include/dateapi.class.php index ceec046..04b09d2 100644 --- a/include/dateapi.class.php +++ b/include/dateapi.class.php @@ -1,74 +1,144 @@ yeardate($year); - if($yeardata){ - $nowday=substr($day,4,4);$dayvalue=@$yeardata[$nowday]; - if(!empty($dayvalue) || is_numeric($dayvalue))$returndata= $dayvalue; - } - if(!is_numeric($returndata))$returndata=$this->checkwork($day); - if(empty($returndata))$returndata=0; - $return=array('status'=>1,'info'=>$returndata); - return $return; - } +class DateApi +{ + // 数据存储路径 + private $dataPath = 'data/'; + // 缓存数组 + private $cache = array(); /** * 获取一天的节假日情况 - * @param $time 日期时间戳 - * @return int 1 工作日 2调休(工作日) 3周未 4假日 + * @param string $day 日期如 20250101 或 2025-01-01 + * @param int $type 类型 1工资倍数【0:工作日 1:周未双倍工资 2:假日三倍工资】 2具体类型【1 工作日 2调休(工作日) 3周未 4假日】 + * @return array 返回结果数组 + */ + public function getday($day, $type = 1) + { + // 清理日期格式 + $day = str_replace('-', '', $day); + // 验证日期格式 + if (!$this->validateDate($day)) { + return array('status' => 0, 'info' => '日期格式错误'); + } + // 获取年份 + $year = substr($day, 0, 4); + // 获取年份数据 + $yearData = $this->getYearData($year); + if ($yearData === false) { + return array('status' => 0, 'info' => '数据获取失败'); + } + // 获取节假日信息 + $holidayData = $this->getHolidayInfo($day, $yearData); + // 根据类型返回结果 + if ($type == 2) { + $data = $this->getDetailType($day, $holidayData); + } else { + $data = $this->getWageMultiplier($day, $holidayData); + } + return array('status' => 1, 'info' => $data); + } + /** + * 检查是否是周末 + * @param string $day 日期 + * @return int 1是周末,0不是 + */ + private function checkWeekend($day) + { + $weekday = date('N', strtotime($day)); + return in_array($weekday, array(6, 7)) ? 1 : 0; + } + /** + * 验证日期格式 + * @param string $str 日期 + * @return bool 是否有效 + */ + public function validateDate($str) { + if(empty($str) || strlen($str)<8)return false; + // 尝试创建 DateTime 对象 + try { + new DateTime($str); + return true; + } catch (Exception $e) { + return false; + } + } + /** + * 获取一年的数据 + * @param string $year 年份 + * @return array|false 年份数据或false */ - public function getday2($time){ - $day=date('Y-m-d',$time);$dayarr=explode('-',$day);$year=$dayarr[0]; - $yeardata=$this->yeardate($year); - $returndata = ''; - if($yeardata){ - $nowday=$dayarr[1].$dayarr[2]; - if(is_numeric($yeardata[$nowday])){ - $returndata = $yeardata[$nowday]>0? 4:2; + private function getYearData($year) + { + // 检查缓存 + if (isset($this->cache[$year])) { + return $this->cache[$year]; + } + // 本地文件路径 + $filePath = $this->dataPath . $year . '_data.json'; + // 检查本地文件是否存在 + if (file_exists($filePath)) { + $data = json_decode(file_get_contents($filePath), true); + if ($data !== null) { + $this->cache[$year] = $data; + return $data; } } - if(empty($returndata)){ - $checkholidy=$this->checkwork($day); - $returndata =$checkholidy>0?3:1; + // 远程获取数据 + $url = 'https://gitee.com/web/holidays_api/raw/master/data/' . $year . '_data.json'; + $content = @file_get_contents($url); + if ($content !== false) { + $data = json_decode($content, true); + if ($data !== null) { + // 保存到本地 + file_put_contents($filePath, json_encode($data)); + $this->cache[$year] = $data; + return $data; + } } - return $returndata; - } - //检查是否是周未 - public function checkwork($day){ - $weak=date("N",strtotime($day)); - return in_array($weak,array(6,7))?1:0; - } - //检查是否为合法日期 - public function check($day){ - $return=array('status'=>0,'info'=>''); - if(empty($day) || strlen($day)<4){$return['info']='日期参数不正确';return $return;} - $day_time=strtotime($day);if(date('Ymd',$day_time)!=$day){$return['info']='日期格式错误';return $return;} - $return['status']=1; - return $return; + return false; } - //获取一年的数据 - private function yeardate($year=''){ - if(empty($year))$year=date('Y'); - static $cache = array();if(!empty($cache[$year])){return $cache[$year];} - $new_file=$this->path.$year.'_data.json'; - if(file_exists($new_file)){ - $cache[$year]=json_decode(file_get_contents($new_file),true); //读取年数据文件并 json转化为数组 - return $cache[$year]; - }else{ - $url = 'https://gitee.com/web/holidays_api/raw/master/data/'.$year.'_data.json'; - $content = file_get_contents($url); - if(!empty($content) && $jsondata = json_decode($content,true)){ - file_put_contents($new_file,json_encode($jsondata));//自动更新 需要目录有写入权限 + /** + * 获取节假日信息 + * @param string $day 日期 + * @param array $yearData 年份数据 + * @return int|null 节假日信息 + */ + private function getHolidayInfo($day, $yearData) + { + if ($yearData) { + $monthDay = substr($day, 4, 4); + if (isset($yearData[$monthDay]) && is_numeric($yearData[$monthDay])) { + return $yearData[$monthDay]; } - return false; - } + } + return null; + } + /** + * 获取详细类型 + * @param string $day 日期 + * @param mixed $holidayData 节假日数据 + * @return int 类型 + */ + private function getDetailType($day, $holidayData) + { + if (is_numeric($holidayData)) { + return $holidayData > 0 ? 4 : 2; + } + return $this->checkWeekend($day) ? 3 : 1; + } + /** + * 获取工资倍数 + * @param string $day 日期 + * @param mixed $holidayData 节假日数据 + * @return int 工资倍数 + */ + private function getWageMultiplier($day, $holidayData) + { + return is_numeric($holidayData) ? $holidayData : $this->checkWeekend($day); } } ?> \ No newline at end of file diff --git a/index.php b/index.php index 22ceb6f..6255691 100644 --- a/index.php +++ b/index.php @@ -11,10 +11,10 @@ if(empty($data['d'])){ }else{ header("content-Type: application/json; charset=utf-8"); include('include/dateapi.class.php');//创建实例 - $api= new dateapi(); - $check=$api->check($data['d']); - if($check['status']==0)ajaxReturn($result);//校验日期合法性 可不校验 - $result= $api->getday($data['d']); + $api= new DateApi(); + if(!$api->validateDate($data['d']))ajaxReturn(['status' => 0, 'info' => '日期格式错误']);//校验日期合法性 可不校验 + $type = !empty($data['type'])?$data['type']:1; + $result= $api->getday($data['d'],$type); ajaxReturn($result); } function ajaxReturn($array){ -- Gitee From 0e6b01a214c424fb7930d5bd2b12e3055cbcf0f2 Mon Sep 17 00:00:00 2001 From: xiaogg Date: Thu, 13 Feb 2025 18:04:53 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- include/dateapi.class.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/dateapi.class.php b/include/dateapi.class.php index 04b09d2..c25d53e 100644 --- a/include/dateapi.class.php +++ b/include/dateapi.class.php @@ -18,12 +18,12 @@ class DateApi */ public function getday($day, $type = 1) { - // 清理日期格式 - $day = str_replace('-', '', $day); // 验证日期格式 if (!$this->validateDate($day)) { return array('status' => 0, 'info' => '日期格式错误'); } + // 清理日期格式 + $day = str_replace('-', '', $day); // 获取年份 $year = substr($day, 0, 4); // 获取年份数据 -- Gitee