|
@@ -1,12 +1,10 @@
|
|
|
import dbConnect from "../../lib/dbConnect";
|
|
import dbConnect from "../../lib/dbConnect";
|
|
|
import Match from "../../models/Match";
|
|
import Match from "../../models/Match";
|
|
|
import { NextResponse } from "next/server";
|
|
import { NextResponse } from "next/server";
|
|
|
|
|
+import { setCORSHeaders, handleError } from "../../lib/apiUtils";
|
|
|
|
|
|
|
|
export async function GET(request) {
|
|
export async function GET(request) {
|
|
|
- console.log("Database connected, starting query 444");
|
|
|
|
|
-
|
|
|
|
|
await dbConnect();
|
|
await dbConnect();
|
|
|
- console.log("Database connected, starting query 555");
|
|
|
|
|
|
|
|
|
|
try {
|
|
try {
|
|
|
const { searchParams } = new URL(request.url);
|
|
const { searchParams } = new URL(request.url);
|
|
@@ -56,7 +54,18 @@ export async function GET(request) {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- console.log("Search Query:", searchQuery); // 添加日志
|
|
|
|
|
|
|
+ console.log("Search Query:", searchQuery);
|
|
|
|
|
+
|
|
|
|
|
+ // 更新比赛状态
|
|
|
|
|
+ const now = new Date();
|
|
|
|
|
+ await Match.updateMany(
|
|
|
|
|
+ {
|
|
|
|
|
+ date: { $lt: now },
|
|
|
|
|
+ time: { $lt: now.toTimeString().slice(0, 5) },
|
|
|
|
|
+ status: "未开始",
|
|
|
|
|
+ },
|
|
|
|
|
+ { $set: { status: "进行中" } }
|
|
|
|
|
+ );
|
|
|
|
|
|
|
|
const skip = (current - 1) * pageSize;
|
|
const skip = (current - 1) * pageSize;
|
|
|
const totalMatches = await Match.countDocuments(searchQuery);
|
|
const totalMatches = await Match.countDocuments(searchQuery);
|
|
@@ -73,8 +82,19 @@ export async function GET(request) {
|
|
|
break;
|
|
break;
|
|
|
|
|
|
|
|
case "getMatchDays":
|
|
case "getMatchDays":
|
|
|
- const allMatches = await Match.find({}).sort("date");
|
|
|
|
|
- const formattedDays = formatMatches(allMatches);
|
|
|
|
|
|
|
+ // 获取当前日期
|
|
|
|
|
+ const currentDate = new Date();
|
|
|
|
|
+ currentDate.setHours(0, 0, 0, 0);
|
|
|
|
|
+
|
|
|
|
|
+ // 查询未来的未开始比赛
|
|
|
|
|
+ const futureMatches = await Match.find({
|
|
|
|
|
+ date: { $gte: currentDate },
|
|
|
|
|
+ status: "未开始",
|
|
|
|
|
+ }).sort("date");
|
|
|
|
|
+
|
|
|
|
|
+ // 使用原有的 formatMatches 函数格式化比赛
|
|
|
|
|
+ const formattedDays = formatMatches(futureMatches);
|
|
|
|
|
+
|
|
|
response = NextResponse.json({ success: true, data: formattedDays });
|
|
response = NextResponse.json({ success: true, data: formattedDays });
|
|
|
break;
|
|
break;
|
|
|
|
|
|
|
@@ -86,7 +106,28 @@ export async function GET(request) {
|
|
|
{ status: 400 }
|
|
{ status: 400 }
|
|
|
);
|
|
);
|
|
|
}
|
|
}
|
|
|
- const matches = await Match.find({ date: new Date(date) });
|
|
|
|
|
|
|
+
|
|
|
|
|
+ // 创建日期范围
|
|
|
|
|
+ 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 matches = await Match.find({
|
|
|
|
|
+ date: { $gte: startDate, $lte: endDate },
|
|
|
|
|
+ status: "未开始",
|
|
|
|
|
+ }).sort({ time: 1 });
|
|
|
|
|
+
|
|
|
response = NextResponse.json({ success: true, data: matches });
|
|
response = NextResponse.json({ success: true, data: matches });
|
|
|
break;
|
|
break;
|
|
|
|
|
|
|
@@ -233,43 +274,9 @@ export async function DELETE(request) {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-// Handle OPTIONS request for CORS preflight
|
|
|
|
|
export async function OPTIONS() {
|
|
export async function OPTIONS() {
|
|
|
const response = new NextResponse(null, { status: 204 });
|
|
const response = new NextResponse(null, { status: 204 });
|
|
|
- response.headers.set("Access-Control-Allow-Origin", "*");
|
|
|
|
|
- response.headers.set(
|
|
|
|
|
- "Access-Control-Allow-Methods",
|
|
|
|
|
- "GET, POST, PUT, DELETE, OPTIONS"
|
|
|
|
|
- );
|
|
|
|
|
- response.headers.set(
|
|
|
|
|
- "Access-Control-Allow-Headers",
|
|
|
|
|
- "Content-Type, Authorization"
|
|
|
|
|
- );
|
|
|
|
|
- response.headers.set("Access-Control-Max-Age", "86400"); // 24 hours
|
|
|
|
|
- return response;
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
-// Helper function to set CORS headers
|
|
|
|
|
-function setCORSHeaders(response) {
|
|
|
|
|
- response.headers.set("Access-Control-Allow-Origin", "*"); // 或者设置为特定域名
|
|
|
|
|
- response.headers.set(
|
|
|
|
|
- "Access-Control-Allow-Methods",
|
|
|
|
|
- "GET, POST, PUT, DELETE, OPTIONS"
|
|
|
|
|
- );
|
|
|
|
|
- response.headers.set(
|
|
|
|
|
- "Access-Control-Allow-Headers",
|
|
|
|
|
- "Content-Type, Authorization"
|
|
|
|
|
- );
|
|
|
|
|
- return response;
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
-// 共享的错误处理逻辑
|
|
|
|
|
-function handleError(error) {
|
|
|
|
|
- console.error("API错误:", error);
|
|
|
|
|
- return NextResponse.json(
|
|
|
|
|
- { success: false, error: error.message },
|
|
|
|
|
- { status: 500 }
|
|
|
|
|
- );
|
|
|
|
|
|
|
+ return setCORSHeaders(response);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
function formatMatches(matches) {
|
|
function formatMatches(matches) {
|
|
@@ -287,9 +294,22 @@ function formatMatches(matches) {
|
|
|
const month = String(date.getMonth() + 1).padStart(2, "0");
|
|
const month = String(date.getMonth() + 1).padStart(2, "0");
|
|
|
const day = String(date.getDate()).padStart(2, "0");
|
|
const day = String(date.getDate()).padStart(2, "0");
|
|
|
|
|
|
|
|
|
|
+ const weekdays = [
|
|
|
|
|
+ "星期日",
|
|
|
|
|
+ "星期一",
|
|
|
|
|
+ "星期二",
|
|
|
|
|
+ "星期三",
|
|
|
|
|
+ "星期四",
|
|
|
|
|
+ "星期五",
|
|
|
|
|
+ "星期六",
|
|
|
|
|
+ ];
|
|
|
|
|
+
|
|
|
|
|
+ // 获取星期几
|
|
|
|
|
+ const weekday = weekdays[date.getDay()];
|
|
|
|
|
+
|
|
|
return {
|
|
return {
|
|
|
id: index + 1,
|
|
id: index + 1,
|
|
|
- title: `比赛日${index + 1}`,
|
|
|
|
|
|
|
+ title: weekday,
|
|
|
date: `${year}-${month}-${day}`,
|
|
date: `${year}-${month}-${day}`,
|
|
|
};
|
|
};
|
|
|
});
|
|
});
|