Compare commits
2 Commits
feature-ip
...
feature/co
| Author | SHA1 | Date | |
|---|---|---|---|
| 1d9ca28ec9 | |||
| 7c0bfbc1de |
@@ -3,9 +3,11 @@ package com.bao.dating;
|
|||||||
import org.mybatis.spring.annotation.MapperScan;
|
import org.mybatis.spring.annotation.MapperScan;
|
||||||
import org.springframework.boot.SpringApplication;
|
import org.springframework.boot.SpringApplication;
|
||||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
import org.springframework.scheduling.annotation.EnableScheduling;
|
||||||
|
|
||||||
@MapperScan("com.bao.dating.mapper")
|
@MapperScan("com.bao.dating.mapper")
|
||||||
@SpringBootApplication
|
@SpringBootApplication
|
||||||
|
@EnableScheduling
|
||||||
public class DatingApplication {
|
public class DatingApplication {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
SpringApplication.run(DatingApplication.class, args);
|
SpringApplication.run(DatingApplication.class, args);
|
||||||
|
|||||||
29
src/main/java/com/bao/dating/config/RedisConfig.java
Normal file
29
src/main/java/com/bao/dating/config/RedisConfig.java
Normal file
@@ -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<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
|
||||||
|
RedisTemplate<String, Object> 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -29,7 +29,11 @@ public class WebConfig implements WebMvcConfigurer {
|
|||||||
.addPathPatterns("/**")
|
.addPathPatterns("/**")
|
||||||
// 忽略的接口
|
// 忽略的接口
|
||||||
.excludePathPatterns(
|
.excludePathPatterns(
|
||||||
"/user/login","/user/register","/user/emailLogin","/ip/location"
|
"/user/login",
|
||||||
|
"/user/register",
|
||||||
|
"/user/emailLogin",
|
||||||
|
"/api/verification/send-email-code",
|
||||||
|
"/ip/location"
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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<String, Object> getFriends() {
|
||||||
|
|
||||||
|
// 从UserContext获取当前用户ID
|
||||||
|
Long userId = UserContext.getUserId();
|
||||||
|
if (userId == null) {
|
||||||
|
Map<String, Object> error = new HashMap<>();
|
||||||
|
error.put("code", 401);
|
||||||
|
error.put("msg", "用户未授权");
|
||||||
|
return error;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 查询好友列表
|
||||||
|
List<Map<String, Object>> friends = contactsService.getFriendsByUserId(userId);
|
||||||
|
|
||||||
|
// 构造响应
|
||||||
|
Map<String, Object> result = new HashMap<>();
|
||||||
|
result.put("code", 200);
|
||||||
|
result.put("msg", "查询成功");
|
||||||
|
result.put("data", friends);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
122
src/main/java/com/bao/dating/controller/FriendController.java
Normal file
122
src/main/java/com/bao/dating/controller/FriendController.java
Normal file
@@ -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<String, Object> sendFriendApply(
|
||||||
|
@RequestParam("targetUserId") Long targetUserId,
|
||||||
|
@RequestParam(value = "greeting", defaultValue = "你好,加个好友吧") String greeting) {
|
||||||
|
Map<String, Object> 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<String, Object> agreeFriendApply(
|
||||||
|
@RequestParam("applyUserId") Long applyUserId,
|
||||||
|
@RequestParam("targetUserId") Long targetUserId,
|
||||||
|
@RequestParam(value = "contactNickname", required = false) String contactNickname) {
|
||||||
|
Map<String, Object> 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<String, Object> getApplyList() {
|
||||||
|
Map<String, Object> result = new HashMap<>();
|
||||||
|
try {
|
||||||
|
Long targetUserId = UserContext.getUserId();
|
||||||
|
if (targetUserId == null) {
|
||||||
|
result.put("code", 401);
|
||||||
|
result.put("msg", "用户未登录");
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
List<Map<String, Object>> 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<String, Object> getFriendRelationList() {
|
||||||
|
Map<String, Object> 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
4
src/main/java/com/bao/dating/controller/text.java
Normal file
4
src/main/java/com/bao/dating/controller/text.java
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
package com.bao.dating.controller;
|
||||||
|
|
||||||
|
public class text {
|
||||||
|
}
|
||||||
20
src/main/java/com/bao/dating/mapper/ContactMapper.java
Normal file
20
src/main/java/com/bao/dating/mapper/ContactMapper.java
Normal file
@@ -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<Map<String, Object>> selectFriendsByUserId(@Param("userId") Long userId);
|
||||||
|
|
||||||
|
}
|
||||||
@@ -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<String, Object> params);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据申请人ID和被申请人ID查询好友申请
|
||||||
|
* @param applyUserId 申请人ID
|
||||||
|
* @param targetUserId 被申请人ID
|
||||||
|
* @return 好友申请记录
|
||||||
|
*/
|
||||||
|
Map<String, Object> selectFriendApplyByUsers(@Param("applyUserId") Long applyUserId, @Param("targetUserId") Long targetUserId);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 更新好友申请状态
|
||||||
|
* @param params 更新参数
|
||||||
|
*/
|
||||||
|
void updateFriendApplyStatus(Map<String, Object> params);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 删除好友申请(可选)
|
||||||
|
* @param params 删除条件
|
||||||
|
*/
|
||||||
|
void deleteFriendApply(Map<String, Object> params);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 查询待处理的好友申请
|
||||||
|
* @param targetUserId 被申请人ID
|
||||||
|
* @return 申请列表
|
||||||
|
*/
|
||||||
|
List<Map<String, Object>> selectFriendApplyList(@Param("targetUserId") Long targetUserId);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 插入联系人记录
|
||||||
|
* @param params 插入参数(匹配contacts表字段)
|
||||||
|
*/
|
||||||
|
void insertFriendRelation(Map<String, Object> params);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据用户ID查询联系人列表
|
||||||
|
* @param userId 用户ID
|
||||||
|
* @return 联系人列表
|
||||||
|
*/
|
||||||
|
List<Map<String, Object>> selectFriendRelationListByUserId(@Param("userId") Long userId);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 删除过期的好友申请
|
||||||
|
* @param days 过期天数
|
||||||
|
* @return 删除的记录数
|
||||||
|
*/
|
||||||
|
int deleteExpiredFriendApply(@Param("days") Integer days);
|
||||||
|
}
|
||||||
@@ -26,6 +26,27 @@ public interface PostMapper {
|
|||||||
* @param postIds 动态ID
|
* @param postIds 动态ID
|
||||||
*/
|
*/
|
||||||
int deletePostByIds(List<Long> postIds);
|
int deletePostByIds(List<Long> 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查询动态
|
* 根据ID查询动态
|
||||||
|
|||||||
@@ -54,5 +54,4 @@ public interface UserMapper {
|
|||||||
*/
|
*/
|
||||||
User selectByUserEmailUser(@Param("userEmail") String email);
|
User selectByUserEmailUser(@Param("userEmail") String email);
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
24
src/main/java/com/bao/dating/pojo/entity/Contacts.java
Normal file
24
src/main/java/com/bao/dating/pojo/entity/Contacts.java
Normal file
@@ -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<String> tags; // 标签(JSON数组)
|
||||||
|
private Date createdAt; // 创建时间
|
||||||
|
private Date updatedAt; // 更新时间
|
||||||
|
}
|
||||||
60
src/main/java/com/bao/dating/pojo/entity/FriendApply.java
Normal file
60
src/main/java/com/bao/dating/pojo/entity/FriendApply.java
Normal file
@@ -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;
|
||||||
|
}
|
||||||
20
src/main/java/com/bao/dating/service/ContactsService.java
Normal file
20
src/main/java/com/bao/dating/service/ContactsService.java
Normal file
@@ -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<Map<String, Object>> getFriendsByUserId(Long userId);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
@@ -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<Map<String, Object>> getFriendApplyList(Long targetUserId);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 添加好友到通讯录表
|
||||||
|
* @param userId 当前用户ID
|
||||||
|
* @param contactUserId 联系人ID
|
||||||
|
* @param contactNickname 备注名
|
||||||
|
*/
|
||||||
|
void addFriendRelation(Long userId, Long contactUserId, String contactNickname);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 查询用户的联系人列表
|
||||||
|
* @param userId 当前用户ID
|
||||||
|
* @return 联系人列表
|
||||||
|
*/
|
||||||
|
List<Map<String, Object>> getFriendRelationList(Long userId);
|
||||||
|
}
|
||||||
@@ -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<Map<String, Object>> getFriendsByUserId(Long userId) {
|
||||||
|
// 直接调用Mapper查询,无额外封装
|
||||||
|
return contactMapper.selectFriendsByUserId(userId);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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<String, Object> existingApply = friendRelationMapper.selectFriendApplyByUsers(applyUserId, targetUserId);
|
||||||
|
if (existingApply != null) {
|
||||||
|
throw new RuntimeException("好友申请已存在,请勿重复发送");
|
||||||
|
}
|
||||||
|
|
||||||
|
Map<String, Object> 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<String, Object> 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<Map<String, Object>> getFriendApplyList(Long targetUserId) {
|
||||||
|
return friendRelationMapper.selectFriendApplyList(targetUserId);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 添加好友到通讯录表(严格匹配contacts表字段)
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
@Transactional(rollbackFor = Exception.class)
|
||||||
|
public void addFriendRelation(Long userId, Long contactUserId, String contactNickname) {
|
||||||
|
Map<String, Object> 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<Map<String, Object>> getFriendRelationList(Long userId) {
|
||||||
|
return friendRelationMapper.selectFriendRelationListByUserId(userId);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -188,7 +188,7 @@ public class PostServiceImpl implements PostService {
|
|||||||
// 判断用户权限
|
// 判断用户权限
|
||||||
Long userId = UserContext.getUserId();
|
Long userId = UserContext.getUserId();
|
||||||
|
|
||||||
// 遍历所有要删除的帖子ID,验证权限
|
// 遍历所有要删除的帖子ID,验证权限并删除相关记录
|
||||||
for (Long postId : postIds) {
|
for (Long postId : postIds) {
|
||||||
Post post = postMapper.selectById(postId);
|
Post post = postMapper.selectById(postId);
|
||||||
if (post == null) {
|
if (post == null) {
|
||||||
@@ -198,6 +198,12 @@ public class PostServiceImpl implements PostService {
|
|||||||
if (post.getUserId() == null || !post.getUserId().equals(userId)) {
|
if (post.getUserId() == null || !post.getUserId().equals(userId)) {
|
||||||
throw new RuntimeException("无权限删除此动态");
|
throw new RuntimeException("无权限删除此动态");
|
||||||
}
|
}
|
||||||
|
// 删除相关的收藏记录
|
||||||
|
postMapper.deletePostFavoriteByPostId(postId);
|
||||||
|
// 删除相关的点赞记录
|
||||||
|
postMapper.deletePostLikeByPostId(postId);
|
||||||
|
// 删除相关的评论记录
|
||||||
|
postMapper.deleteCommentsByPostId(postId);
|
||||||
}
|
}
|
||||||
// 批量删除动态
|
// 批量删除动态
|
||||||
return postMapper.deletePostByIds(postIds);
|
return postMapper.deletePostByIds(postIds);
|
||||||
|
|||||||
@@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -84,6 +84,7 @@ public class EmailUtil {
|
|||||||
} catch (MessagingException e) {
|
} catch (MessagingException e) {
|
||||||
log.error("HTML邮件发送失败,收件人:{},异常信息:{}", to, e.getMessage(), e);
|
log.error("HTML邮件发送失败,收件人:{},异常信息:{}", to, e.getMessage(), e);
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -173,6 +174,7 @@ public class EmailUtil {
|
|||||||
}
|
}
|
||||||
log.info("批量邮件发送完成,总数:{},成功:{}", toList.length, successCount);
|
log.info("批量邮件发送完成,总数:{},成功:{}", toList.length, successCount);
|
||||||
return successCount;
|
return successCount;
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -39,7 +39,6 @@ spring:
|
|||||||
connectiontimeout: 10000 # 连接超时时间(毫秒)
|
connectiontimeout: 10000 # 连接超时时间(毫秒)
|
||||||
timeout: 10000 # 读取超时时间(毫秒)
|
timeout: 10000 # 读取超时时间(毫秒)
|
||||||
writetimeout: 10000 # 写入超时时间(毫秒)
|
writetimeout: 10000 # 写入超时时间(毫秒)
|
||||||
|
|
||||||
# MyBatis 配置
|
# MyBatis 配置
|
||||||
mybatis:
|
mybatis:
|
||||||
mapper-locations: classpath:mapper/*.xml
|
mapper-locations: classpath:mapper/*.xml
|
||||||
|
|||||||
25
src/main/resources/com/bao/dating/mapper/ContactMapper.xml
Normal file
25
src/main/resources/com/bao/dating/mapper/ContactMapper.xml
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8" ?>
|
||||||
|
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
||||||
|
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||||
|
|
||||||
|
<mapper namespace="com.bao.dating.mapper.ContactMapper">
|
||||||
|
<select id="selectFriendsByUserId" resultType="java.util.Map">
|
||||||
|
SELECT c.contact_id,
|
||||||
|
c.user_id,
|
||||||
|
c.contact_user_id,
|
||||||
|
c.contact_nickname,
|
||||||
|
c.relation_type,
|
||||||
|
c.add_time,
|
||||||
|
u.user_name,
|
||||||
|
u.nickname,
|
||||||
|
u.avatar_url,
|
||||||
|
u.signature
|
||||||
|
FROM contacts c
|
||||||
|
LEFT JOIN user u ON c.contact_user_id = u.user_id
|
||||||
|
WHERE c.user_id = #{userId}
|
||||||
|
AND c.contact_status = 1 -- 正常状态
|
||||||
|
AND c.relation_type != 3 -- 排除黑名单
|
||||||
|
AND c.user_id != c.contact_user_id -- 排除自己
|
||||||
|
</select>
|
||||||
|
</mapper>
|
||||||
|
|
||||||
@@ -0,0 +1,130 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8" ?>
|
||||||
|
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
||||||
|
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||||
|
|
||||||
|
<mapper namespace="com.bao.dating.mapper.FriendRelationMapper">
|
||||||
|
<!-- 插入好友申请 -->
|
||||||
|
<insert id="insertFriendApply" parameterType="java.util.Map">
|
||||||
|
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()
|
||||||
|
)
|
||||||
|
</insert>
|
||||||
|
|
||||||
|
<!-- 更新好友申请状态 -->
|
||||||
|
<update id="updateFriendApplyStatus" parameterType="java.util.Map">
|
||||||
|
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
|
||||||
|
</update>
|
||||||
|
|
||||||
|
<!-- 删除好友申请(可选) -->
|
||||||
|
<delete id="deleteFriendApply" parameterType="java.util.Map">
|
||||||
|
DELETE FROM friend_apply
|
||||||
|
WHERE apply_user_id = #{apply_user_id}
|
||||||
|
AND target_user_id = #{target_user_id}
|
||||||
|
</delete>
|
||||||
|
|
||||||
|
<!-- 根据申请人ID和被申请人ID查询好友申请 -->
|
||||||
|
<select id="selectFriendApplyByUsers" parameterType="java.util.Map" resultType="java.util.Map">
|
||||||
|
SELECT
|
||||||
|
apply_id,
|
||||||
|
apply_user_id,
|
||||||
|
target_user_id,
|
||||||
|
greeting,
|
||||||
|
apply_time,
|
||||||
|
apply_status,
|
||||||
|
created_at,
|
||||||
|
updated_at
|
||||||
|
FROM friend_apply
|
||||||
|
WHERE apply_user_id = #{applyUserId}
|
||||||
|
AND target_user_id = #{targetUserId}
|
||||||
|
AND apply_status = 0
|
||||||
|
</select>
|
||||||
|
|
||||||
|
<!-- 查询待处理的好友申请 -->
|
||||||
|
<select id="selectFriendApplyList" parameterType="java.lang.Long" resultType="java.util.Map">
|
||||||
|
SELECT
|
||||||
|
apply_id,
|
||||||
|
apply_user_id,
|
||||||
|
target_user_id,
|
||||||
|
greeting,
|
||||||
|
apply_time,
|
||||||
|
apply_status,
|
||||||
|
created_at,
|
||||||
|
updated_at
|
||||||
|
FROM friend_apply
|
||||||
|
WHERE target_user_id = #{targetUserId}
|
||||||
|
AND apply_status = 0
|
||||||
|
ORDER BY apply_time DESC
|
||||||
|
</select>
|
||||||
|
|
||||||
|
<!-- 插入联系人记录(方法名同步修改,字段名不变) -->
|
||||||
|
<insert id="insertFriendRelation" parameterType="java.util.Map">
|
||||||
|
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()
|
||||||
|
)
|
||||||
|
</insert>
|
||||||
|
|
||||||
|
<!-- 查询联系人列表(方法名同步修改,字段名不变) -->
|
||||||
|
<select id="selectFriendRelationListByUserId" parameterType="java.lang.Long" resultType="java.util.Map">
|
||||||
|
SELECT
|
||||||
|
contact_id,
|
||||||
|
user_id,
|
||||||
|
contact_user_id,
|
||||||
|
contact_nickname,
|
||||||
|
contact_avatar,
|
||||||
|
relation_type,
|
||||||
|
add_time,
|
||||||
|
last_chat_time,
|
||||||
|
contact_status,
|
||||||
|
contact_remark,
|
||||||
|
tags,
|
||||||
|
created_at,
|
||||||
|
updated_at
|
||||||
|
FROM contacts
|
||||||
|
WHERE user_id = #{userId}
|
||||||
|
AND contact_status = 1
|
||||||
|
ORDER BY add_time DESC
|
||||||
|
</select>
|
||||||
|
|
||||||
|
<!-- 删除过期的好友申请 -->
|
||||||
|
<delete id="deleteExpiredFriendApply" parameterType="java.lang.Integer">
|
||||||
|
DELETE FROM friend_apply
|
||||||
|
WHERE apply_status = 0
|
||||||
|
AND created_at < DATE_SUB(NOW(), INTERVAL #{days} DAY)
|
||||||
|
</delete>
|
||||||
|
|
||||||
|
</mapper>
|
||||||
@@ -36,15 +36,15 @@
|
|||||||
</foreach>
|
</foreach>
|
||||||
</delete>
|
</delete>
|
||||||
<!--删除收藏记录-->
|
<!--删除收藏记录-->
|
||||||
<delete id="1">
|
<delete id="deletePostFavoriteByPostId">
|
||||||
DELETE FROM post_favorite WHERE post_id = #{postId}
|
DELETE FROM post_favorite WHERE post_id = #{postId}
|
||||||
</delete>
|
</delete>
|
||||||
<!--删除点赞记录-->
|
<!--删除点赞记录-->
|
||||||
<delete id="2">
|
<delete id="deletePostLikeByPostId">
|
||||||
DELETE FROM post_like WHERE post_id = #{postId}
|
DELETE FROM post_like WHERE post_id = #{postId}
|
||||||
</delete>
|
</delete>
|
||||||
<!--动态评论删除-->
|
<!--动态评论删除-->
|
||||||
<delete id="3">
|
<delete id="deleteCommentsByPostId">
|
||||||
DELETE FROM comments WHERE post_id = #{postId}
|
DELETE FROM comments WHERE post_id = #{postId}
|
||||||
</delete>
|
</delete>
|
||||||
|
|
||||||
|
|||||||
@@ -88,4 +88,6 @@
|
|||||||
</set>
|
</set>
|
||||||
WHERE user_id = #{userId}
|
WHERE user_id = #{userId}
|
||||||
</update>
|
</update>
|
||||||
|
|
||||||
|
|
||||||
</mapper>
|
</mapper>
|
||||||
Reference in New Issue
Block a user