85 lines
3.1 KiB
Java
85 lines
3.1 KiB
Java
package com.designhub.controller;
|
|
|
|
import com.designhub.common.Result;
|
|
import com.designhub.entity.CheckIn;
|
|
import com.designhub.service.CheckInService;
|
|
import com.designhub.util.JwtUtil;
|
|
import io.swagger.v3.oas.annotations.Operation;
|
|
import io.swagger.v3.oas.annotations.tags.Tag;
|
|
import lombok.RequiredArgsConstructor;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
import java.util.HashMap;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
|
|
/**
|
|
* 签到Controller
|
|
*/
|
|
@Slf4j
|
|
@Tag(name = "签到系统", description = "每日签到相关API")
|
|
@RestController
|
|
@RequestMapping("/api/check-in")
|
|
@RequiredArgsConstructor
|
|
public class CheckInController {
|
|
|
|
private final CheckInService checkInService;
|
|
private final JwtUtil jwtUtil;
|
|
|
|
@Operation(summary = "用户签到")
|
|
@PostMapping
|
|
public Result<CheckIn> checkIn(@RequestHeader("Authorization") String authHeader) {
|
|
String token = jwtUtil.extractTokenFromHeader(authHeader);
|
|
Long userId = jwtUtil.getUserIdFromToken(token);
|
|
if (userId == null) {
|
|
return Result.error(401, "未登录或登录已过期");
|
|
}
|
|
CheckIn checkIn = checkInService.checkIn(userId);
|
|
return Result.success(checkIn);
|
|
}
|
|
|
|
@Operation(summary = "检查今天是否已签到")
|
|
@GetMapping("/today")
|
|
public Result<Boolean> hasCheckedInToday(@RequestHeader("Authorization") String authHeader) {
|
|
String token = jwtUtil.extractTokenFromHeader(authHeader);
|
|
Long userId = jwtUtil.getUserIdFromToken(token);
|
|
if (userId == null) {
|
|
return Result.error(401, "未登录或登录已过期");
|
|
}
|
|
boolean checked = checkInService.hasCheckedInToday(userId);
|
|
return Result.success(checked);
|
|
}
|
|
|
|
@Operation(summary = "获取签到历史记录")
|
|
@GetMapping("/history")
|
|
public Result<List<CheckIn>> getCheckInHistory(
|
|
@RequestHeader("Authorization") String authHeader,
|
|
@RequestParam(defaultValue = "30") Integer limit
|
|
) {
|
|
String token = jwtUtil.extractTokenFromHeader(authHeader);
|
|
Long userId = jwtUtil.getUserIdFromToken(token);
|
|
if (userId == null) {
|
|
return Result.error(401, "未登录或登录已过期");
|
|
}
|
|
List<CheckIn> history = checkInService.getUserCheckInHistory(userId, limit);
|
|
return Result.success(history);
|
|
}
|
|
|
|
@Operation(summary = "获取签到统计信息")
|
|
@GetMapping("/stats")
|
|
public Result<Map<String, Object>> getCheckInStats(@RequestHeader("Authorization") String authHeader) {
|
|
String token = jwtUtil.extractTokenFromHeader(authHeader);
|
|
Long userId = jwtUtil.getUserIdFromToken(token);
|
|
if (userId == null) {
|
|
return Result.error(401, "未登录或登录已过期");
|
|
}
|
|
Map<String, Object> stats = new HashMap<>();
|
|
stats.put("continuousDays", checkInService.getContinuousDays(userId));
|
|
stats.put("totalDays", checkInService.getTotalCheckInDays(userId));
|
|
stats.put("hasCheckedInToday", checkInService.hasCheckedInToday(userId));
|
|
return Result.success(stats);
|
|
}
|
|
}
|
|
|