|
@@ -2,6 +2,10 @@ import dbConnect from "../../lib/dbConnect";
|
|
|
import Match from "../../models/Match";
|
|
|
import { NextResponse } from "next/server";
|
|
|
import { setCORSHeaders, handleError } from "../../lib/apiUtils";
|
|
|
+import moment from "moment-timezone/moment-timezone-utils";
|
|
|
+
|
|
|
+// 假设你的服务器时区是 'Asia/Shanghai'
|
|
|
+const timezone = "Asia/Shanghai";
|
|
|
|
|
|
export async function GET(request) {
|
|
|
await dbConnect();
|
|
@@ -15,6 +19,10 @@ export async function GET(request) {
|
|
|
|
|
|
let response;
|
|
|
|
|
|
+ const now = moment.tz(timezone);
|
|
|
+ const currentDate = now.toDate(); // 当前日期,包含时间
|
|
|
+ const currentTimeString = now.format("HH:mm"); // 当前时间字符串
|
|
|
+
|
|
|
switch (action) {
|
|
|
case "getMatches":
|
|
|
const searchQuery = {};
|
|
@@ -57,11 +65,10 @@ export async function GET(request) {
|
|
|
console.log("Search Query:", searchQuery);
|
|
|
|
|
|
// 更新比赛状态
|
|
|
- const now = new Date();
|
|
|
await Match.updateMany(
|
|
|
{
|
|
|
- date: { $lt: now },
|
|
|
- time: { $lt: now.toTimeString().slice(0, 5) },
|
|
|
+ date: { $lte: currentDate }, // 日期小于或等于当前日期
|
|
|
+ time: { $lte: currentTimeString }, // 时间小于或等于当前时间
|
|
|
status: "未开始",
|
|
|
},
|
|
|
{ $set: { status: "进行中" } }
|
|
@@ -82,13 +89,12 @@ export async function GET(request) {
|
|
|
break;
|
|
|
|
|
|
case "getMatchDays":
|
|
|
- // 获取当前日期
|
|
|
- const currentDate = new Date();
|
|
|
- currentDate.setHours(0, 0, 0, 0);
|
|
|
+ // 获取当前日期(使用指定时区)
|
|
|
+ const currentDate = moment().tz(timezone).startOf("day");
|
|
|
|
|
|
// 查询未来的未开始比赛
|
|
|
const futureMatches = await Match.find({
|
|
|
- date: { $gte: currentDate },
|
|
|
+ date: { $gte: currentDate.toDate() },
|
|
|
status: "未开始",
|
|
|
}).sort("date");
|
|
|
|
|
@@ -108,20 +114,8 @@ export async function GET(request) {
|
|
|
}
|
|
|
|
|
|
// 创建日期范围
|
|
|
- const startDate = new Date(date);
|
|
|
- startDate.setHours(0, 0, 0, 0);
|
|
|
- const endDate = new Date(date);
|
|
|
- endDate.setHours(23, 59, 59, 999);
|
|
|
-
|
|
|
- // 更新比赛状态
|
|
|
- await Match.updateMany(
|
|
|
- {
|
|
|
- date: { $lt: new Date() },
|
|
|
- time: { $lt: new Date().toTimeString().slice(0, 5) },
|
|
|
- status: "未开始",
|
|
|
- },
|
|
|
- { $set: { status: "进行中" } }
|
|
|
- );
|
|
|
+ const startDate = moment.tz(date, timezone).startOf("day").toDate();
|
|
|
+ const endDate = moment.tz(date, timezone).endOf("day").toDate();
|
|
|
|
|
|
const matches = await Match.find({
|
|
|
date: { $gte: startDate, $lte: endDate },
|