diff --git a/src/main/java/com/bao/dating/DatingApplication.java b/src/main/java/com/bao/dating/DatingApplication.java index 6295922..9cc225c 100644 --- a/src/main/java/com/bao/dating/DatingApplication.java +++ b/src/main/java/com/bao/dating/DatingApplication.java @@ -3,9 +3,11 @@ package com.bao.dating; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.scheduling.annotation.EnableScheduling; @MapperScan("com.bao.dating.mapper") @SpringBootApplication +@EnableScheduling public class DatingApplication { public static void main(String[] args) { SpringApplication.run(DatingApplication.class, args); diff --git a/src/main/java/com/bao/dating/config/RedisConfig.java b/src/main/java/com/bao/dating/config/RedisConfig.java new file mode 100644 index 0000000..cf68a8a --- /dev/null +++ b/src/main/java/com/bao/dating/config/RedisConfig.java @@ -0,0 +1,29 @@ +package com.bao.dating.config; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.data.redis.connection.RedisConnectionFactory; +import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer; +import org.springframework.data.redis.serializer.StringRedisSerializer; + +@Configuration +public class RedisConfig { + + @Bean + public RedisTemplate redisTemplate(RedisConnectionFactory redisConnectionFactory) { + RedisTemplate redisTemplate = new RedisTemplate<>(); + redisTemplate.setConnectionFactory(redisConnectionFactory); + + // 设置key的序列化器 + redisTemplate.setKeySerializer(new StringRedisSerializer()); + redisTemplate.setHashKeySerializer(new StringRedisSerializer()); + + // 设置value的序列化器 + redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer()); + redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer()); + + redisTemplate.afterPropertiesSet(); + return redisTemplate; + } +} \ No newline at end of file diff --git a/src/main/java/com/bao/dating/config/WebConfig.java b/src/main/java/com/bao/dating/config/WebConfig.java index 98a0683..c99157f 100644 --- a/src/main/java/com/bao/dating/config/WebConfig.java +++ b/src/main/java/com/bao/dating/config/WebConfig.java @@ -29,7 +29,8 @@ public class WebConfig implements WebMvcConfigurer { .addPathPatterns("/**") // 忽略的接口 .excludePathPatterns( - "/user/login" + "/user/login", + "/api/verification/send-email-code" ); } } diff --git a/src/main/java/com/bao/dating/controller/ContactsController.java b/src/main/java/com/bao/dating/controller/ContactsController.java new file mode 100644 index 0000000..5aba7b6 --- /dev/null +++ b/src/main/java/com/bao/dating/controller/ContactsController.java @@ -0,0 +1,45 @@ +package com.bao.dating.controller; + +import com.bao.dating.context.UserContext; +import com.bao.dating.service.ContactsService; + +import org.springframework.web.bind.annotation.*; + +import javax.annotation.Resource; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +@RestController +public class ContactsController { + @Resource + private ContactsService contactsService; + + /** + * 查询好友列表 + * @return 响应结果 + */ + @GetMapping("/friends") + public Map getFriends() { + + // 从UserContext获取当前用户ID + Long userId = UserContext.getUserId(); + if (userId == null) { + Map error = new HashMap<>(); + error.put("code", 401); + error.put("msg", "用户未授权"); + return error; + } + + // 查询好友列表 + List> friends = contactsService.getFriendsByUserId(userId); + + // 构造响应 + Map result = new HashMap<>(); + result.put("code", 200); + result.put("msg", "查询成功"); + result.put("data", friends); + return result; + } +} diff --git a/src/main/java/com/bao/dating/controller/FriendController.java b/src/main/java/com/bao/dating/controller/FriendController.java new file mode 100644 index 0000000..7cd9373 --- /dev/null +++ b/src/main/java/com/bao/dating/controller/FriendController.java @@ -0,0 +1,122 @@ +package com.bao.dating.controller; + +import com.bao.dating.context.UserContext; +import com.bao.dating.service.FriendRelationService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.*; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +@RestController +@RequestMapping("/friend-relation") +public class FriendController { + @Autowired + private FriendRelationService friendRelationService; + + /** + * 发送添加好友申请(写入数据库申请表) + * @param targetUserId 被申请人ID + * @param greeting 打招呼内容 + * @return 操作结果 + */ + @PostMapping("/apply") + public Map sendFriendApply( + @RequestParam("targetUserId") Long targetUserId, + @RequestParam(value = "greeting", defaultValue = "你好,加个好友吧") String greeting) { + Map result = new HashMap<>(); + try { + Long applyUserId = UserContext.getUserId(); + if (applyUserId == null) { + result.put("code", 401); + result.put("msg", "用户未登录"); + return result; + } + friendRelationService.addFriendApply(applyUserId, targetUserId, greeting); + result.put("code", 200); + result.put("msg", "好友申请发送成功"); + } catch (Exception e) { + result.put("code", 500); + result.put("msg", "发送失败:" + e.getMessage()); + } + return result; + } + + /** + * 同意好友申请 + * @param applyUserId 申请人ID + * @param targetUserId 被申请人ID(当前登录用户) + * @param contactNickname 给申请人的备注名 + * @return 操作结果 + */ + @PostMapping("/agree") + public Map agreeFriendApply( + @RequestParam("applyUserId") Long applyUserId, + @RequestParam("targetUserId") Long targetUserId, + @RequestParam(value = "contactNickname", required = false) String contactNickname) { + Map result = new HashMap<>(); + try { + // 1. 同意申请(更新申请状态+删除/标记) + friendRelationService.agreeFriendApply(applyUserId, targetUserId); + // 2. 保存好友关系到通讯录表 + friendRelationService.addFriendRelation(targetUserId, applyUserId, contactNickname); + friendRelationService.addFriendRelation(applyUserId, targetUserId, null); + result.put("code", 200); + result.put("msg", "同意好友申请成功"); + } catch (Exception e) { + result.put("code", 500); + result.put("msg", "同意失败:" + e.getMessage()); + } + return result; + } + + /** + * 查询待处理的好友申请 + * @return 申请列表 + */ + @GetMapping("/apply/list") + public Map getApplyList() { + Map result = new HashMap<>(); + try { + Long targetUserId = UserContext.getUserId(); + if (targetUserId == null) { + result.put("code", 401); + result.put("msg", "用户未登录"); + return result; + } + List> applyList = friendRelationService.getFriendApplyList(targetUserId); + result.put("code", 200); + result.put("msg", "查询成功"); + result.put("data", applyList); + } catch (Exception e) { + result.put("code", 500); + result.put("msg", "查询失败:" + e.getMessage()); + } + return result; + } + + /** + * 测试查询联系人 + * @return 联系人列表 + */ + @GetMapping("/list") + public Map getFriendRelationList() { + Map result = new HashMap<>(); + try { + Long userId = UserContext.getUserId(); + if (userId == null) { + result.put("code", 401); + result.put("msg", "用户未登录"); + return result; + } + result.put("code", 200); + result.put("msg", "查询成功"); + result.put("data", friendRelationService.getFriendRelationList(userId)); + } catch (Exception e) { + result.put("code", 500); + result.put("msg", "查询失败:" + e.getMessage()); + } + return result; + } +} \ No newline at end of file diff --git a/src/main/java/com/bao/dating/controller/text.java b/src/main/java/com/bao/dating/controller/text.java new file mode 100644 index 0000000..7806c8a --- /dev/null +++ b/src/main/java/com/bao/dating/controller/text.java @@ -0,0 +1,4 @@ +package com.bao.dating.controller; + +public class text { +} diff --git a/src/main/java/com/bao/dating/mapper/ContactMapper.java b/src/main/java/com/bao/dating/mapper/ContactMapper.java new file mode 100644 index 0000000..3bb5f4c --- /dev/null +++ b/src/main/java/com/bao/dating/mapper/ContactMapper.java @@ -0,0 +1,20 @@ +package com.bao.dating.mapper; + +import com.bao.dating.pojo.entity.Contacts; +import com.bao.dating.pojo.entity.User; +import org.apache.ibatis.annotations.Mapper; +import org.apache.ibatis.annotations.Param; + +import java.util.List; +import java.util.Map; + +@Mapper +public interface ContactMapper { + /** + * 根据用户ID查询好友列表(仅正常状态、非黑名单的好友) + * @param userId 当前用户ID + * @return 好友列表(包含联系人+用户基础信息) + */ + List> selectFriendsByUserId(@Param("userId") Long userId); + +} diff --git a/src/main/java/com/bao/dating/mapper/FriendRelationMapper.java b/src/main/java/com/bao/dating/mapper/FriendRelationMapper.java new file mode 100644 index 0000000..f393a83 --- /dev/null +++ b/src/main/java/com/bao/dating/mapper/FriendRelationMapper.java @@ -0,0 +1,61 @@ +package com.bao.dating.mapper; + +import org.apache.ibatis.annotations.Param; + +import java.util.List; +import java.util.Map; + +public interface FriendRelationMapper { + /** + * 插入好友申请 + * @param params 申请参数(匹配friend_apply表字段) + */ + void insertFriendApply(Map params); + + /** + * 根据申请人ID和被申请人ID查询好友申请 + * @param applyUserId 申请人ID + * @param targetUserId 被申请人ID + * @return 好友申请记录 + */ + Map selectFriendApplyByUsers(@Param("applyUserId") Long applyUserId, @Param("targetUserId") Long targetUserId); + + /** + * 更新好友申请状态 + * @param params 更新参数 + */ + void updateFriendApplyStatus(Map params); + + /** + * 删除好友申请(可选) + * @param params 删除条件 + */ + void deleteFriendApply(Map params); + + /** + * 查询待处理的好友申请 + * @param targetUserId 被申请人ID + * @return 申请列表 + */ + List> selectFriendApplyList(@Param("targetUserId") Long targetUserId); + + /** + * 插入联系人记录 + * @param params 插入参数(匹配contacts表字段) + */ + void insertFriendRelation(Map params); + + /** + * 根据用户ID查询联系人列表 + * @param userId 用户ID + * @return 联系人列表 + */ + List> selectFriendRelationListByUserId(@Param("userId") Long userId); + + /** + * 删除过期的好友申请 + * @param days 过期天数 + * @return 删除的记录数 + */ + int deleteExpiredFriendApply(@Param("days") Integer days); +} diff --git a/src/main/java/com/bao/dating/mapper/PostMapper.java b/src/main/java/com/bao/dating/mapper/PostMapper.java index 8380a2f..b2ff337 100644 --- a/src/main/java/com/bao/dating/mapper/PostMapper.java +++ b/src/main/java/com/bao/dating/mapper/PostMapper.java @@ -26,6 +26,27 @@ public interface PostMapper { * @param postIds 动态ID */ int deletePostByIds(List postIds); + + /** + * 根据动态ID删除收藏记录 + * + * @param postId 动态ID + */ + int deletePostFavoriteByPostId(Long postId); + + /** + * 根据动态ID删除点赞记录 + * + * @param postId 动态ID + */ + int deletePostLikeByPostId(Long postId); + + /** + * 根据动态ID删除评论记录 + * + * @param postId 动态ID + */ + int deleteCommentsByPostId(Long postId); /** * 根据ID查询动态 diff --git a/src/main/java/com/bao/dating/mapper/UserMapper.java b/src/main/java/com/bao/dating/mapper/UserMapper.java index b15f293..8c49967 100644 --- a/src/main/java/com/bao/dating/mapper/UserMapper.java +++ b/src/main/java/com/bao/dating/mapper/UserMapper.java @@ -3,6 +3,7 @@ package com.bao.dating.mapper; import com.bao.dating.pojo.dto.UserInfoUpdateDTO; import com.bao.dating.pojo.entity.User; import org.apache.ibatis.annotations.Mapper; +import org.apache.ibatis.annotations.Param; /** * 用户Mapper @@ -33,4 +34,5 @@ public interface UserMapper { */ void updateUserInfoByUserId(UserInfoUpdateDTO userInfoUpdateDTO); + } diff --git a/src/main/java/com/bao/dating/pojo/entity/Contacts.java b/src/main/java/com/bao/dating/pojo/entity/Contacts.java new file mode 100644 index 0000000..85cd910 --- /dev/null +++ b/src/main/java/com/bao/dating/pojo/entity/Contacts.java @@ -0,0 +1,24 @@ +package com.bao.dating.pojo.entity; + +import lombok.Data; + +import java.time.LocalDateTime; +import java.util.Date; +import java.util.List; + +@Data +public class Contacts { + private Long contactId; // 通讯录记录ID + private Long userId; // 当前用户ID + private Long contactUserId; // 联系人用户ID + private String contactNickname; // 联系人备注名 + private String contactAvatar; // 联系人头像缓存 + private Integer relationType; // 关系类型(1普通好友/2特别关注/3黑名单/4陌生人) + private Date addTime; // 添加时间 + private Date lastChatTime; // 最后聊天时间 + private Integer contactStatus; // 联系人状态(1正常/2已删除/3已拉黑) + private String contactRemark; // 备注信息 + private List tags; // 标签(JSON数组) + private Date createdAt; // 创建时间 + private Date updatedAt; // 更新时间 +} diff --git a/src/main/java/com/bao/dating/pojo/entity/FriendApply.java b/src/main/java/com/bao/dating/pojo/entity/FriendApply.java new file mode 100644 index 0000000..e4f3277 --- /dev/null +++ b/src/main/java/com/bao/dating/pojo/entity/FriendApply.java @@ -0,0 +1,60 @@ +package com.bao.dating.pojo.entity; + +import lombok.Data; + +import java.util.Date; + +/** + * 好友申请实体类 + * @author KilLze + */ +@Data +public class FriendApply { + /** + * 申请ID,主键 + * 对应字段:apply_id + */ + private Long applyId; + + /** + * 申请人ID(对应user表user_id) + * 对应字段:apply_user_id + */ + private Long applyUserId; + + /** + * 被申请人ID(对应user表user_id) + * 对应字段:target_user_id + */ + private Long targetUserId; + + /** + * 打招呼内容 + * 对应字段:greeting + */ + private String greeting; + + /** + * 申请时间 + * 对应字段:apply_time + */ + private Date applyTime; + + /** + * 申请状态:0-待处理,1-已同意,2-已拒绝 + * 对应字段:apply_status + */ + private Byte applyStatus; + + /** + * 创建时间 + * 对应字段:created_at + */ + private Date createdAt; + + /** + * 更新时间 + * 对应字段:updated_at + */ + private Date updatedAt; +} \ No newline at end of file diff --git a/src/main/java/com/bao/dating/service/ContactsService.java b/src/main/java/com/bao/dating/service/ContactsService.java new file mode 100644 index 0000000..1efe464 --- /dev/null +++ b/src/main/java/com/bao/dating/service/ContactsService.java @@ -0,0 +1,20 @@ +package com.bao.dating.service; + + +import com.bao.dating.pojo.entity.User; + +import java.util.List; +import java.util.Map; + +public interface ContactsService { + /** + * 根据用户ID查询好友列表 + * @param userId 用户ID + * @return 好友列表 + */ + List> getFriendsByUserId(Long userId); + + + +} + diff --git a/src/main/java/com/bao/dating/service/FriendRelationService.java b/src/main/java/com/bao/dating/service/FriendRelationService.java new file mode 100644 index 0000000..dc95d8d --- /dev/null +++ b/src/main/java/com/bao/dating/service/FriendRelationService.java @@ -0,0 +1,43 @@ +package com.bao.dating.service; + +import java.util.List; +import java.util.Map; + +public interface FriendRelationService { + /** + * 新增好友申请 + * @param applyUserId 申请人ID + * @param targetUserId 被申请人ID + * @param greeting 打招呼内容 + */ + void addFriendApply(Long applyUserId, Long targetUserId, String greeting); + + /** + * 同意好友申请(更新状态/删除记录) + * @param applyUserId 申请人ID + * @param targetUserId 被申请人ID + */ + void agreeFriendApply(Long applyUserId, Long targetUserId); + + /** + * 查询待处理的好友申请 + * @param targetUserId 被申请人ID + * @return 申请列表 + */ + List> getFriendApplyList(Long targetUserId); + + /** + * 添加好友到通讯录表 + * @param userId 当前用户ID + * @param contactUserId 联系人ID + * @param contactNickname 备注名 + */ + void addFriendRelation(Long userId, Long contactUserId, String contactNickname); + + /** + * 查询用户的联系人列表 + * @param userId 当前用户ID + * @return 联系人列表 + */ + List> getFriendRelationList(Long userId); +} diff --git a/src/main/java/com/bao/dating/service/impl/ContactServiceImpl.java b/src/main/java/com/bao/dating/service/impl/ContactServiceImpl.java new file mode 100644 index 0000000..78c7020 --- /dev/null +++ b/src/main/java/com/bao/dating/service/impl/ContactServiceImpl.java @@ -0,0 +1,22 @@ +package com.bao.dating.service.impl; + +import com.bao.dating.mapper.ContactMapper; +import com.bao.dating.service.ContactsService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.stereotype.Service; + +import java.util.List; +import java.util.Map; + +@Service +public class ContactServiceImpl implements ContactsService { + @Autowired + private ContactMapper contactMapper; + + @Override + public List> getFriendsByUserId(Long userId) { + // 直接调用Mapper查询,无额外封装 + return contactMapper.selectFriendsByUserId(userId); + } +} diff --git a/src/main/java/com/bao/dating/service/impl/FriendRelationServiceImpl.java b/src/main/java/com/bao/dating/service/impl/FriendRelationServiceImpl.java new file mode 100644 index 0000000..e6ef79c --- /dev/null +++ b/src/main/java/com/bao/dating/service/impl/FriendRelationServiceImpl.java @@ -0,0 +1,81 @@ +package com.bao.dating.service.impl; + +import com.bao.dating.mapper.FriendRelationMapper; +import com.bao.dating.service.FriendRelationService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +@Service +public class FriendRelationServiceImpl implements FriendRelationService { + + @Autowired + private FriendRelationMapper friendRelationMapper; + + /** + * 新增好友申请(写入数据库) + */ + @Override + public void addFriendApply(Long applyUserId, Long targetUserId, String greeting) { + // 检查是否已存在待处理的申请 + Map existingApply = friendRelationMapper.selectFriendApplyByUsers(applyUserId, targetUserId); + if (existingApply != null) { + throw new RuntimeException("好友申请已存在,请勿重复发送"); + } + + Map params = new HashMap<>(); + params.put("apply_user_id", applyUserId); + params.put("target_user_id", targetUserId); + params.put("greeting", greeting); + friendRelationMapper.insertFriendApply(params); + } + + /** + * 同意好友申请(更新状态为已同意,也可直接删除) + */ + @Override + @Transactional(rollbackFor = Exception.class) + public void agreeFriendApply(Long applyUserId, Long targetUserId) { + Map params = new HashMap<>(); + params.put("apply_user_id", applyUserId); + params.put("target_user_id", targetUserId); + params.put("apply_status", 1); // 1-已同意 + friendRelationMapper.updateFriendApplyStatus(params); + // 也可选择直接删除申请记录:friendRelationMapper.deleteFriendApply(params); + } + + /** + * 查询待处理的好友申请 + */ + @Override + public List> getFriendApplyList(Long targetUserId) { + return friendRelationMapper.selectFriendApplyList(targetUserId); + } + + /** + * 添加好友到通讯录表(严格匹配contacts表字段) + */ + @Override + @Transactional(rollbackFor = Exception.class) + public void addFriendRelation(Long userId, Long contactUserId, String contactNickname) { + Map params = new HashMap<>(); + params.put("user_id", userId); + params.put("contact_user_id", contactUserId); + params.put("contact_nickname", contactNickname); + params.put("relation_type", 1); // 1-普通好友 + params.put("contact_status", 1); // 1-正常 + friendRelationMapper.insertFriendRelation(params); + } + + /** + * 查询用户的联系人列表 + */ + @Override + public List> getFriendRelationList(Long userId) { + return friendRelationMapper.selectFriendRelationListByUserId(userId); + } +} diff --git a/src/main/java/com/bao/dating/service/impl/PostServiceImpl.java b/src/main/java/com/bao/dating/service/impl/PostServiceImpl.java index 5a430cd..9d9e84b 100644 --- a/src/main/java/com/bao/dating/service/impl/PostServiceImpl.java +++ b/src/main/java/com/bao/dating/service/impl/PostServiceImpl.java @@ -188,7 +188,7 @@ public class PostServiceImpl implements PostService { // 判断用户权限 Long userId = UserContext.getUserId(); - // 遍历所有要删除的帖子ID,验证权限 + // 遍历所有要删除的帖子ID,验证权限并删除相关记录 for (Long postId : postIds) { Post post = postMapper.selectById(postId); if (post == null) { @@ -198,6 +198,12 @@ public class PostServiceImpl implements PostService { if (post.getUserId() == null || !post.getUserId().equals(userId)) { throw new RuntimeException("无权限删除此动态"); } + // 删除相关的收藏记录 + postMapper.deletePostFavoriteByPostId(postId); + // 删除相关的点赞记录 + postMapper.deletePostLikeByPostId(postId); + // 删除相关的评论记录 + postMapper.deleteCommentsByPostId(postId); } // 批量删除动态 return postMapper.deletePostByIds(postIds); diff --git a/src/main/java/com/bao/dating/task/FriendApplyCleanupTask.java b/src/main/java/com/bao/dating/task/FriendApplyCleanupTask.java new file mode 100644 index 0000000..7b4c4ba --- /dev/null +++ b/src/main/java/com/bao/dating/task/FriendApplyCleanupTask.java @@ -0,0 +1,36 @@ +package com.bao.dating.task; + +import com.bao.dating.mapper.FriendRelationMapper; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.scheduling.annotation.Scheduled; +import org.springframework.stereotype.Component; + +/** + * 好友申请清理定时任务 + * 定时清理数据库中超过7天未处理的好友申请 + * @author KilLze + */ +@Slf4j +@Component +public class FriendApplyCleanupTask { + + @Autowired + private FriendRelationMapper friendRelationMapper; + + /** + * 定时清理过期的好友申请 + * 每1分钟执行一次,清理创建时间超过7天的未处理申请 + */ + @Scheduled(fixedRate = 60000) // 每1分钟执行一次 + public void cleanupExpiredFriendApply() { + log.info("开始执行好友申请清理任务"); + try { + // 删除创建时间超过7天的未处理申请(apply_status = 0) + int deletedCount = friendRelationMapper.deleteExpiredFriendApply(7); + log.info("好友申请清理任务执行完成,删除了 {} 条过期记录", deletedCount); + } catch (Exception e) { + log.error("好友申请清理任务执行失败", e); + } + } +} \ No newline at end of file diff --git a/src/main/java/com/bao/dating/util/EmailUtil.java b/src/main/java/com/bao/dating/util/EmailUtil.java index afcb813..055971d 100644 --- a/src/main/java/com/bao/dating/util/EmailUtil.java +++ b/src/main/java/com/bao/dating/util/EmailUtil.java @@ -84,6 +84,7 @@ public class EmailUtil { } catch (MessagingException e) { log.error("HTML邮件发送失败,收件人:{},异常信息:{}", to, e.getMessage(), e); return false; + } } @@ -173,6 +174,7 @@ public class EmailUtil { } log.info("批量邮件发送完成,总数:{},成功:{}", toList.length, successCount); return successCount; + } } diff --git a/src/main/java/com/bao/dating/util/MD5Util.java b/src/main/java/com/bao/dating/util/MD5Util.java index e69de29..1e8cfc7 100644 --- a/src/main/java/com/bao/dating/util/MD5Util.java +++ b/src/main/java/com/bao/dating/util/MD5Util.java @@ -0,0 +1,93 @@ +package com.bao.dating.util; + +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; + +/** + * MD5工具类 + * 提供MD5加密功能 + * @author KilLze + */ +public class MD5Util { + + /** + * 对字符串进行MD5加密 + * @param input 待加密的字符串 + * @return MD5加密后的32位小写字符串 + */ + public static String encrypt(String input) { + if (input == null || input.isEmpty()) { + return null; + } + + try { + MessageDigest md = MessageDigest.getInstance("MD5"); + byte[] digest = md.digest(input.getBytes()); + return bytesToHex(digest); + } catch (NoSuchAlgorithmException e) { + throw new RuntimeException("MD5算法不可用", e); + } + } + + /** + * 对字符串进行MD5加密(带盐值) + * @param input 待加密的字符串 + * @param salt 盐值 + * @return MD5加密后的32位小写字符串 + */ + public static String encryptWithSalt(String input, String salt) { + if (input == null || input.isEmpty()) { + return null; + } + if (salt == null) { + salt = ""; + } + return encrypt(input + salt); + } + + /** + * 验证字符串与MD5值是否匹配 + * @param input 原始字符串 + * @param md5Hash MD5哈希值 + * @return 是否匹配 + */ + public static boolean verify(String input, String md5Hash) { + if (input == null || md5Hash == null) { + return false; + } + return encrypt(input).equalsIgnoreCase(md5Hash); + } + + /** + * 验证字符串与MD5值是否匹配(带盐值) + * @param input 原始字符串 + * @param salt 盐值 + * @param md5Hash MD5哈希值 + * @return 是否匹配 + */ + public static boolean verifyWithSalt(String input, String salt, String md5Hash) { + if (input == null || md5Hash == null) { + return false; + } + if (salt == null) { + salt = ""; + } + return encryptWithSalt(input, salt).equalsIgnoreCase(md5Hash); + } + + /** + * 将字节数组转换为十六进制字符串 + * @param bytes 字节数组 + * @return 十六进制字符串 + */ + private static String bytesToHex(byte[] bytes) { + StringBuilder sb = new StringBuilder(); + for (byte b : bytes) { + sb.append(String.format("%02x", b)); + } + return sb.toString(); + } +} + + + diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 82aafc7..5ad47ea 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -39,7 +39,6 @@ spring: connectiontimeout: 10000 # 连接超时时间(毫秒) timeout: 10000 # 读取超时时间(毫秒) writetimeout: 10000 # 写入超时时间(毫秒) - # MyBatis 配置 mybatis: mapper-locations: classpath:mapper/*.xml diff --git a/src/main/resources/com/bao/dating/mapper/ContactMapper.xml b/src/main/resources/com/bao/dating/mapper/ContactMapper.xml new file mode 100644 index 0000000..40c7e8c --- /dev/null +++ b/src/main/resources/com/bao/dating/mapper/ContactMapper.xml @@ -0,0 +1,25 @@ + + + + + + + diff --git a/src/main/resources/com/bao/dating/mapper/FriendRelationMapper.xml b/src/main/resources/com/bao/dating/mapper/FriendRelationMapper.xml new file mode 100644 index 0000000..d4ee89f --- /dev/null +++ b/src/main/resources/com/bao/dating/mapper/FriendRelationMapper.xml @@ -0,0 +1,130 @@ + + + + + + + INSERT INTO friend_apply ( + apply_user_id, + target_user_id, + greeting, + apply_time, + apply_status, + created_at, + updated_at + ) VALUES ( + #{apply_user_id}, + #{target_user_id}, + #{greeting}, + NOW(), + 0, + NOW(), + NOW() + ) + + + + + UPDATE friend_apply + SET apply_status = #{apply_status}, + updated_at = NOW() + WHERE apply_user_id = #{apply_user_id} + AND target_user_id = #{target_user_id} + AND apply_status = 0 + + + + + DELETE FROM friend_apply + WHERE apply_user_id = #{apply_user_id} + AND target_user_id = #{target_user_id} + + + + + + + + + + + INSERT INTO contacts ( + user_id, + contact_user_id, + contact_nickname, + relation_type, + contact_status, + add_time, + created_at, + updated_at + ) VALUES ( + #{user_id}, + #{contact_user_id}, + #{contact_nickname}, + #{relation_type}, + #{contact_status}, + NOW(), + NOW(), + NOW() + ) + + + + + + + + DELETE FROM friend_apply + WHERE apply_status = 0 + AND created_at < DATE_SUB(NOW(), INTERVAL #{days} DAY) + + + \ No newline at end of file diff --git a/src/main/resources/com/bao/dating/mapper/PostMapper.xml b/src/main/resources/com/bao/dating/mapper/PostMapper.xml index b1924e4..5dce274 100644 --- a/src/main/resources/com/bao/dating/mapper/PostMapper.xml +++ b/src/main/resources/com/bao/dating/mapper/PostMapper.xml @@ -36,15 +36,15 @@ - + DELETE FROM post_favorite WHERE post_id = #{postId} - + DELETE FROM post_like WHERE post_id = #{postId} - + DELETE FROM comments WHERE post_id = #{postId} diff --git a/src/main/resources/com/bao/dating/mapper/UserMapper.xml b/src/main/resources/com/bao/dating/mapper/UserMapper.xml index 61bb82b..eff7413 100644 --- a/src/main/resources/com/bao/dating/mapper/UserMapper.xml +++ b/src/main/resources/com/bao/dating/mapper/UserMapper.xml @@ -74,4 +74,6 @@ WHERE user_id = #{userId} + + \ No newline at end of file