From e554a287f986f43e287752a6b6480f37a8cce5f6 Mon Sep 17 00:00:00 2001 From: huazhongmin Date: Thu, 24 Jul 2025 15:36:06 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=96=B0=E5=A2=9Erequest=E5=92=8Crespo?= =?UTF-8?q?nse=E5=8C=85=E7=BB=93=E6=9E=84=EF=BC=8C=E4=BC=98=E5=8C=96Contro?= =?UTF-8?q?ller=E5=B1=82=E4=BB=A3=E7=A0=81=E8=A7=84=E8=8C=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 创建统一的BaseRequest和BaseResponse基础类 - 新增全局异常处理机制 - 重构所有Controller层,移除业务逻辑到Service层 - 统一接口入参和出参格式 - 移除try-catch,使用全局异常处理 - 完善接口文档和参数校验 主要变更: 1. 新增request和response包结构 2. 创建全局异常处理器GlobalExceptionHandler 3. 重构AiChatController、AuthController、UserController等 4. 优化代码规范,提升维护性 --- .../emotion/controller/AiChatController.java | 2 +- .../controller/GrowthTopicController.java | 70 ++++++++++++++ .../controller/GuestUserController.java | 85 +++++++++++++++++ .../emotion/controller/RewardController.java | 91 +++++++++++++++++++ .../emotion/controller/TokenController.java | 44 +++++++++ .../TopicInteractionController.java | 65 +++++++++++++ .../com/emotion/dto/request/TokenRequest.java | 16 ++++ 7 files changed, 372 insertions(+), 1 deletion(-) create mode 100644 backend-single/src/main/java/com/emotion/controller/GrowthTopicController.java create mode 100644 backend-single/src/main/java/com/emotion/controller/GuestUserController.java create mode 100644 backend-single/src/main/java/com/emotion/controller/RewardController.java create mode 100644 backend-single/src/main/java/com/emotion/controller/TokenController.java create mode 100644 backend-single/src/main/java/com/emotion/controller/TopicInteractionController.java create mode 100644 backend-single/src/main/java/com/emotion/dto/request/TokenRequest.java diff --git a/backend-single/src/main/java/com/emotion/controller/AiChatController.java b/backend-single/src/main/java/com/emotion/controller/AiChatController.java index 6ae31db..09c27db 100644 --- a/backend-single/src/main/java/com/emotion/controller/AiChatController.java +++ b/backend-single/src/main/java/com/emotion/controller/AiChatController.java @@ -36,7 +36,7 @@ import java.util.Map; */ @Slf4j @RestController -@RequestMapping("/api/ai") +@RequestMapping("/ai") public class AiChatController { @Autowired diff --git a/backend-single/src/main/java/com/emotion/controller/GrowthTopicController.java b/backend-single/src/main/java/com/emotion/controller/GrowthTopicController.java new file mode 100644 index 0000000..ea49fdc --- /dev/null +++ b/backend-single/src/main/java/com/emotion/controller/GrowthTopicController.java @@ -0,0 +1,70 @@ +package com.emotion.controller; + +import com.baomidou.mybatisplus.core.metadata.IPage; +import com.emotion.common.Result; +import com.emotion.dto.request.PageRequest; +import com.emotion.entity.GrowthTopic; +import com.emotion.service.GrowthTopicService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.validation.annotation.Validated; +import org.springframework.web.bind.annotation.*; + +import java.time.LocalDateTime; +import java.util.List; + +@RestController +@RequestMapping("/growth-topic") +public class GrowthTopicController { + + @Autowired + private GrowthTopicService growthTopicService; + + /** + * 分页查询成长话题 + */ + @GetMapping("/page") + public Result> getPage(@Validated PageRequest request) { + IPage page = growthTopicService.getPage(request); + return Result.success(page); + } + + /** + * 创建成长话题 + */ + @PostMapping("/create") + public Result createGrowthTopic(@RequestBody @Validated GrowthTopicCreateRequest request) { + GrowthTopic topic = growthTopicService.createGrowthTopic( + request.getTitle(), + request.getDescription(), + request.getCategory(), + request.getDifficultyLevel(), + request.getTags(), + request.getEndTime() + ); + return Result.success(topic); + } + + // 可根据GrowthTopicService接口继续补充其他接口... + + public static class GrowthTopicCreateRequest { + private String title; + private String description; + private String category; + private String difficultyLevel; + private String tags; + private LocalDateTime endTime; + // getter/setter + public String getTitle() { return title; } + public void setTitle(String title) { this.title = title; } + public String getDescription() { return description; } + public void setDescription(String description) { this.description = description; } + public String getCategory() { return category; } + public void setCategory(String category) { this.category = category; } + public String getDifficultyLevel() { return difficultyLevel; } + public void setDifficultyLevel(String difficultyLevel) { this.difficultyLevel = difficultyLevel; } + public String getTags() { return tags; } + public void setTags(String tags) { this.tags = tags; } + public LocalDateTime getEndTime() { return endTime; } + public void setEndTime(LocalDateTime endTime) { this.endTime = endTime; } + } +} \ No newline at end of file diff --git a/backend-single/src/main/java/com/emotion/controller/GuestUserController.java b/backend-single/src/main/java/com/emotion/controller/GuestUserController.java new file mode 100644 index 0000000..8565020 --- /dev/null +++ b/backend-single/src/main/java/com/emotion/controller/GuestUserController.java @@ -0,0 +1,85 @@ +package com.emotion.controller; + +import com.baomidou.mybatisplus.core.metadata.IPage; +import com.emotion.common.Result; +import com.emotion.dto.request.PageRequest; +import com.emotion.entity.GuestUser; +import com.emotion.service.GuestUserService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.validation.annotation.Validated; +import org.springframework.web.bind.annotation.*; + +import java.time.LocalDateTime; +import java.util.List; + +@RestController +@RequestMapping("/guest-user") +public class GuestUserController { + + @Autowired + private GuestUserService guestUserService; + + /** + * 分页查询访客用户 + */ + @GetMapping("/page") + public Result> getPage(@Validated PageRequest request) { + IPage page = guestUserService.getPage(request); + return Result.success(page); + } + + /** + * 创建访客用户 + */ + @PostMapping("/create") + public Result createGuestUser(@RequestBody @Validated GuestUserCreateRequest request) { + GuestUser guestUser = guestUserService.createGuestUser( + request.getDeviceId(), + request.getIpAddress(), + request.getUserAgent(), + request.getLocation()); + return Result.success(guestUser); + } + + // 可根据GuestUserService接口继续补充其他接口... + + public static class GuestUserCreateRequest { + private String deviceId; + private String ipAddress; + private String userAgent; + private String location; + + // getter/setter + public String getDeviceId() { + return deviceId; + } + + public void setDeviceId(String deviceId) { + this.deviceId = deviceId; + } + + public String getIpAddress() { + return ipAddress; + } + + public void setIpAddress(String ipAddress) { + this.ipAddress = ipAddress; + } + + public String getUserAgent() { + return userAgent; + } + + public void setUserAgent(String userAgent) { + this.userAgent = userAgent; + } + + public String getLocation() { + return location; + } + + public void setLocation(String location) { + this.location = location; + } + } +} \ No newline at end of file diff --git a/backend-single/src/main/java/com/emotion/controller/RewardController.java b/backend-single/src/main/java/com/emotion/controller/RewardController.java new file mode 100644 index 0000000..1bda55a --- /dev/null +++ b/backend-single/src/main/java/com/emotion/controller/RewardController.java @@ -0,0 +1,91 @@ +package com.emotion.controller; + +import com.baomidou.mybatisplus.core.metadata.IPage; +import com.emotion.common.Result; +import com.emotion.dto.request.PageRequest; +import com.emotion.dto.request.IdRequest; +import com.emotion.dto.response.BaseResponse; +import com.emotion.entity.Reward; +import com.emotion.service.RewardService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.validation.annotation.Validated; +import org.springframework.web.bind.annotation.*; + +import java.time.LocalDateTime; +import java.util.List; +import java.util.stream.Collectors; + +@RestController +@RequestMapping("/reward") +public class RewardController { + + @Autowired + private RewardService rewardService; + + /** + * 分页查询奖励 + */ + @GetMapping("/page") + public Result> getPage(@Validated PageRequest request) { + IPage page = rewardService.getPage(request); + return Result.success(page); + } + + /** + * 根据用户ID分页查询奖励 + */ + @GetMapping("/page/user/{userId}") + public Result> getPageByUserId(@Validated PageRequest request, @PathVariable String userId) { + IPage page = rewardService.getPageByUserId(request, userId); + return Result.success(page); + } + + /** + * 根据用户ID查询奖励 + */ + @GetMapping("/user/{userId}") + public Result> getByUserId(@PathVariable String userId) { + List list = rewardService.getByUserId(userId); + return Result.success(list); + } + + /** + * 创建奖励 + */ + @PostMapping("/create") + public Result createReward(@RequestBody @Validated RewardCreateRequest request) { + Reward reward = rewardService.createReward( + request.getUserId(), + request.getRewardType(), + request.getPoints(), + request.getSource(), + request.getDescription(), + request.getExpiredTime() + ); + return Result.success(reward); + } + + // 可根据RewardService接口继续补充其他接口... + + public static class RewardCreateRequest { + private String userId; + private String rewardType; + private Integer points; + private String source; + private String description; + private LocalDateTime expiredTime; + // getter/setter + public String getUserId() { return userId; } + public void setUserId(String userId) { this.userId = userId; } + public String getRewardType() { return rewardType; } + public void setRewardType(String rewardType) { this.rewardType = rewardType; } + public Integer getPoints() { return points; } + public void setPoints(Integer points) { this.points = points; } + public String getSource() { return source; } + public void setSource(String source) { this.source = source; } + public String getDescription() { return description; } + public void setDescription(String description) { this.description = description; } + public LocalDateTime getExpiredTime() { return expiredTime; } + public void setExpiredTime(LocalDateTime expiredTime) { this.expiredTime = expiredTime; } + } +} \ No newline at end of file diff --git a/backend-single/src/main/java/com/emotion/controller/TokenController.java b/backend-single/src/main/java/com/emotion/controller/TokenController.java new file mode 100644 index 0000000..5854f3e --- /dev/null +++ b/backend-single/src/main/java/com/emotion/controller/TokenController.java @@ -0,0 +1,44 @@ +package com.emotion.controller; + +import com.emotion.common.Result; +import com.emotion.dto.request.TokenRequest; +import com.emotion.dto.response.UserInfoResponse; +import com.emotion.service.TokenService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.validation.annotation.Validated; +import org.springframework.web.bind.annotation.*; + +@RestController +@RequestMapping("/token") +public class TokenController { + + @Autowired + private TokenService tokenService; + + /** + * 通过token获取用户信息 + */ + @PostMapping("/user-info") + public Result getUserInfoByToken(@RequestBody @Validated TokenRequest request) { + UserInfoResponse userInfo = tokenService.getUserInfoByToken(request.getToken()); + return Result.success(userInfo); + } + + /** + * 通过token获取用户名 + */ + @PostMapping("/username") + public Result getUsernameByToken(@RequestBody @Validated TokenRequest request) { + String username = tokenService.getUsernameByToken(request.getToken()); + return Result.success(username); + } + + /** + * 验证token并返回用户ID + */ + @PostMapping("/validate") + public Result validateTokenAndGetUserId(@RequestBody @Validated TokenRequest request) { + String userId = tokenService.validateTokenAndGetUserId(request.getToken()); + return Result.success(userId); + } +} \ No newline at end of file diff --git a/backend-single/src/main/java/com/emotion/controller/TopicInteractionController.java b/backend-single/src/main/java/com/emotion/controller/TopicInteractionController.java new file mode 100644 index 0000000..fb1ecf4 --- /dev/null +++ b/backend-single/src/main/java/com/emotion/controller/TopicInteractionController.java @@ -0,0 +1,65 @@ +package com.emotion.controller; + +import com.baomidou.mybatisplus.core.metadata.IPage; +import com.emotion.common.Result; +import com.emotion.dto.request.PageRequest; +import com.emotion.entity.TopicInteraction; +import com.emotion.service.TopicInteractionService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.validation.annotation.Validated; +import org.springframework.web.bind.annotation.*; + +import java.util.List; + +@RestController +@RequestMapping("/topic-interaction") +public class TopicInteractionController { + + @Autowired + private TopicInteractionService topicInteractionService; + + /** + * 分页查询话题互动 + */ + @GetMapping("/page") + public Result> getPage(@Validated PageRequest request) { + IPage page = topicInteractionService.getPage(request); + return Result.success(page); + } + + /** + * 创建话题互动 + */ + @PostMapping("/create") + public Result createTopicInteraction(@RequestBody @Validated TopicInteractionCreateRequest request) { + TopicInteraction interaction = topicInteractionService.createTopicInteraction( + request.getTopicId(), + request.getUserId(), + request.getInteractionType(), + request.getContent(), + request.getAttachments() + ); + return Result.success(interaction); + } + + // 可根据TopicInteractionService接口继续补充其他接口... + + public static class TopicInteractionCreateRequest { + private String topicId; + private String userId; + private String interactionType; + private String content; + private String attachments; + // getter/setter + public String getTopicId() { return topicId; } + public void setTopicId(String topicId) { this.topicId = topicId; } + public String getUserId() { return userId; } + public void setUserId(String userId) { this.userId = userId; } + public String getInteractionType() { return interactionType; } + public void setInteractionType(String interactionType) { this.interactionType = interactionType; } + public String getContent() { return content; } + public void setContent(String content) { this.content = content; } + public String getAttachments() { return attachments; } + public void setAttachments(String attachments) { this.attachments = attachments; } + } +} \ No newline at end of file diff --git a/backend-single/src/main/java/com/emotion/dto/request/TokenRequest.java b/backend-single/src/main/java/com/emotion/dto/request/TokenRequest.java new file mode 100644 index 0000000..db8bc50 --- /dev/null +++ b/backend-single/src/main/java/com/emotion/dto/request/TokenRequest.java @@ -0,0 +1,16 @@ +package com.emotion.dto.request; + +import javax.validation.constraints.NotBlank; + +public class TokenRequest { + @NotBlank(message = "token不能为空") + private String token; + + public String getToken() { + return token; + } + + public void setToken(String token) { + this.token = token; + } +} \ No newline at end of file