From 16cde022f33171661c292c33c38ce71df4e0d70b Mon Sep 17 00:00:00 2001 From: cody <648753004@qq.com> Date: Fri, 3 Apr 2026 17:39:34 +0800 Subject: [PATCH] update --- .../Controllers/Mobile/CourseController.php | 49 ++++++++++++++----- 1 file changed, 37 insertions(+), 12 deletions(-) diff --git a/app/Http/Controllers/Mobile/CourseController.php b/app/Http/Controllers/Mobile/CourseController.php index 7e91b2c..84b4f25 100755 --- a/app/Http/Controllers/Mobile/CourseController.php +++ b/app/Http/Controllers/Mobile/CourseController.php @@ -1077,27 +1077,35 @@ class CourseController extends CommonController if ($validator->fails()) { return $this->fail([ResponseCode::ERROR_PARAMETER, implode(',', $validator->errors()->all())]); } + $monthTs = strtotime($all['month'] . '-01'); + if ($monthTs === false) { + return $this->fail([ResponseCode::ERROR_PARAMETER, '月份格式错误']); + } + $monthStart = date('Y-m-01 00:00:00', $monthTs); + $monthEnd = date('Y-m-t 23:59:59', $monthTs); + $list = Calendar::with('course', 'courseContent') ->where(function ($query) use ($all) { if (isset($all['type'])) { $query->where('type', $all['type']); } - })->where('start_time', 'like', '%' . $all['month'] . '%') + })->where(function ($query) use ($monthStart, $monthEnd) { + $query->whereBetween('start_time', [$monthStart, $monthEnd]) + ->orWhereBetween('end_time', [$monthStart, $monthEnd]) + ->orWhere(function ($sub) use ($monthStart, $monthEnd) { + $sub->where('start_time', '<=', $monthStart) + ->where('end_time', '>=', $monthEnd); + }); + }) ->where('is_publish', 1) ->orderBy('start_time', 'asc') ->get(); $list->each(function ($calendar) { - if (!empty($calendar->course)) { - $calendar->course->start_time = $this->formatDateTimeWithSeconds($calendar->course->start_time ?? null); - $calendar->course->end_time = $this->formatDateTimeWithSeconds($calendar->course->end_time ?? null); - } - if (!empty($calendar->courseContent)) { - $calendar->courseContent->each(function ($content) { - $content->start_time = $this->formatDateTimeWithSeconds($content->start_time ?? null); - $content->end_time = $this->formatDateTimeWithSeconds($content->end_time ?? null); - }); - } + $rawStartTime = $calendar->start_time ?? null; + $rawEndTime = $calendar->end_time ?? null; + $calendar->start_time = $this->normalizeCalendarDateTime($rawStartTime, $rawEndTime, true); + $calendar->end_time = $this->normalizeCalendarDateTime($rawStartTime, $rawEndTime, false); }); return $this->success($list); @@ -1112,7 +1120,24 @@ class CourseController extends CommonController if ($timestamp === false) { return $value; } - return date('Y-m-d 00:00:00', $timestamp); + return date('Y-m-d H:i:s', $timestamp); + } + + private function normalizeCalendarDateTime($startValue, $endValue, $isStart = true) + { + $start = $this->formatDateTimeWithSeconds($startValue); + $end = $this->formatDateTimeWithSeconds($endValue); + $target = $isStart ? $start : $end; + if (empty($target)) { + return $target; + } + + // 跨天事件统一按“日期维度”返回,避免前端以时分秒切段时漏首日 + if (!empty($start) && !empty($end) && date('Y-m-d', strtotime($start)) !== date('Y-m-d', strtotime($end))) { + return date('Y-m-d 00:00:00', strtotime($target)); + } + + return $target; } }