113 lines
3.0 KiB
JavaScript
113 lines
3.0 KiB
JavaScript
import { NextResponse } from "next/server";
|
||
import dbConnect from "@/lib/mongodb";
|
||
import CheckIn from "@/models/CheckIn";
|
||
import User from "@/models/User";
|
||
import { verifyToken } from "@/lib/auth";
|
||
|
||
export async function POST(request) {
|
||
try {
|
||
const token = request.headers.get("authorization")?.split(" ")[1];
|
||
if (!token) {
|
||
return NextResponse.json({ error: "未登录" }, { status: 401 });
|
||
}
|
||
|
||
const decoded = verifyToken(token);
|
||
await dbConnect();
|
||
|
||
// 检查今天是否已经签到
|
||
const today = new Date();
|
||
today.setHours(0, 0, 0, 0);
|
||
|
||
const existingCheckIn = await CheckIn.findOne({
|
||
userId: decoded.userId,
|
||
date: {
|
||
$gte: today,
|
||
$lt: new Date(today.getTime() + 24 * 60 * 60 * 1000),
|
||
},
|
||
});
|
||
|
||
if (existingCheckIn) {
|
||
return NextResponse.json({ error: "今天已经签到过了" }, { status: 400 });
|
||
}
|
||
|
||
// 检查昨天是否签到,计算连续签到天数
|
||
const yesterday = new Date(today.getTime() - 24 * 60 * 60 * 1000);
|
||
const lastCheckIn = await CheckIn.findOne({
|
||
userId: decoded.userId,
|
||
date: {
|
||
$gte: yesterday,
|
||
$lt: today,
|
||
},
|
||
});
|
||
|
||
const streak = lastCheckIn ? lastCheckIn.streak + 1 : 1;
|
||
const points = Math.min(streak, 7); // 连续签到最多7天,每天获得对应天数的积分
|
||
|
||
// 创建新的签到记录
|
||
const checkIn = await CheckIn.create({
|
||
userId: decoded.userId,
|
||
date: today,
|
||
points,
|
||
streak,
|
||
});
|
||
|
||
// 更新用户的总积分
|
||
const user = await User.findById(decoded.userId);
|
||
user.points += points;
|
||
await user.save();
|
||
|
||
// 计算用户的总积分
|
||
const totalPoints = await CheckIn.aggregate([
|
||
{ $match: { userId: decoded.userId } },
|
||
{ $group: { _id: null, total: { $sum: "$points" } } },
|
||
]);
|
||
|
||
return NextResponse.json({
|
||
message: "签到成功",
|
||
points,
|
||
streak,
|
||
checkIn,
|
||
totalPoints: user.points,
|
||
});
|
||
} catch (error) {
|
||
return NextResponse.json({ error: "签到失败" }, { status: 500 });
|
||
}
|
||
}
|
||
|
||
export async function GET(request) {
|
||
try {
|
||
const token = request.headers.get("authorization")?.split(" ")[1];
|
||
if (!token) {
|
||
return NextResponse.json({ error: "未登录" }, { status: 401 });
|
||
}
|
||
|
||
const decoded = verifyToken(token);
|
||
await dbConnect();
|
||
|
||
// 获取用户的签到记录
|
||
const today = new Date();
|
||
today.setHours(0, 0, 0, 0);
|
||
|
||
const checkIns = await CheckIn.find({
|
||
userId: decoded.userId,
|
||
date: {
|
||
$gte: new Date(today.getTime() - 7 * 24 * 60 * 60 * 1000),
|
||
$lt: new Date(today.getTime() + 24 * 60 * 60 * 1000),
|
||
},
|
||
}).sort({ date: -1 });
|
||
|
||
// 计算总积分
|
||
const totalPoints = await CheckIn.aggregate([
|
||
{ $match: { userId: decoded.userId } },
|
||
{ $group: { _id: null, total: { $sum: "$points" } } },
|
||
]);
|
||
|
||
return NextResponse.json({
|
||
checkIns,
|
||
totalPoints: totalPoints[0]?.total || 0,
|
||
});
|
||
} catch (error) {
|
||
return NextResponse.json({ error: "获取签到记录失败" }, { status: 500 });
|
||
}
|
||
}
|