优化处理

This commit is contained in:
2025-10-26 16:59:50 +08:00
parent fdac026720
commit 2e243c7671
45 changed files with 346 additions and 3757 deletions
+2 -2
View File
@@ -5,11 +5,11 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.emotion</groupId>
<artifactId>emotion-single</artifactId>
<artifactId>backend-single</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging>
<name>emotion-single</name>
<name>backend-single</name>
<description>情感博物馆单体服务</description>
<properties>
@@ -23,7 +23,7 @@ public class EmotionSimpleApplication {
System.out.println("========================================");
System.out.println("🎉 情感博物馆服务启动成功!");
System.out.println("📋 服务信息:");
System.out.println(" - 服务名称: emotion-single");
System.out.println(" - 服务名称: backend-single");
System.out.println(" - 服务端口: 19089");
System.out.println(" - 环境配置: " + System.getProperty("spring.profiles.active"));
System.out.println(" - API文档: http://localhost:19089/api/health");
@@ -20,17 +20,18 @@ public class WebMvcConfig implements WebMvcConfigurer {
@Override
public void addInterceptors(InterceptorRegistry registry) {
// 注意:已配置 server.servlet.context-path=/api,拦截器路径匹配不需要再带 /api 前缀
registry.addInterceptor(jwtAuthInterceptor)
.addPathPatterns("/api/**") // 拦截所有API请求
.addPathPatterns("/**") // 拦截应用内的所有请求(已去掉 /api 前缀)
.excludePathPatterns(
"/api/auth/login", // 登录接口
"/api/auth/register", // 注册接口
"/api/auth/captcha", // 图形验证码接口
"/api/auth/sms-code", // 短信验证码接口(免登录)
"/api/auth/refresh-token", // 刷新token接口
"/api/auth/resetPassword", // 重置密码接口(免登录)
"/api/health", // 健康检查接口
"/api/ws/**", // WebSocket接口
"/auth/login", // 登录接口
"/auth/register", // 注册接口
"/auth/captcha", // 图形验证码接口
"/auth/sms-code", // 短信验证码接口(免登录)
"/auth/refresh-token", // 刷新token接口
"/auth/resetPassword", // 重置密码接口(免登录)
"/health", // 健康检查接口
"/ws/**", // WebSocket接口
"/swagger-ui/**", // Swagger UI
"/v3/api-docs/**", // API文档
"/actuator/**" // 监控端点
@@ -28,7 +28,7 @@ public class HealthController {
log.info("健康检查请求");
Map<String, Object> response = new HashMap<>();
response.put("service", "emotion-single");
response.put("service", "backend-single");
response.put("message", "情感博物馆单体服务运行正常");
response.put("version", "1.0.0");
response.put("status", "UP");
@@ -45,7 +45,7 @@ public class HealthController {
log.info("服务信息请求");
Map<String, Object> response = new HashMap<>();
response.put("service", "emotion-single");
response.put("service", "backend-single");
response.put("description", "情感博物馆单体服务");
response.put("version", "1.0.0");
response.put("author", "emotion-museum");
@@ -0,0 +1,42 @@
package com.emotion.dto.request;
import lombok.Data;
import lombok.EqualsAndHashCode;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;
/**
* 重置密码请求对象
* 用于通过手机号+验证码重置用户登录密码(验证码本期固定为 123456)
*
* 作者: emotion-museum
* 日期: 2025-10-26
* 版本: 1.0
*/
@Data
@EqualsAndHashCode(callSuper = true)
public class ResetPasswordRequest extends BaseRequest {
/**
* 手机号
*/
@NotBlank(message = "手机号不能为空")
@Pattern(regexp = "^1[3-9]\\d{9}$", message = "手机号格式不正确")
private String phone;
/**
* 新密码(6-20位)
*/
@NotBlank(message = "新密码不能为空")
@Size(min = 6, max = 20, message = "新密码长度必须在6-20个字符之间")
private String newPassword;
/**
* 验证码(本期固定为 123456)
*/
@NotBlank(message = "验证码不能为空")
private String captcha;
}
@@ -0,0 +1,21 @@
package com.emotion.dto.response;
import lombok.Data;
/**
* 重置密码响应对象
*
* 作者: emotion-museum
* 日期: 2025-10-26
* 版本: 1.0
*/
@Data
public class ResetPasswordResponse {
/** 是否成功 */
private boolean success;
/** 提示信息 */
private String message;
}
@@ -3,14 +3,10 @@ package com.emotion.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.emotion.entity.Message;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import java.time.LocalDateTime;
import java.util.List;
/**
* 消息Mapper接口
* 说明:统一使用 MyBatis-Plus 的 BaseMapper 与 Service 层的 LambdaQueryWrapper 构建条件,
*
* @author emotion-museum
* @date 2025-07-23
@@ -18,66 +14,4 @@ import java.util.List;
@Mapper
public interface MessageMapper extends BaseMapper<Message> {
/**
* 根据用户ID和时间范围查询消息
* 通过conversation表关联查询
*/
@Select("SELECT m.* FROM message m " +
"INNER JOIN conversation c ON m.conversation_id = c.id " +
"WHERE c.user_id = #{userId} " +
"AND m.create_time BETWEEN #{startTime} AND #{endTime} " +
"AND m.is_deleted = 0 " +
"ORDER BY m.create_time ASC")
List<Message> getByUserIdAndTimeRange(@Param("userId") String userId,
@Param("startTime") LocalDateTime startTime,
@Param("endTime") LocalDateTime endTime);
/**
* 根据用户ID分页查询消息
* 通过conversation表关联查询
*/
@Select("SELECT m.* FROM message m " +
"INNER JOIN conversation c ON m.conversation_id = c.id " +
"WHERE c.user_id = #{userId} " +
"AND m.is_deleted = 0 " +
"ORDER BY m.create_time DESC " +
"LIMIT #{offset}, #{size}")
List<Message> getByUserIdWithPageList(@Param("userId") String userId,
@Param("offset") Integer offset,
@Param("size") Integer size);
/**
* 统计用户消息总数
*/
@Select("SELECT COUNT(*) FROM message m " +
"INNER JOIN conversation c ON m.conversation_id = c.id " +
"WHERE c.user_id = #{userId} " +
"AND m.is_deleted = 0")
Long countByUserId(@Param("userId") String userId);
/**
* 根据用户ID和关键词搜索消息
*/
@Select("SELECT m.* FROM message m " +
"INNER JOIN conversation c ON m.conversation_id = c.id " +
"WHERE c.user_id = #{userId} " +
"AND m.content LIKE CONCAT('%', #{keyword}, '%') " +
"AND m.is_deleted = 0 " +
"ORDER BY m.create_time DESC " +
"LIMIT #{limit}")
List<Message> searchByUserIdAndKeyword(@Param("userId") String userId,
@Param("keyword") String keyword,
@Param("limit") Integer limit);
/**
* 根据用户ID获取最近的消息
*/
@Select("SELECT m.* FROM message m " +
"INNER JOIN conversation c ON m.conversation_id = c.id " +
"WHERE c.user_id = #{userId} " +
"AND m.is_deleted = 0 " +
"ORDER BY m.create_time DESC " +
"LIMIT #{limit}")
List<Message> getRecentByUserId(@Param("userId") String userId,
@Param("limit") Integer limit);
}
@@ -195,35 +195,49 @@ public class MessageServiceImpl extends ServiceImpl<MessageMapper, Message> impl
@Override
public List<Message> getByUserIdAndTimeRange(String userId, LocalDateTime startTime, LocalDateTime endTime) {
// 由于Message表没有直接的userId字段,需要通过conversation表关联查询
// 这里先通过conversationService获取用户的所有对话ID,然后查询这些对话的消息
return this.baseMapper.getByUserIdAndTimeRange(userId, startTime, endTime);
// 使用 MyBatis-Plus 条件构造器,直接根据消息表的 user_id 字段查询
LambdaQueryWrapper<Message> wrapper = new LambdaQueryWrapper<>();
wrapper.eq(Message::getUserId, userId)
.between(Message::getCreateTime, startTime, endTime)
.eq(Message::getIsDeleted, 0)
.orderByAsc(Message::getCreateTime);
return this.list(wrapper);
}
@Override
public IPage<Message> getByUserIdWithPage(String userId, Integer current, Integer size) {
// 手动实现分页
Integer offset = (current - 1) * size;
List<Message> records = this.baseMapper.getByUserIdWithPageList(userId, offset, size);
Long total = this.baseMapper.countByUserId(userId);
// 使用 MyBatis-Plus 分页 + 条件构造器
Page<Message> page = new Page<>(current, size);
page.setRecords(records);
page.setTotal(total);
return page;
LambdaQueryWrapper<Message> wrapper = new LambdaQueryWrapper<>();
wrapper.eq(Message::getUserId, userId)
.eq(Message::getIsDeleted, 0)
.orderByDesc(Message::getCreateTime);
return this.page(page, wrapper);
}
@Override
public List<Message> searchByUserIdAndKeyword(String userId, String keyword, Integer limit) {
// 通过conversation表关联查询用户的消息,根据关键词搜索
return this.baseMapper.searchByUserIdAndKeyword(userId, keyword, limit);
// 使用 MyBatis-Plus 分页 + 条件构造器,避免硬编码 SQL
Page<Message> page = new Page<>(1, limit);
LambdaQueryWrapper<Message> wrapper = new LambdaQueryWrapper<>();
wrapper.eq(Message::getUserId, userId)
.eq(Message::getIsDeleted, 0)
.like(StringUtils.hasText(keyword), Message::getContent, keyword)
.orderByDesc(Message::getCreateTime);
IPage<Message> result = this.page(page, wrapper);
return result.getRecords();
}
@Override
public List<Message> getRecentByUserId(String userId, Integer limit) {
// 获取用户最近消息,按时间倒序
return this.baseMapper.getRecentByUserId(userId, limit);
// 使用 MyBatis-Plus 分页查询最近消息
Page<Message> page = new Page<>(1, limit);
LambdaQueryWrapper<Message> wrapper = new LambdaQueryWrapper<>();
wrapper.eq(Message::getUserId, userId)
.eq(Message::getIsDeleted, 0)
.orderByDesc(Message::getCreateTime);
IPage<Message> result = this.page(page, wrapper);
return result.getRecords();
}
@Override