library-picturebook-activity/java-backend/src/main/java/com/lesingle/creation/controller/PublicController.java
En b805f456a6 feat: 完善后端基础架构和登录功能
- 添加 Lombok 配置支持
- 完善枚举类和常量定义
- 新增工具类(TraceId、限流、OSS 等)
- 添加切面(日志、限流、TraceId)
- 更新数据库索引规范(应用层防重)
- 登录页面样式优化
- 前后端项目文档补充
2026-03-31 13:58:28 +08:00

177 lines
6.9 KiB
Java

package com.lesingle.creation.controller;
import com.lesingle.creation.common.core.Result;
import com.lesingle.creation.common.security.UserPrincipal;
import com.lesingle.creation.dto.child.CreateChildDTO;
import com.lesingle.creation.dto.child.UpdateChildDTO;
import com.lesingle.creation.dto.publicuser.PublicLoginDTO;
import com.lesingle.creation.dto.publicuser.PublicRegisterDTO;
import com.lesingle.creation.dto.publicuser.PublicUserUpdateDTO;
import com.lesingle.creation.service.PublicService;
import com.lesingle.creation.vo.child.ChildVO;
import com.lesingle.creation.vo.publicuser.PublicUserVO;
import com.lesingle.creation.vo.publicuser.LoginResponseVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;
import java.util.List;
/**
* 公共接口控制器
* 提供公众用户注册、登录、个人信息、子女管理等功能
*/
@Tag(name = "公共接口", description = "公众用户相关接口")
@RestController
@RequestMapping("/api/public")
@RequiredArgsConstructor
public class PublicController {
private final PublicService publicService;
// ==================== 注册 & 登录(公开接口) ====================
@PostMapping("/auth/register")
@Operation(summary = "用户注册")
public Result<LoginResponseVO> register(@RequestBody @Valid PublicRegisterDTO dto) {
return Result.success(publicService.register(dto));
}
@PostMapping("/auth/login")
@Operation(summary = "用户登录")
public Result<LoginResponseVO> login(@RequestBody @Valid PublicLoginDTO dto) {
return Result.success(publicService.login(dto));
}
// ==================== 个人信息(需要登录) ====================
@GetMapping("/mine/profile")
@Operation(summary = "获取个人信息")
public Result<PublicUserVO> getProfile(@AuthenticationPrincipal UserPrincipal userPrincipal) {
Long userId = userPrincipal.getUserId();
return Result.success(publicService.getUserInfo(userId));
}
@PutMapping("/mine/profile")
@Operation(summary = "更新个人信息")
public Result<PublicUserVO> updateProfile(
@AuthenticationPrincipal UserPrincipal userPrincipal,
@RequestBody PublicUserUpdateDTO dto) {
Long userId = userPrincipal.getUserId();
return Result.success(publicService.updateProfile(userId, dto));
}
// ==================== 子女管理(需要登录) ====================
@GetMapping("/mine/children")
@Operation(summary = "获取子女列表")
public Result<List<ChildVO>> getChildren(@AuthenticationPrincipal UserPrincipal userPrincipal) {
Long userId = userPrincipal.getUserId();
return Result.success(publicService.getChildren(userId));
}
@PostMapping("/mine/children")
@Operation(summary = "创建子女")
public Result<ChildVO> createChild(
@AuthenticationPrincipal UserPrincipal userPrincipal,
@RequestBody @Valid CreateChildDTO dto) {
Long userId = userPrincipal.getUserId();
return Result.success(publicService.createChild(userId, dto));
}
@GetMapping("/mine/children/{id}")
@Operation(summary = "获取子女详情")
public Result<ChildVO> getChild(
@AuthenticationPrincipal UserPrincipal userPrincipal,
@PathVariable Long id) {
Long userId = userPrincipal.getUserId();
return Result.success(publicService.getChild(userId, id));
}
@PutMapping("/mine/children/{id}")
@Operation(summary = "更新子女信息")
public Result<ChildVO> updateChild(
@AuthenticationPrincipal UserPrincipal userPrincipal,
@PathVariable Long id,
@RequestBody UpdateChildDTO dto) {
Long userId = userPrincipal.getUserId();
return Result.success(publicService.updateChild(userId, id, dto));
}
@DeleteMapping("/mine/children/{id}")
@Operation(summary = "删除子女")
public Result<Void> deleteChild(
@AuthenticationPrincipal UserPrincipal userPrincipal,
@PathVariable Long id) {
Long userId = userPrincipal.getUserId();
publicService.deleteChild(userId, id);
return Result.success(null);
}
// ==================== 子女独立账号管理 ====================
@PostMapping("/children/create-account")
@Operation(summary = "创建子女独立账号")
public Result<ChildVO> createChildAccount(
@AuthenticationPrincipal UserPrincipal userPrincipal,
@RequestBody @Valid CreateChildDTO dto) {
Long userId = userPrincipal.getUserId();
return Result.success(publicService.createChildAccount(userId, dto));
}
@GetMapping("/children/accounts")
@Operation(summary = "获取子女账号列表")
public Result<List<ChildVO>> getChildAccounts(@AuthenticationPrincipal UserPrincipal userPrincipal) {
Long userId = userPrincipal.getUserId();
return Result.success(publicService.getChildAccounts(userId));
}
@PutMapping("/children/accounts/{id}")
@Operation(summary = "更新子女账号信息")
public Result<ChildVO> updateChildAccount(
@AuthenticationPrincipal UserPrincipal userPrincipal,
@PathVariable Long id,
@RequestBody UpdateChildDTO dto) {
Long userId = userPrincipal.getUserId();
return Result.success(publicService.updateChildAccount(userId, id, dto));
}
@GetMapping("/mine/parent-info")
@Operation(summary = "获取监护人信息")
public Result<PublicUserVO> getParentInfo(@AuthenticationPrincipal UserPrincipal userPrincipal) {
Long userId = userPrincipal.getUserId();
return Result.success(publicService.getParentInfo(userId));
}
// ==================== 活动浏览(公开接口) ====================
@GetMapping("/activities")
@Operation(summary = "获取活动列表")
public Result getActivities(
@RequestParam(defaultValue = "1") Integer page,
@RequestParam(defaultValue = "12") Integer pageSize,
@RequestParam(required = false) String keyword,
@RequestParam(required = false) String contestType,
@AuthenticationPrincipal UserPrincipal userPrincipal) {
Long userId = userPrincipal != null ? userPrincipal.getUserId() : null;
return Result.success(publicService.getPublicActivities(page, pageSize, keyword, contestType, userId));
}
@GetMapping("/activities/{id}")
@Operation(summary = "获取活动详情")
public Result getActivityDetail(@PathVariable Long id) {
return Result.success(publicService.getActivityDetail(id));
}
}