30 Commits

Author SHA1 Message Date
KilLze
39af4e6596 修该死的bug 2026-01-14 11:44:36 +08:00
KilLze
ea5c95584d 优化会话列表,添加用户在线状态 2026-01-14 11:31:10 +08:00
KilLze
e170016019 完成用户在线状态查询 2026-01-14 11:21:06 +08:00
KilLze
6d95b8d391 完成数据库定时解封低频同步(凌晨3点),主要还是依赖redis实时同步 2026-01-13 01:34:05 +08:00
KilLze
1c2a4edae9 完成用户封禁,解封,封禁查询(由于没有管理员,所以现在先用普通用户测试封禁) 2026-01-13 01:29:34 +08:00
KilLze
088c94e723 用户封禁拦截器,登录验证完成 2026-01-13 00:32:30 +08:00
KilLze
a3d0d7423c 用户封禁拦截器,登录验证完成 2026-01-13 00:32:17 +08:00
KilLze
4f94c43f94 修bug 2026-01-11 00:57:29 +08:00
KilLze
7a8a03510c 修bug 2026-01-11 00:28:23 +08:00
KilLze
2189575659 修bug 2026-01-11 00:25:19 +08:00
bao
e9d20ccf6c feat:修复接口 2026-01-09 15:41:14 +08:00
bao
d70ca89707 merge feature-init into master
# Conflicts:
#	src/main/java/com/bao/dating/config/WebConfig.java
#	src/main/java/com/bao/dating/controller/UserController.java
#	src/main/java/com/bao/dating/mapper/UserMapper.java
#	src/main/java/com/bao/dating/service/UserService.java
#	src/main/java/com/bao/dating/service/impl/UserServiceImpl.java
#	src/main/resources/com/bao/dating/mapper/UserMapper.xml
2026-01-09 15:35:31 +08:00
bao
a4faa11036 feat: update user module on feature-init 2026-01-09 15:22:18 +08:00
bao
2811a987c0 merge: 合并feature-WebSocket分支到master 2026-01-09 09:46:55 +08:00
bao
88579a382e merge: 合并feature-ip-location分支到master 2026-01-09 09:40:39 +08:00
bao
4f36dd9aeb merge: 合并feature/contacts-friends分支到master 2026-01-09 09:07:12 +08:00
290db24bc4 查询附近的人(可以查看版) 2026-01-08 10:10:25 +08:00
bao
7c0bfbc1de feat: 添加联系人管理和好友关系功能 2026-01-08 10:06:49 +08:00
31b0dffd68 查询附近的人(可以查看版) 2026-01-08 10:06:03 +08:00
31fd23afa8 查询附近的人(可以查看版) 2026-01-08 10:05:58 +08:00
d8d46ab089 查询附近的人(可以查看版) 2026-01-08 10:00:27 +08:00
5624e598ec 完成用户邮箱登录和获得ip地址 2026-01-08 08:36:28 +08:00
KilLze
cf6f9b8b7c 完成消息撤回功能 2026-01-08 01:20:11 +08:00
KilLze
c83d86ad1a Ciallo~(∠·ω< )⌒★ 2026-01-08 00:09:26 +08:00
KilLze
0a17eb8deb 简单优化 2026-01-08 00:07:25 +08:00
KilLze
448ce1d3d6 完成聊天文件上传OSS 2026-01-08 00:06:10 +08:00
KilLze
a4e66b39d1 完成会话状态更新、置顶状态更新、免打扰状态更新 2026-01-07 23:35:49 +08:00
KilLze
0a1425c7ab Ciallo~(∠·ω< )⌒★ 2026-01-07 22:59:46 +08:00
KilLze
21c0a94a5d 优化会话,当对删除的会话发送信息时返回error 2026-01-07 03:43:33 +08:00
KilLze
b6953cb8d0 完成会话列表查询并校验会话状态 2026-01-07 03:33:34 +08:00
71 changed files with 2409 additions and 52 deletions

View File

@@ -82,6 +82,13 @@
<version>3.12.0</version>
</dependency>
<!-- OkHttp用于调用API -->
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>4.12.0</version>
</dependency>
<!-- AOP起步依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>

View File

@@ -3,12 +3,40 @@ 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;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
@MapperScan("com.bao.dating.mapper")
@SpringBootApplication
@EnableScheduling
public class DatingApplication {
public static void main(String[] args) {
SpringApplication.run(DatingApplication.class, args);
// 读取并打印 ciallo.txt 文件内容
printCialloFile();
}
}
/**
* 读取并打印 ciallo.txt 文件内容
*/
private static void printCialloFile() {
try (InputStream inputStream = DatingApplication.class.getClassLoader().getResourceAsStream("ciallo.txt");
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) {
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
System.err.println("读取 ciallo.txt 文件时发生错误: " + e.getMessage());
}
}
}

View File

@@ -75,6 +75,8 @@ public class SmsUtil {
.setTemplateCode(templateCode != null ? templateCode : this.templateCode)
.setTemplateParam(templateParam);
log.error("TemplateParam 实际值 = {}", templateParam);
SendSmsResponse response = client.sendSms(sendSmsRequest);
if ("OK".equals(response.getBody().getCode())) {
@@ -145,6 +147,7 @@ public class SmsUtil {
jsonBuilder.append("}");
return sendSms(phoneNumber, templateCode, jsonBuilder.toString());
}
}

View File

@@ -0,0 +1,14 @@
package com.bao.dating.common.ip2location;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Data
@Component
@ConfigurationProperties(prefix = "ip2location.api")
public class Ip2LocationConfig {
private String key;
private String url;
private int timeout;
}

View File

@@ -0,0 +1,15 @@
package com.bao.dating.common.result;
import com.bao.dating.pojo.vo.ChatRecordsVO;
import lombok.Data;
/**
* 聊天发送结果
* @author KilLze
*/
@Data
public class ChatSendResult {
private boolean success;
private String message;
private ChatRecordsVO record;
}

View File

@@ -28,6 +28,7 @@ public class RedisConfig {
redisTemplate.setHashKeySerializer(new StringRedisSerializer());
redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());
redisTemplate.afterPropertiesSet();
return redisTemplate;
}
}

View File

@@ -29,7 +29,13 @@ public class WebConfig implements WebMvcConfigurer {
.addPathPatterns("/**")
// 忽略的接口
.excludePathPatterns(
"/user/login"
"/user/login",
"/user/register",
"/user/emailLogin",
"/api/verification/send-email-code",
"/ip/location",
"/user/login",
"/user/sendCode"
);
}
}

View File

@@ -0,0 +1,51 @@
package com.bao.dating.controller;
import com.bao.dating.common.Result;
import com.bao.dating.common.ResultCode;
import com.bao.dating.pojo.dto.UserBanDTO;
import com.bao.dating.pojo.entity.UserBan;
import com.bao.dating.service.UserBanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
/**
* 管理员控制器
* @author lenovo
*/
@RestController
@RequestMapping("/admin")
public class AdminController {
@Autowired
private UserBanService userBanService;
/**
* 封禁用户
*/
@PostMapping("/{userId}/ban")
public Result<?> banUser(@PathVariable Long userId,
@RequestBody UserBanDTO userBanDTO) {
userBanDTO.setUserId(userId);
userBanService.banUser(userBanDTO);
return Result.success(ResultCode.SUCCESS, "封禁成功");
}
/**
* 解封用户
*/
@PostMapping("/{userId}/unban")
public Result<?> unbanUser(@PathVariable Long userId) {
userBanService.unbanUser(userId);
return Result.success(ResultCode.SUCCESS, "解封成功");
}
/**
* 查询封禁状态
*/
@GetMapping("/{userId}/banInfo")
public Result<UserBan> banInfo(@PathVariable Long userId) {
UserBan ban = userBanService.getActiveBan(userId);
return Result.success(ResultCode.SUCCESS, "查询成功", ban);
}
}

View File

@@ -4,14 +4,14 @@ import com.bao.dating.common.Result;
import com.bao.dating.common.ResultCode;
import com.bao.dating.common.result.PageResult;
import com.bao.dating.context.UserContext;
import com.bao.dating.pojo.dto.ChatCursorPageDTO;
import com.bao.dating.pojo.dto.ChatHistoryQueryDTO;
import com.bao.dating.pojo.dto.*;
import com.bao.dating.pojo.vo.ChatRecordsVO;
import com.bao.dating.pojo.vo.ChatSessionsVO;
import com.bao.dating.service.ChatService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.util.List;
@@ -26,6 +26,17 @@ public class ChatController {
@Autowired
private ChatService chatService;
/**
* 上传文件
* @param file 文件
* @return 文件URL
*/
@PostMapping("/upload")
public Result<String> uploadVideo(@RequestParam MultipartFile file) {
String url = chatService.uploadChat(file);
return Result.success(ResultCode.SUCCESS, "文件上传成功", url);
}
/**
* 获取聊天记录
* @param targetUserId 目标用户ID
@@ -57,4 +68,37 @@ public class ChatController {
List<ChatSessionsVO> list = chatService.getSessionList(currentUserId);
return Result.success(ResultCode.SUCCESS, "获取会话列表成功", list);
}
/**
* 更新会话状态
* @param dto 会话状态
* @return 无
*/
@PostMapping("/session/status")
public Result<Void> updateSessionStatus(@RequestBody ChatSessionStatusDTO dto) {
chatService.updateSessionStatus(UserContext.getUserId(), dto);
return Result.success(ResultCode.SUCCESS, null);
}
/**
* 置顶会话
* @param dto 置顶状态
* @return 无
*/
@PostMapping("/session/top")
public Result<Void> updateTopStatus(@RequestBody ChatSessionTopDTO dto) {
chatService.updateTopStatus(UserContext.getUserId(), dto);
return Result.success(ResultCode.SUCCESS, null);
}
/**
* 静音会话
* @param dto 静音状态
* @return 无
*/
@PostMapping("/session/mute")
public Result<Void> updateMuteStatus(@RequestBody ChatSessionMuteDTO dto) {
chatService.updateMuteStatus(UserContext.getUserId(), dto);
return Result.success(ResultCode.SUCCESS, null);
}
}

View File

@@ -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;
}
}

View 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;
}
}

View File

@@ -0,0 +1,37 @@
package com.bao.dating.controller;
import com.bao.dating.common.Result;
import com.bao.dating.common.ResultCode;
import com.bao.dating.pojo.vo.IpLocationVO;
import com.bao.dating.service.Ip2LocationClientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/ip")
public class IpLocationController {
@Autowired
private Ip2LocationClientService ip2LocationClientService;
/**
* 前端访问接口获取IP地址位置信息
* @param ip 可选参数要查询的IP地址
* @return IP位置信息JSON格式
*/
@GetMapping("/location")
public Result<?> getIpLocation(@RequestParam(required = false) String ip) {
if (ip.isEmpty()){
return Result.error(ResultCode.PARAM_ERROR);
}
try {
// 调用工具类获取API响应
IpLocationVO ipLocationVo = ip2LocationClientService.getIpLocation(ip);
return Result.success(ResultCode.SUCCESS,ipLocationVo);
} catch (Exception e) {
// 异常处理:返回错误信息(实际项目建议封装统一响应格式)
return Result.error(ResultCode.SYSTEM_ERROR,e.getMessage());
}
}
}

View File

@@ -31,7 +31,6 @@ public class PostController {
* @param files 媒体文件数组
* @return 上传后的文件URL列表
*/
@Log
@PostMapping(value = "/upload", consumes = "multipart/form-data")
public Result<List<String>> uploadMedia(@RequestParam("files") MultipartFile[] files) {
List<String> fileUrls = postService.uploadMedia(files);
@@ -43,7 +42,6 @@ public class PostController {
* @param postDTO 动态信息
* @return 发布的动态对象
*/
@Log
@PostMapping( "/createPost")
public Result<Post> createPostJson(@RequestBody PostRequestDTO postDTO) {
// 调用 Service 层处理发布动态业务逻辑
@@ -57,7 +55,6 @@ public class PostController {
* @param postIds 动态ID
* @return 删除结果
*/
@Log
@PostMapping("/deletePost")
public Result<String> deleteById(@RequestBody List<Long> postIds){
int deletedCount = postService.deletePostById(postIds);
@@ -81,7 +78,6 @@ public class PostController {
* @param postRequestDTO 动态信息
* @return 更新后的动态对象
*/
@Log
@PostMapping("/{postId}/updatePost")
public Result<PostEditVO> updatePost(@PathVariable Long postId, @RequestBody PostRequestDTO postRequestDTO) {
PostEditVO result = postService.updatePost(postId, postRequestDTO);

View File

@@ -2,11 +2,13 @@ package com.bao.dating.controller;
import com.bao.dating.common.Result;
import com.bao.dating.common.ResultCode;
import com.bao.dating.pojo.entity.Post;
import com.bao.dating.pojo.entity.User;
import com.bao.dating.service.PostFavoriteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Map;
@RestController
@@ -14,6 +16,13 @@ import java.util.Map;
public class PostFavoriteController {
@Autowired
private PostFavoriteService postFavoriteService;
/**
* 收藏
* @param postId 动态ID
* @param user 当前登录用户对象
* @return 结果
*/
@PostMapping("/{post_id}/favorites")
public Result<Map<String,Long>> addPostFavorite(@PathVariable("post_id")Long postId, User user){
if (user == null){
@@ -22,6 +31,13 @@ public class PostFavoriteController {
Long userId = user.getUserId();
return postFavoriteService.postFavorite(userId,postId);
}
/**
* 取消收藏
* @param postId 动态id
* @param user 登录用户
* @return 结果
*/
@DeleteMapping("/{post_id}/favorites")
public Result<?> deletePostFavorite(@PathVariable("post_id")Long postId, User user){
if (user == null){
@@ -30,4 +46,21 @@ public class PostFavoriteController {
Long userId = user.getUserId();
return postFavoriteService.deletePostFavorite(userId, postId);
}
/**
* 展示所有收藏
* @param user 当前登录用户
* @return 结果
*/
@GetMapping("/showFavorites")
public Result<List<Post>> showPostFavorites(@RequestBody User user){
//校验参数
if (user == null){
return Result.error(ResultCode.PARAM_ERROR);
}
Long userId = user.getUserId();
List<Post> posts = postFavoriteService.selectAllFavorites(userId);
return Result.success(ResultCode.SUCCESS,posts);
}
}

View File

@@ -13,7 +13,9 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;
/**
* 用户接口
@@ -96,4 +98,120 @@ public class UserController {
UserInfoVO userInfoVO =userService.updateUserInfo(userInfoUpdateDTO);
return Result.success(ResultCode.SUCCESS, "用户信息更新成功", userInfoVO);
}
@PostMapping("/sendCode")
public Result sendCode(@RequestBody Map<String, String> body) {
String phone = body.get("phone");
userService.sendSmsCode(phone);
return Result.success(ResultCode.SUCCESS, "验证码发送成功");
}
@PostMapping("/loginByCode")
public Result loginByCode(@RequestBody Map<String, String> body){
boolean ok = userService.verifyCode(body.get("phone"), body.get("code"));
return ok ? Result.success(ResultCode.SUCCESS, "登录成功") : Result.error(ResultCode.SYSTEM_ERROR, "登录失败");
}
/**
* 登录
* @param body 登录参数
*/
@PostMapping("/loginPhone")
public Result<?> loginPhone(@RequestBody Map<String, String> body) {
String phone = body.get("phone");
String code = body.get("code");
//校验验证码
boolean verify = userService.verifyCode(phone, code);
if (!verify){
return Result.error(ResultCode.SYSTEM_ERROR, "验证码错误或已过期");
}
//登录
UserLoginVO vo = userService.loginByPhone(phone);
return Result.success(ResultCode.SUCCESS, "登录成功", vo);
}
@GetMapping("/nearby")
public Result<List<UserInfoVO>> nearby(@RequestParam(defaultValue = "5") double distance){
// 获取当前线程的用户 ID
Long userId = UserContext.getUserId();
if (userId == null) {
return Result.error(ResultCode.SYSTEM_ERROR, "用户未登录");
}
// 通过 UserID 获取当前用户的经纬度信息
UserInfoVO currentUser = userService.getUserInfo(userId);
if (currentUser == null) {
return Result.error(ResultCode.SYSTEM_ERROR, "用户信息获取失败");
}
// 获取当前用户的经纬度
Double latitude = currentUser.getLatitude();
Double longitude = currentUser.getLongitude();
// 检查经纬度是否为空
if (latitude == null || longitude == null) {
return Result.error(ResultCode.SYSTEM_ERROR, "用户经纬度信息未完善");
}
// 这里可以添加默认值,比如如果 latitude 或 longitude 为 null则设置为某个默认值例如 0
latitude = (latitude != null) ? latitude : 0.0;
longitude = (longitude != null) ? longitude : 0.0;
// 查询附近用户
List<UserInfoVO> nearbyUsers = userService.findNearbyUsers(latitude, longitude, distance);
// 返回成功结果
return Result.success(ResultCode.SUCCESS, "查询成功", nearbyUsers);
}
/**
* 用户注册
* @param userName 用户名称
* @param userPassword 用户密码
* @return 返回
*/
@PostMapping("/register")
public Result<?> userRegister(@RequestParam String userName , @RequestParam String userPassword){
//校验参数是否为空
if (userName.isEmpty() || userPassword.isEmpty()){
return Result.error(ResultCode.PARAM_ERROR);
}
if (!userService.registerUser(userName,userPassword)){
return Result.error(ResultCode.FAIL);
}
return Result.success(ResultCode.SUCCESS,"用户注册成功",null);
}
/**
* 通过邮箱登录
* @param email 邮箱
* @param code 验证码
* @return 用户信息
*/
@PostMapping("/emailLogin")
public Result<UserLoginVO> emailLogin(@RequestParam String email , @RequestParam String code){
//校验参数是否为空
if (email.isEmpty() || code.isEmpty()){
return Result.error(ResultCode.PARAM_ERROR);
}
UserLoginVO userLoginVO = userService.emailLogin(email, code);
if (userLoginVO == null){
return Result.error(ResultCode.FAIL,"请先注册用户或添加邮箱");
}
return Result.success(ResultCode.SUCCESS,"用户登录成功",userLoginVO);
}
/**
* 判断用户是否在线
* @param userId 用户ID
* @return 用户是否在线
*/
@GetMapping("/{userId}/online")
public Result<Boolean> isUserOnline(@PathVariable Long userId) {
boolean online = userService.isUserOnline(userId);
return Result.success(ResultCode.SUCCESS, "查询成功", online);
}
}

View File

@@ -1,11 +1,13 @@
package com.bao.dating.controller.websocket;
import com.bao.dating.message.WsMessage;
import com.bao.dating.pojo.dto.ChatRecallDTO;
import com.bao.dating.pojo.dto.ChatRecordSendDTO;
import com.bao.dating.pojo.vo.ChatRecordsVO;
import com.bao.dating.service.ChatService;
import com.bao.dating.session.WsSessionManager;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
@@ -35,7 +37,7 @@ public class ChatWebSocketHandler extends TextWebSocketHandler {
/**
* 用户建立连接(上线)
* @param session
* @param session WebSocketSession
*/
@Override
public void afterConnectionEstablished(WebSocketSession session) {
@@ -46,8 +48,8 @@ public class ChatWebSocketHandler extends TextWebSocketHandler {
/**
* 接收并处理消息
* @param session
* @param message
* @param session WebSocketSession
* @param message 消息
*/
@Override
protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception{
@@ -57,25 +59,41 @@ public class ChatWebSocketHandler extends TextWebSocketHandler {
log.error("WebSocket session 中未找到 userId");
return;
}
// 解析消息
WsMessage<ChatRecordSendDTO> wsMessage =
objectMapper.readValue(message.getPayload(),
new TypeReference<WsMessage<ChatRecordSendDTO>>(){});
// 处理私聊消息
if ("chat".equals(wsMessage.getType())) {
handlePrivateChat(senderUserId, wsMessage.getData());
JsonNode node = objectMapper.readTree(message.getPayload());
String type = node.get("type").asText();
// 根据消息类型解析消息
WsMessage wsMessage = objectMapper.readValue(message.getPayload(), WsMessage.class);
// 先获取消息类型,再根据类型进行相应处理和转换
if ("chat".equals(type)) {
// 处理私聊消息
WsMessage<ChatRecordSendDTO> chatWsMessage =
objectMapper.convertValue(node, new TypeReference<WsMessage<ChatRecordSendDTO>>(){});
handlePrivateChat(session, senderUserId, chatWsMessage.getData());
} else if ("recall".equals(type)) {
// 处理撤回消息
WsMessage<ChatRecallDTO> recallWsMessage =
objectMapper.convertValue(node, new TypeReference<WsMessage<ChatRecallDTO>>(){});
handleRecallMessage(session, senderUserId, recallWsMessage.getData());
}
}
/**
* 私聊处理
*/
private void handlePrivateChat(Long senderUserId, ChatRecordSendDTO dto) throws Exception {
private void handlePrivateChat(WebSocketSession session, Long senderUserId, ChatRecordSendDTO dto) throws Exception {
// 1. 消息入库 + 会话更新
ChatRecordsVO chatRecordsVO = chatService.createSession(senderUserId, dto);
if (chatRecordsVO == null){
WsMessage<String> errorMsg = new WsMessage<>();
errorMsg.setType("error");
errorMsg.setData("会话已删除,无法发送消息");
// 返回错误信息
session.sendMessage(new TextMessage(objectMapper.writeValueAsString(errorMsg)));
return;
}
// 2. 推送给接收方
WebSocketSession receiverSession =
sessionManager.getSession(dto.getReceiverUserId());
@@ -91,11 +109,51 @@ public class ChatWebSocketHandler extends TextWebSocketHandler {
}
}
/**
* 消息撤回处理
*/
private void handleRecallMessage(WebSocketSession session, Long senderUserId, Object data) throws Exception {
// 转 DTO
ChatRecallDTO dto = objectMapper.convertValue(data, ChatRecallDTO.class);
// 撤回逻辑
boolean success = chatService.recallMessage(senderUserId, dto.getChatId());
// 如果返回false说明消息撤回失败
if (!success) {
WsMessage<String> errorMsg = new WsMessage<>();
errorMsg.setType("error");
errorMsg.setData("撤回失败,消息可能无法撤回或不存在");
// 返回错误信息
session.sendMessage(new TextMessage(objectMapper.writeValueAsString(errorMsg)));
return;
}
// 创建撤回通知消息
WsMessage<ChatRecallDTO> pushMsg = new WsMessage<>();
pushMsg.setType("recall");
pushMsg.setData(dto);
// 通知自己
if (session.isOpen()) {
session.sendMessage(new TextMessage(objectMapper.writeValueAsString(pushMsg)));
}
// 通知对方
WebSocketSession receiverSession =
sessionManager.getSession(dto.getReceiverUserId());
if (receiverSession != null && receiverSession.isOpen()) {
receiverSession.sendMessage(new TextMessage(objectMapper.writeValueAsString(pushMsg))
);
}
}
/**
* 用户断开连接(下线)
* @param session
* @param status
* @param session WebSocketSession
* @param status 断开原因
*/
@Override
public void afterConnectionClosed(WebSocketSession session, CloseStatus status){

View File

@@ -44,10 +44,10 @@ public class TokenInterceptor implements HandlerInterceptor {
}
// 从 header 获取 token
String token = request.getHeader("token");
try {
log.info("jwt校验: {}", token);
// 验证 token 是否有效(包括是否过期)
if (!JwtUtil.validateToken(token)) {
log.error("Token无效或已过期");
@@ -66,10 +66,22 @@ public class TokenInterceptor implements HandlerInterceptor {
response.getWriter().write("登录已失效, 请重新登录");
return false;
}
// 解析 token
Long userId = Long.valueOf(JwtUtil.getSubjectFromToken(token));
// 检查用户是否被封禁
String banKey = "user:ban:" + userId;
if (Boolean.TRUE.equals(redisTemplate.hasKey(banKey))) {
String reason = String.valueOf(redisTemplate.opsForValue().get(banKey));
log.error("用户 {} 已被封禁,原因:{}", userId, reason);
response.setStatus(403);
response.setContentType("application/json;charset=UTF-8");
response.getWriter().write("账号已被封禁:" + reason);
return false;
}
// 从Redis获取存储的token进行比对
Object redisTokenObj = redisTemplate.opsForValue().get("login:token:" + userId);
String redisToken = redisTokenObj != null ? redisTokenObj.toString() : null;

View File

@@ -73,6 +73,14 @@ public class WsAuthInterceptor implements HandshakeInterceptor {
Long userId = Long.valueOf(userIdStr);
// 检查用户是否被封禁
String banKey = "user:ban:" + userId;
if (Boolean.TRUE.equals(redisTemplate.hasKey(banKey))) {
String reason = String.valueOf(redisTemplate.opsForValue().get(banKey));
log.error("WebSocket拒绝用户 {} 被封禁,原因:{}", userId, reason);
return false;
}
// 从Redis获取存储的token进行比对
String redisTokenKey = "login:token:" + userId;
Object redisTokenObj = redisTemplate.opsForValue().get(redisTokenKey);

View File

@@ -32,4 +32,20 @@ public interface ChatRecordsMapper {
* @return 影响行数
*/
int markMessagesAsRead(ChatMarkReadDTO markReadDTO);
/**
* 根据ID查询聊天记录
* @param chatId 聊天记录ID
* @return 聊天记录
*/
ChatRecords selectById(@Param("chatId") Long chatId);
/**
* 撤回聊天记录
* @param chatId 聊天记录ID
* @param senderUserId 发送者ID
* @return 影响行数
*/
int recallMessage(@Param("chatId") Long chatId,
@Param("senderUserId") Long senderUserId);
}

View File

@@ -5,6 +5,7 @@ import com.bao.dating.pojo.entity.ChatRecords;
import com.bao.dating.pojo.entity.ChatSessions;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import java.util.List;
@@ -18,6 +19,15 @@ public interface ChatSessionsMapper {
*/
int upsertSessionForSender(ChatSessions chatSessions);
/**
* 获取会话
* @param userId 用户ID
* @param targetUserId 目标用户ID
* @return 会话
*/
@Select("SELECT * FROM chat_sessions WHERE user_id = #{userId} AND target_user_id = #{targetUserId} LIMIT 1")
ChatSessions getSession(@Param("userId") Long userId, @Param("targetUserId") Long targetUserId);
/**
* 如果接收方存在会话则更新,不存在则创建
* @param chatSessions 会话
@@ -42,4 +52,37 @@ public interface ChatSessionsMapper {
* @return 会话列表
*/
List<ChatSessions> selectSessionsByUserId(@Param("userId") Long userId);
/**
* 更新会话状态
* @param userId 用户ID
* @param targetUserId 目标用户ID
* @param sessionStatus 会话状态
* @return 影响行数
*/
int updateSessionStatus(@Param("userId") Long userId,
@Param("targetUserId") Long targetUserId,
@Param("sessionStatus") Integer sessionStatus);
/**
* 更新会话置顶状态
* @param userId 用户ID
* @param targetUserId 目标用户ID
* @param topStatus 置顶状态
* @return 影响行数
*/
int updateTopStatus(@Param("userId") Long userId,
@Param("targetUserId") Long targetUserId,
@Param("topStatus") Integer topStatus);
/**
* 更新会话免打扰状态
* @param userId 用户ID
* @param targetUserId 目标用户ID
* @param muteStatus 免打扰状态
* @return 影响行数
*/
int updateMuteStatus(@Param("userId") Long userId,
@Param("targetUserId") Long targetUserId,
@Param("muteStatus") Integer muteStatus);
}

View File

@@ -5,18 +5,18 @@ import com.bao.dating.pojo.entity.Comments;
import org.apache.ibatis.annotations.*;
import java.util.List;
@Mapper
public interface CommentsMapper {
// 添加评论
@Insert("INSERT INTO comments(content, user_id, post_id, created_at) VALUES(#{content}, #{user_id}, #{post_id}, #{created_at})")
@Insert("INSERT INTO dating.comments(content, user_id, post_id, created_at) VALUES(#{content}, #{user_id}, #{post_id}, #{created_at})")
int addComment(Comments comments);
// 删除评论
@Delete("DELETE FROM comments WHERE user_id = #{user_id}")
@Delete("DELETE FROM dating.comments WHERE user_id = #{user_id}")
int deleteComments(@Param("user_id") Long user_id);
// 根据动态ID查询评论列表
@Select("SELECT * FROM comments WHERE post_id = #{post_id} ORDER BY created_at DESC")
@Select("SELECT * FROM dating.comments WHERE post_id = #{post_id} ORDER BY created_at DESC")
List<Comments> getCommentsByPostId(@Param("post_id") Long post_id);
/**

View 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);
}

View File

@@ -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);
}

View File

@@ -1,5 +1,6 @@
package com.bao.dating.mapper;
import com.bao.dating.pojo.entity.Post;
import com.bao.dating.pojo.entity.PostFavorite;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
@@ -7,10 +8,12 @@ import org.apache.ibatis.annotations.Param;
import java.util.List;
@Mapper
public interface PostFavoriteMapper {
public interface PostFavoriteMapper {
//查询当前已收藏所有用户
List<Long> selectUserIDByPostID(@Param("postId") Long postId);
//添加收藏
int addPostFavorite(PostFavorite postFavorite);
//删除收藏
int deletePostFavorite(@Param("postId") Long postId);
/**
@@ -20,4 +23,6 @@ public interface PostFavoriteMapper {
*/
int deleteFavoritesByPostIds(@Param("postIds") List<Long> postIds);
//查询用户所有收藏
List<Post> showAllFavorites(@Param("userid")Long userid);
}

View File

@@ -24,8 +24,30 @@ public interface PostMapper {
* 根据ID修改动态状态
*
* @param postIds 动态ID
* @param userId 用户ID
*/
int updatePublicById(@Param("postIds") List<Long> postIds, @Param("userId") Long userId);
int updatePublicById(@Param("postIds") List<Long> postIds, @Param("userId") Long userId);
/**
* 根据动态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查询动态

View File

@@ -0,0 +1,43 @@
package com.bao.dating.mapper;
import com.bao.dating.pojo.entity.UserBan;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
@Mapper
public interface UserBanMapper {
/**
* 新增封禁记录
* @param userBan 封禁记录
* @return 影响行数
*/
int insertBan(UserBan userBan);
/**
* 查询是否存在生效中的封禁
* @param userId 用户ID
* @return 存在返回1不存在返回0
*/
int existsActiveBan(@Param("userId") Long userId);
/**
* 查询生效中的封禁记录
* @param userId 用户ID
* @return 封禁记录
*/
UserBan selectActiveBan(@Param("userId") Long userId);
/**
* 解封用户
* @param userId 用户ID
* @return 影响行数
*/
int unbanUser(@Param("userId") Long userId);
/**
* 定时任务:过期自动解封
* @return 影响行数
*/
int updateExpiredBans();
}

View File

@@ -2,7 +2,11 @@ package com.bao.dating.mapper;
import com.bao.dating.pojo.dto.UserInfoDTO;
import com.bao.dating.pojo.entity.User;
import com.bao.dating.pojo.vo.UserInfoVO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import java.util.List;
/**
* 用户Mapper
@@ -33,4 +37,35 @@ public interface UserMapper {
*/
void updateUserInfoByUserId(UserInfoDTO userInfoUpdateDTO);
/**
* 添加用户
* @param user 用户对象
* @return 受影响行数
*/
int saveUser(User user);
/**
* 查询最大用户id
* @return 用户id
*/
Long selectMaxId();
/**
* 根据邮箱查询用户
* @param email 用户邮箱
* @return 用户信息
*/
User selectByUserEmailUser(@Param("userEmail") String email);
User selectByPhone(@Param("phone") String phone);
/**
* 根据经纬度范围查询用户
* @param minLat 最小纬度
* @param maxLat 最大纬度
* @param minLng 最小经度
* @param maxLng 最大经度
* @return 用户列表
*/
List<UserInfoVO> findByLatLngRange(@Param("minLat") double minLat, @Param("maxLat") double maxLat, @Param("minLng") double minLng, @Param("maxLng") double maxLng);
}

View File

@@ -0,0 +1,13 @@
package com.bao.dating.pojo.dto;
import lombok.Data;
/**
* 聊天记录撤回参数
* @author lenovo
*/
@Data
public class ChatRecallDTO {
private Long chatId;
private Long receiverUserId;
}

View File

@@ -0,0 +1,13 @@
package com.bao.dating.pojo.dto;
import lombok.Data;
/**
* 聊天会话静音参数
* @author lenovo
*/
@Data
public class ChatSessionMuteDTO {
private Long targetUserId;
private Integer muteStatus;
}

View File

@@ -0,0 +1,13 @@
package com.bao.dating.pojo.dto;
import lombok.Data;
/**
* 会话状态参数
* @author lenovo
*/
@Data
public class ChatSessionStatusDTO {
private Long targetUserId;
private Integer sessionStatus;
}

View File

@@ -0,0 +1,13 @@
package com.bao.dating.pojo.dto;
import lombok.Data;
/**
* 会话置顶参数
* @author lenovo
*/
@Data
public class ChatSessionTopDTO {
private Long targetUserId;
private Integer topStatus;
}

View File

@@ -0,0 +1,13 @@
package com.bao.dating.pojo.dto;
import lombok.Data;
/**
* 用户封禁数据传输对象
* @author KilLze
*/
@Data
public class UserBanDTO {
private Long userId;
private String reason;
private Integer banDays;
}

View 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; // 更新时间
}

View 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;
}

View File

@@ -43,4 +43,8 @@ public class User implements Serializable {
private String userEmail;
private String userPhone;
private Double latitude; // 纬度
private Double longitude; // 经度
}

View File

@@ -0,0 +1,30 @@
package com.bao.dating.pojo.entity;
import lombok.Data;
import java.time.LocalDateTime;
/**
* 用户封禁记录
* @author KilLze
*/
@Data
public class UserBan {
private Long id;
private Long userId;
private String reason;
private LocalDateTime banStartTime;
private LocalDateTime banEndTime;
/**
* 1:封禁中 0:已解封
*/
private Integer status;
private LocalDateTime createTime;
}

View File

@@ -29,4 +29,6 @@ public class ChatSessionsVO {
private Integer topStatus;
/** 免打扰状态 */
private Integer muteStatus;
/** 会话状态 */
private Boolean online;
}

View File

@@ -0,0 +1,30 @@
package com.bao.dating.pojo.vo;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
@JsonIgnoreProperties(ignoreUnknown = true)
public class IpLocationVO {
// 国家
@JsonProperty("country_name")
private String countryName;
// 省份/地区
@JsonProperty("region_name")
private String regionName;
// 城市
@JsonProperty("city_name")
private String cityName;
// 纬度
private String latitude;
// 经度
private String longitude;
// ISP运营商
@JsonProperty("isp")
private String isp;
}

View File

@@ -24,4 +24,7 @@ public class UserInfoVO implements Serializable {
private String signature;
private LocalDateTime updatedAt;
private LocalDateTime createdAt;
private Double latitude;
private Double longitude;
private Boolean online;
}

View File

@@ -1,10 +1,10 @@
package com.bao.dating.service;
import com.bao.dating.common.result.PageResult;
import com.bao.dating.pojo.dto.ChatHistoryQueryDTO;
import com.bao.dating.pojo.dto.ChatRecordSendDTO;
import com.bao.dating.pojo.dto.*;
import com.bao.dating.pojo.vo.ChatRecordsVO;
import com.bao.dating.pojo.vo.ChatSessionsVO;
import org.springframework.web.multipart.MultipartFile;
import java.time.LocalDateTime;
import java.util.List;
@@ -22,6 +22,13 @@ public interface ChatService {
*/
ChatRecordsVO createSession(Long senderUserId, ChatRecordSendDTO dto);
/**
* 上传媒体文件
* @param file 文件
* @return 文件URL列表
*/
String uploadChat(MultipartFile file);
/**
* 获取聊天记录
* @param dto 查询参数
@@ -43,4 +50,33 @@ public interface ChatService {
* @return 会话列表
*/
List<ChatSessionsVO> getSessionList(Long currentUserId);
/**
* 更新会话状态
* @param userId 用户ID
* @param dto 更新参数
*/
void updateSessionStatus(Long userId, ChatSessionStatusDTO dto);
/**
* 置顶会话
* @param userId 用户ID
* @param dto 置顶参数
*/
void updateTopStatus(Long userId, ChatSessionTopDTO dto);
/**
* 免打扰会话
* @param userId 用户ID
* @param dto 免打扰参数
*/
void updateMuteStatus(Long userId, ChatSessionMuteDTO dto);
/**
* 撤回消息
* @param senderUserId 发送方ID
* @param chatId 聊天记录ID
* @return 撤回结果
*/
boolean recallMessage(Long senderUserId, Long chatId);
}

View 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);
}

View File

@@ -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);
}

View File

@@ -0,0 +1,7 @@
package com.bao.dating.service;
import com.bao.dating.pojo.vo.IpLocationVO;
public interface Ip2LocationClientService {
IpLocationVO getIpLocation(String ip)throws Exception;
}

View File

@@ -1,10 +1,13 @@
package com.bao.dating.service;
import com.bao.dating.common.Result;
import com.bao.dating.pojo.entity.Post;
import java.util.List;
import java.util.Map;
public interface PostFavoriteService {
Result<Map<String,Long>> postFavorite(Long userid,Long postId);
Result<?> deletePostFavorite(Long userid,Long postId);
List<Post> selectAllFavorites(Long userid);
}

View File

@@ -0,0 +1,31 @@
package com.bao.dating.service;
import com.bao.dating.pojo.dto.UserBanDTO;
import com.bao.dating.pojo.entity.UserBan;
/**
* 用户封禁服务接口
* @author KilLze
*/
public interface UserBanService {
/**
* 封禁用户
* @param userBanDTO 用户封禁信息
*
*/
void banUser(UserBanDTO userBanDTO);
/**
* 解封用户
* @param userId 用户ID
*/
void unbanUser(Long userId);
/**
* 查询封禁信息
* @param userId 用户ID
* @return 封禁信息
*/
UserBan getActiveBan(Long userId);
}

View File

@@ -6,6 +6,8 @@ import com.bao.dating.pojo.vo.UserInfoVO;
import com.bao.dating.pojo.vo.UserLoginVO;
import org.springframework.web.multipart.MultipartFile;
import java.util.List;
/**
* 用户服务接口
* @author KilLze
@@ -53,10 +55,48 @@ public interface UserService {
*/
UserInfoVO updateUserInfo(UserInfoDTO userInfoUpdateDTO);
/**
* 用户注册
* @param userName 用户名称
* @param userPassword 用户密码
* @return 用户信息
*/
Boolean registerUser(String userName, String userPassword);
/**
* 邮箱登录
* @param email 邮箱
* @param code 验证码
* @return 登录结果
*/
UserLoginVO emailLogin(String email, String code);
/**
* 根据用户ID查询用户信息
* @param userId 用户ID
* @return 用户
*/
UserInfoDTO getUserInfoById(Long userId);
void sendSmsCode(String phone);
boolean verifyCode(String phone, String code);
UserLoginVO loginByPhone(String phone);
/**
* 获取指定经纬度范围内的用户
* @param lat 用户纬度
* @param lng 用户经度
* @param radiusKm 半径 km
* @return 用户列表
*/
List<UserInfoVO> findNearbyUsers(double lat,double lng,double radiusKm);
/**
* 判断用户是否在线
* @param userId 用户ID
* @return 是否在线
*/
boolean isUserOnline(Long userId);
}

View File

@@ -1,26 +1,34 @@
package com.bao.dating.service.impl;
import com.bao.dating.common.aliyun.AliOssUtil;
import com.bao.dating.common.result.AliOssResult;
import com.bao.dating.context.UserContext;
import com.bao.dating.mapper.ChatRecordsMapper;
import com.bao.dating.mapper.ChatSessionsMapper;
import com.bao.dating.pojo.dto.ChatHistoryQueryDTO;
import com.bao.dating.pojo.dto.ChatMarkReadDTO;
import com.bao.dating.pojo.dto.ChatRecordSendDTO;
import com.bao.dating.pojo.dto.UserInfoDTO;
import com.bao.dating.pojo.dto.*;
import com.bao.dating.pojo.entity.ChatRecords;
import com.bao.dating.pojo.entity.ChatSessions;
import com.bao.dating.pojo.vo.ChatRecordsVO;
import com.bao.dating.pojo.vo.ChatSessionsVO;
import com.bao.dating.service.ChatService;
import com.bao.dating.service.UserService;
import com.bao.dating.util.FileUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;
@@ -30,13 +38,16 @@ import java.util.stream.Collectors;
*/
@Slf4j
@Service
public class ChatServiceImpl implements ChatService {
public class ChatServiceImpl implements ChatService {
@Autowired
private ChatRecordsMapper chatRecordsMapper;
@Autowired
private ChatSessionsMapper chatSessionsMapper;
@Autowired
private AliOssUtil ossUtil;
@Autowired
private UserService userService;
@@ -49,6 +60,13 @@ public class ChatServiceImpl implements ChatService {
@Override
@Transactional(rollbackFor = Exception.class)
public ChatRecordsVO createSession(Long senderUserId, ChatRecordSendDTO dto) {
ChatSessions session = chatSessionsMapper.getSession(senderUserId, dto.getReceiverUserId());
if (session != null && session.getSessionStatus() == 3) {
log.warn("会话已删除,无法发送消息");
return null;
}
ChatRecords record = new ChatRecords();
record.setSenderUserId(senderUserId);
record.setReceiverUserId(dto.getReceiverUserId());
@@ -102,15 +120,55 @@ public class ChatServiceImpl implements ChatService {
chatSessionsMapper.upsertSessionForReceiver(sessions);
// 3. 返回 VO
ChatRecordsVO vo = new ChatRecordsVO();
BeanUtils.copyProperties(record, vo);
return vo;
}
/**
* 上传媒体文件
* @return 上传后的文件URL
*/
@Override
public String uploadChat(MultipartFile file) {
// 参数校验
if (file == null || file.isEmpty()) {
throw new RuntimeException("文件不存在");
}
String originalFilename = file.getOriginalFilename();
if (originalFilename == null) {
throw new RuntimeException("文件名非法");
}
String fileType = FileUtil.getFileType(originalFilename);
// 仅支持图片和视频文件上传
if (!AliOssResult.IMAGE.equals(fileType) && !AliOssResult.VIDEO.equals(fileType)) {
throw new RuntimeException("仅支持图片和视频文件上传");
}
//生成 OSS 路径
String extension = FileUtil.getFileExtension(originalFilename);
String fileName = UUID.randomUUID().toString().replace("-", "") + "." + extension;
Long userId = UserContext.getUserId();
String objectKey = "chat/" + userId + "/" + fileName;
try {
byte[] fileBytes = file.getBytes();
String ossUrl = ossUtil.upload(fileBytes, objectKey);
if (ossUrl == null || ossUrl.isEmpty()) {
throw new RuntimeException("图片上传失败");
}
return ossUrl;
} catch (Exception e) {
throw new RuntimeException("上传图片失败", e);
}
}
/**
* 获取聊天记录
* @return 聊天记录列表
@@ -136,6 +194,9 @@ public class ChatServiceImpl implements ChatService {
}).collect(Collectors.toList());
}
/**
* 标记聊天消息为已读
*/
@Override
@Transactional(rollbackFor = Exception.class)
public void markChatMessagesAsRead(Long currentUserId, Long targetUserId) {
@@ -175,8 +236,91 @@ public class ChatServiceImpl implements ChatService {
vo.setSessionName("用户" + session.getTargetUserId());
vo.setAvatarUrl(null);
}
vo.setOnline(userService.isUserOnline(vo.getTargetUserId()));
return vo;
}).collect(Collectors.toList());
}
/**
* 更新会话状态
* @param userId 用户ID
* @param dto 会话状态
*/
@Override
public void updateSessionStatus(Long userId, ChatSessionStatusDTO dto) {
chatSessionsMapper.updateSessionStatus(
userId,
dto.getTargetUserId(),
dto.getSessionStatus()
);
}
/**
* 置顶会话
* @param userId 用户ID
* @param dto 置顶状态
*/
@Override
public void updateTopStatus(Long userId, ChatSessionTopDTO dto) {
chatSessionsMapper.updateTopStatus(
userId,
dto.getTargetUserId(),
dto.getTopStatus()
);
}
/**
* 免打扰会话
* @param userId 用户ID
* @param dto 免打扰状态
*/
@Override
public void updateMuteStatus(Long userId, ChatSessionMuteDTO dto) {
chatSessionsMapper.updateMuteStatus(
userId,
dto.getTargetUserId(),
dto.getMuteStatus()
);
}
/**
* 撤回消息
* @param senderUserId 发送者用户ID
* @param chatId 聊天记录ID
* @return 是否成功
*/
@Override
@Transactional(rollbackFor = Exception.class)
public boolean recallMessage(Long senderUserId, Long chatId) {
// 查询聊天记录
ChatRecords record = chatRecordsMapper.selectById(chatId);
// 消息不存在
if (record == null) {
log.info("消息不存在chatId: {}", chatId);
return false;
}
// 只能撤回自己发的
if (!record.getSenderUserId().equals(senderUserId)) {
log.info("不能撤回别人发的消息chatId: {},当前用户: {},消息发送者: {}", chatId, senderUserId, record.getSenderUserId());
return false;
}
// 已撤回或已删除
if (record.getMessageStatus() != 1) {
log.info("消息已撤回或已删除chatId: {},当前状态: {}", chatId, record.getMessageStatus());
return false;
}
// 时间限制2 分钟)
Duration duration = Duration.between(record.getSendTime(), LocalDateTime.now());
if (duration.toMinutes() > 2) {
log.info("消息已超过 2 分钟不能撤回chatId: {},发送时间: {}", chatId, record.getSendTime());
return false;
}
return chatRecordsMapper.recallMessage(chatId, senderUserId) > 0;
}
}

View File

@@ -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);
}
}

View File

@@ -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);
}
}

View File

@@ -0,0 +1,67 @@
package com.bao.dating.service.impl;
import com.bao.dating.common.ip2location.Ip2LocationConfig;
import com.bao.dating.pojo.vo.IpLocationVO;
import com.bao.dating.service.Ip2LocationClientService;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import java.net.URI;
@Service
public class Ip2LocationClientServiceImpl implements Ip2LocationClientService {
@Autowired
private Ip2LocationConfig ip2LocationConfig;
// Jackson的ObjectMapper用于JSON解析
private final ObjectMapper objectMapper = new ObjectMapper();
/**
* 调用API并只返回核心位置信息
* @param ip 要查询的IP地址
* @return 精简的位置信息实体类
* @throws Exception 异常
*/
@Override
public IpLocationVO getIpLocation(String ip) throws Exception {
// 1. 构建请求URL
URIBuilder uriBuilder = new URIBuilder(ip2LocationConfig.getUrl());
uriBuilder.addParameter("key", ip2LocationConfig.getKey());
if (ip != null && !ip.trim().isEmpty()) {
uriBuilder.addParameter("ip", ip);
}
URI uri = uriBuilder.build();
// 2. 配置超时时间(逻辑和之前一致)
RequestConfig requestConfig = RequestConfig.custom()
.setConnectTimeout(ip2LocationConfig.getTimeout())
.setSocketTimeout(ip2LocationConfig.getTimeout())
.build();
// 3. 发送请求并解析响应
try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
HttpGet httpGet = new HttpGet(uri);
httpGet.setConfig(requestConfig);
try (CloseableHttpResponse response = httpClient.execute(httpGet)) {
if (response.getStatusLine().getStatusCode() == 200) {
String jsonStr = EntityUtils.toString(response.getEntity(), "UTF-8");
// 核心将完整JSON解析为只包含位置信息的VO类
return objectMapper.readValue(jsonStr, IpLocationVO.class);
} else {
throw new RuntimeException("调用API失败状态码" + response.getStatusLine().getStatusCode());
}
}
}
}
}

View File

@@ -4,6 +4,7 @@ import com.bao.dating.common.Result;
import com.bao.dating.common.ResultCode;
import com.bao.dating.mapper.PostFavoriteMapper;
import com.bao.dating.mapper.PostMapper;
import com.bao.dating.pojo.entity.Post;
import com.bao.dating.pojo.entity.PostFavorite;
import com.bao.dating.service.PostFavoriteService;
import org.springframework.beans.factory.annotation.Autowired;
@@ -58,4 +59,18 @@ public class PostFavoriteServiceImpl implements PostFavoriteService {
postMapper.decreaseFavoriteCount(postId);
return null;
}
/**
* 展示所有收藏
* @param userid 用户id
* @return 查询到的结果
*/
@Override
public List<Post> selectAllFavorites(Long userid) {
if (userid == null){
return null;
}
return postFavoriteMapper.showAllFavorites(userid);
}
}

View File

@@ -205,6 +205,19 @@ public class PostServiceImpl implements PostService {
if (CollectionUtils.isEmpty(postIds)) {
return 0;
}
// 遍历所有要删除的帖子ID验证权限
for (Long postId : postIds) {
Post post = postMapper.selectById(postId);
if (post == null) {
throw new RuntimeException("动态不存在");
}
// 验证用户权限
if (post.getUserId() == null || !post.getUserId().equals(userId)) {
throw new RuntimeException("无权限删除此动态");
}
}
int affected = postMapper.updatePublicById(postIds, userId);
if (affected == 0) {

View File

@@ -0,0 +1,74 @@
package com.bao.dating.service.impl;
import com.bao.dating.mapper.UserBanMapper;
import com.bao.dating.pojo.dto.UserBanDTO;
import com.bao.dating.pojo.entity.UserBan;
import com.bao.dating.service.UserBanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.util.concurrent.TimeUnit;
@Service
public class UserBanServiceImpl implements UserBanService {
@Autowired
private UserBanMapper userBanMapper;
@Autowired
private RedisTemplate<String, Object> redisTemplate;
@Override
public void banUser(UserBanDTO userBanDTO) {
// 已被封禁,直接拒绝
if (userBanMapper.existsActiveBan(userBanDTO.getUserId()) > 0) {
throw new RuntimeException("用户已处于封禁状态");
}
LocalDateTime now = LocalDateTime.now();
LocalDateTime endTime = userBanDTO.getBanDays() == null ? null : now.plusDays(userBanDTO.getBanDays());
// 1. 写数据库
UserBan ban = new UserBan();
ban.setUserId(userBanDTO.getUserId());
ban.setReason(userBanDTO.getReason());
ban.setBanStartTime(now);
ban.setBanEndTime(endTime);
ban.setStatus(1);
userBanMapper.insertBan(ban);
// 2. 写 Redis
String key = "user:ban:" + userBanDTO.getUserId();
if (userBanDTO.getBanDays() == null) {
redisTemplate.opsForValue().set(key, userBanDTO.getReason());
} else {
redisTemplate.opsForValue().set(key, userBanDTO.getReason(), userBanDTO.getBanDays(), TimeUnit.DAYS);
}
// 3. 踢下线
redisTemplate.delete("login:token:" + userBanDTO.getUserId());
}
/**
* 解封用户
*/
@Override
public void unbanUser(Long userId) {
// 更新数据库
userBanMapper.unbanUser(userId);
// 删除 Redis
redisTemplate.delete("user:ban:" + userId);
}
/**
* 获取用户封禁信息
*/
@Override
public UserBan getActiveBan(Long userId) {
return userBanMapper.selectActiveBan(userId);
}
}

View File

@@ -3,6 +3,7 @@ package com.bao.dating.service.impl;
import com.bao.dating.common.aliyun.AliOssUtil;
import com.bao.dating.common.aliyun.GreenImageScan;
import com.bao.dating.common.aliyun.GreenTextScan;
import com.bao.dating.common.aliyun.SmsUtil;
import com.bao.dating.common.result.AliOssResult;
import com.bao.dating.common.result.GreenAuditResult;
import com.bao.dating.context.UserContext;
@@ -13,13 +14,18 @@ import com.bao.dating.pojo.entity.User;
import com.bao.dating.pojo.vo.UserInfoVO;
import com.bao.dating.pojo.vo.UserLoginVO;
import com.bao.dating.service.UserService;
import com.bao.dating.service.VerificationCodeService;
import com.bao.dating.util.*;
import com.bao.dating.util.CodeUtil;
import com.bao.dating.util.FileUtil;
import com.bao.dating.util.JwtUtil;
import com.bao.dating.util.MD5Util;
import com.bao.dating.util.UserBanUtil;
import io.jsonwebtoken.Claims;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
@@ -36,6 +42,10 @@ import java.util.concurrent.TimeUnit;
@Service
public class UserServiceImpl implements UserService {
@Autowired
private SmsUtil smsUtil;
@Autowired
private AliOssUtil ossUtil;
@@ -51,6 +61,15 @@ public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Autowired
private StringRedisTemplate stringRedisTemplate;
@Autowired
private VerificationCodeService verificationCodeService;
@Autowired
private UserBanUtil userBanValidator;
/**
* 用户登录
*
@@ -77,6 +96,8 @@ public class UserServiceImpl implements UserService {
if (!match) {
throw new RuntimeException("密码错误");
}
// 用户封禁验证
userBanValidator.validateUserNotBanned(user.getUserId());
// 生成token
String token = JwtUtil.generateToken(String.valueOf(user.getUserId()));
@@ -103,6 +124,9 @@ public class UserServiceImpl implements UserService {
@Override
public void logout(String token) {
Claims claims = JwtUtil.getClaimsFromToken(token);
// 获取token信息
String subject = claims.getSubject();
// 获取token的过期时间
Date expiration = claims.getExpiration();
// 判断 token 是否已过期
long ttl = expiration.getTime() - System.currentTimeMillis();
@@ -111,6 +135,10 @@ public class UserServiceImpl implements UserService {
return;
}
// 从Redis中删除登录token记录
String loginTokenKey = "login:token:" + subject;
redisTemplate.delete(loginTokenKey);
String logoutKey = "jwt:blacklist:" + token;
redisTemplate.opsForValue().set(
logoutKey,
@@ -224,6 +252,7 @@ public class UserServiceImpl implements UserService {
}
}
/**
* 更新用户信息
*
@@ -311,6 +340,65 @@ public class UserServiceImpl implements UserService {
return userInfoVO;
}
/**
* 用户注册
* @param userName 用户名称
* @param userPassword 用户密码
* @return 注册结果
*/
@Override
public Boolean registerUser(String userName, String userPassword) {
//校验参数是否为空
if (userName.isEmpty() || userPassword.isEmpty()){
return false;
}
//查看数据库是否存在已注册用户
User user = userMapper.getByUsername(userName);
if (user != null){
return false;
}
//将用户数据存入数据库
String salt = "lyy123";
String passwordHash = MD5Util.encryptWithSalt(userPassword, salt);
//查询最大用户id
Long maxId = userMapper.selectMaxId();
User saveUser = new User();
saveUser.setUserId(maxId+1);
saveUser.setUserName(userName);
saveUser.setPasswordHash(passwordHash);
saveUser.setSalt(salt);
saveUser.setUpdatedAt(LocalDateTime.now());
saveUser.setCreatedAt(LocalDateTime.now());
int count = userMapper.saveUser(saveUser);
return count > 0;
}
/**
* 邮箱登录
* @param email 邮箱
* @param code 验证码
* @return 脱敏用户信息
*/
@Override
public UserLoginVO emailLogin(String email, String code) {
User user = userMapper.selectByUserEmailUser(email);
if (user == null) {
return null;
}
boolean flag = verificationCodeService.verifyEmailCode(email, code);
if (!flag) {
return null;
}
// 生成token
String token = JwtUtil.generateToken(String.valueOf(user.getUserId()));
UserLoginVO userLoginVO = new UserLoginVO();
userLoginVO.setUserId(user.getUserId());
userLoginVO.setNickname(user.getNickname());
userLoginVO.setToken(token);
return userLoginVO;
}
/**
* 根据用户ID获取用户昵称和头像
*
@@ -329,4 +417,104 @@ public class UserServiceImpl implements UserService {
dto.setNickname(user.getNickname());
return dto;
}
}
// 发送短信验证码
@Override
public void sendSmsCode(String phone) {
//防刷60 秒内只能发一次
String key = "sms:code:" + phone;
Boolean exists = stringRedisTemplate.hasKey(key);
if (Boolean.TRUE.equals(exists)){
throw new RuntimeException("请勿频繁发送验证码");
}
// 生成验证码
String code = CodeUtil.generateCode();
// 发送短信
smsUtil.sendVerificationCode(phone, code);
//存 Redis5分钟过期
stringRedisTemplate.opsForValue()
.set(key,code, 5, TimeUnit.MINUTES);
}
// 校验验证码
@Override
public boolean verifyCode(String phone, String code) {
String key = "sms:code:" + phone;
String realCode = stringRedisTemplate.opsForValue().get(key);
//过期,未发送
if (realCode ==null){
return false;
}
//不匹配
if (!realCode.equals(code)){
return false;
}
// 校验成功,删除验证码(一次性)
stringRedisTemplate.delete(key);
return true;
}
@Override
public UserLoginVO loginByPhone(String phone) {
//根据手机号查询用户
User user = userMapper.selectByPhone(phone);
//创建token
String token = JwtUtil.generateToken(user.getUserId().toString());
//封装返回结果
UserLoginVO VO = new UserLoginVO();
VO.setUserId(user.getUserId());
VO.setToken(token);
return VO;
}
@Override
public List<UserInfoVO> findNearbyUsers(double lat, double lng, double radiusKm) {
//先用经纬度范围筛选(矩形框,提高性能)
double delta = radiusKm / 111.0; // 1° ≈ 111km
double minLat = lat - delta;// 最小纬度
double maxLat = lat + delta;// 最大纬度
double minLng = lng - delta;// 最小经度
double maxLng = lng + delta;// 最大经度
// 打印经纬度范围
System.out.println("Min Latitude: " + minLat + ", Max Latitude: " + maxLat);
System.out.println("Min Longitude: " + minLng + ", Max Longitude: " + maxLng);
List<UserInfoVO> byLatLngRange = userMapper.findByLatLngRange(minLat, maxLat, minLng, maxLng);
//精确计算距离,筛选在半径内的用户
List<UserInfoVO> result = new ArrayList<>();
for (UserInfoVO u:byLatLngRange){
// 检查用户是否有经纬度信息
if (u.getLatitude() != null && u.getLongitude() != null) {
double distance = DistanceUtil.calculate(lat, lng, u.getLatitude(), u.getLongitude());
if (distance <= radiusKm){
result.add(u);
}
}
}
return result;
}
@Override
public boolean isUserOnline(Long userId) {
if (userId == null) {
return false;
}
// 1. 是否被封禁
String banKey = "user:ban:" + userId;
if (Boolean.TRUE.equals(redisTemplate.hasKey(banKey))) {
return false;
}
// 2. 是否存在登录 token
String tokenKey = "login:token:" + userId;
Boolean online = redisTemplate.hasKey(tokenKey);
return Boolean.TRUE.equals(online);
}
}

View File

@@ -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);
}
}
}

View File

@@ -0,0 +1,26 @@
package com.bao.dating.task;
import com.bao.dating.mapper.UserBanMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
@Slf4j
@Component
@EnableScheduling
public class UserBanScheduleTask {
@Autowired
private UserBanMapper userBanMapper;
/**
* 每天凌晨 3 点同步过期封禁
*/
@Scheduled(cron = "0 0 3 * * ?")
public void syncExpiredUserBan() {
int rows = userBanMapper.updateExpiredBans();
log.info("封禁同步任务执行完成,解封 {} 个用户", rows);
}
}

View File

@@ -0,0 +1,12 @@
package com.bao.dating.util;
import java.util.Random;
public class CodeUtil {
// 生成6位数字验证码
public static String generateCode() {
Random random = new Random();
int code=100000+random.nextInt(900000);
return String.valueOf(code);
}
}

View File

@@ -0,0 +1,29 @@
package com.bao.dating.util;
//Haversine 公式(标准写法)
public class DistanceUtil {
private static final double EARTH_RADIUS = 6371.0; // 地球半径 km
/**
* 计算两点之间距离单位km
*/
public static double calculate(
double lat1, double lon1,
double lat2, double lon2) {
double dLat = Math.toRadians(lat2 - lat1);
double dLon = Math.toRadians(lon2 - lon1);
lat1 = Math.toRadians(lat1);
lat2 = Math.toRadians(lat2);
double a = Math.sin(dLat / 2) * Math.sin(dLat / 2)
+ Math.cos(lat1) * Math.cos(lat2)
* Math.sin(dLon / 2) * Math.sin(dLon / 2);
double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
return EARTH_RADIUS * c;
}
}

View File

@@ -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;
}
}

View File

@@ -9,7 +9,6 @@ import java.security.NoSuchAlgorithmException;
* @author KilLze
*/
public class MD5Util {
/**
* 对字符串进行MD5加密
* @param input 待加密的字符串
@@ -19,7 +18,6 @@ public class MD5Util {
if (input == null || input.isEmpty()) {
return null;
}
try {
MessageDigest md = MessageDigest.getInstance("MD5");
byte[] digest = md.digest(input.getBytes());
@@ -28,7 +26,6 @@ public class MD5Util {
throw new RuntimeException("MD5算法不可用", e);
}
}
/**
* 对字符串进行MD5加密带盐值
* @param input 待加密的字符串
@@ -44,7 +41,6 @@ public class MD5Util {
}
return encrypt(input + salt);
}
/**
* 验证字符串与MD5值是否匹配
* @param input 原始字符串
@@ -57,7 +53,6 @@ public class MD5Util {
}
return encrypt(input).equalsIgnoreCase(md5Hash);
}
/**
* 验证字符串与MD5值是否匹配带盐值
* @param input 原始字符串
@@ -74,7 +69,6 @@ public class MD5Util {
}
return encryptWithSalt(input, salt).equalsIgnoreCase(md5Hash);
}
/**
* 将字节数组转换为十六进制字符串
* @param bytes 字节数组

View File

@@ -0,0 +1,65 @@
package com.bao.dating.util;
import com.bao.dating.context.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import java.util.concurrent.TimeUnit;
/**
* 用户封禁验证工具类
* 提供统一的用户封禁状态检查功能
*
* @author KilLze
*/
@Component
public class UserBanUtil {
@Autowired
private RedisTemplate<String, Object> redisTemplate;
/**
* 验证指定用户是否被封禁
*
* @param userId 用户ID
* @throws RuntimeException 如果用户被封禁则抛出异常
*/
public void validateUserNotBanned(Long userId) {
String banKey = "user:ban:" + userId;
if (Boolean.TRUE.equals(redisTemplate.hasKey(banKey))) {
String reason = (String) redisTemplate.opsForValue().get(banKey);
// 获取剩余过期时间(秒)
Long ttlSeconds = redisTemplate.getExpire(banKey, TimeUnit.SECONDS);
String remainingTime = "";
if (ttlSeconds != null && ttlSeconds > 0) {
long days = ttlSeconds / (24 * 3600);
long hours = (ttlSeconds % (24 * 3600)) / 3600;
long minutes = (ttlSeconds % 3600) / 60;
if (days > 0) {
remainingTime = ",剩余时间:" + days + "" + hours + "小时";
} else if (hours > 0) {
remainingTime = ",剩余时间:" + hours + "小时" + minutes + "分钟";
} else {
remainingTime = ",剩余时间:" + minutes + "分钟";
}
} else {
remainingTime = ",永久封禁";
}
throw new RuntimeException("账号已被封禁,原因:" + reason + remainingTime);
}
}
/**
* 验证当前登录用户是否被封禁
*
* @throws RuntimeException 如果用户被封禁则抛出异常
*/
public void validateCurrentUserNotBanned() {
Long userId = UserContext.getUserId();
validateUserNotBanned(userId);
}
}

View File

@@ -44,7 +44,6 @@ spring:
connectiontimeout: 10000 # 连接超时时间(毫秒)
timeout: 10000 # 读取超时时间(毫秒)
writetimeout: 10000 # 写入超时时间(毫秒)
# MyBatis 配置
mybatis:
mapper-locations: classpath:mapper/*.xml
@@ -70,3 +69,11 @@ aliyun:
region-id: cn-hangzhou
sign-name: 速通互联验证码
template-code: 100001
# ip2location.io 相关配置
ip2location:
api:
key: 95F4AB991174E296AFD5AD0EF927B2ED # ip2location.io API密钥
url: https://api.ip2location.io/
timeout: 5000 # 请求超时时间(毫秒)

View File

@@ -0,0 +1,9 @@
▄▄▄▄ ██ ▄▄▄▄ ▄▄▄▄ ▄▄ ▄▄
██▀▀▀▀█ ▀▀ ▀▀██ ▀▀██ ██ ▄▄ ██
██▀ ████ ▄█████▄ ██ ██ ▄████▄ ▄▄▄ ▄█▀ ▄█▀ ██ █▄ ▄▄▄█ ▀█▄ ▄▄▄▄
██ ██ ▀ ▄▄▄██ ██ ██ ██▀ ▀██ ▀ ▀▀▄▄ ▄ ██ ▄█▀ ██ ██ ██ ▄▄█▀▀▀ ██ █▀▀ ▀█▄ █▄
██▄ ██ ▄██▀▀▀██ ██ ██ ██ ██ ▀▀▀ ██ ▄█▄▄▄▄▄ ▀▀ ██ ██ ██ ▀▀█▄▄▄ ██ █▀ █ ▀▀████▀
██▄▄▄▄█ ▄▄▄██▄▄▄ ██▄▄▄███ ██▄▄▄ ██▄▄▄ ▀██▄▄██▀ ▀█▄ ▀▀▀▀▀▀▀▀ ██▄██▄██ ▀▀▀█ ▄█▀ █▀▀█
▀▀▀▀ ▀▀▀▀▀▀▀▀ ▀▀▀▀ ▀▀ ▀▀▀▀ ▀▀▀▀ ▀▀▀▀ ██ ▀▀▀ ▀▀▀ ██
▀▀ ▀▀

View File

@@ -64,4 +64,29 @@
AND read_status = 0
AND message_status = 1
</update>
<!-- 根据ID查询聊天记录 -->
<select id="selectById" resultType="com.bao.dating.pojo.entity.ChatRecords">
SELECT
chat_id,
sender_user_id,
receiver_user_id,
send_time,
message_status
FROM chat_records
WHERE chat_id = #{chatId}
</select>
<!-- 撤回消息 -->
<update id="recallMessage">
UPDATE chat_records
SET
message_status = 2,
updated_at = NOW()
WHERE
chat_id = #{chatId}
AND sender_user_id = #{senderUserId}
AND message_status = 1
</update>
</mapper>

View File

@@ -57,7 +57,35 @@
last_message_time, unread_count, top_status, mute_status
FROM chat_sessions
WHERE user_id = #{userId}
AND session_status = 1
AND session_status in (1,2)
ORDER BY top_status DESC, last_message_time DESC
</select>
<!-- 更新会话状态 -->
<update id="updateSessionStatus">
UPDATE chat_sessions
SET session_status = #{sessionStatus},
updated_at = NOW()
WHERE user_id = #{userId}
AND target_user_id = #{targetUserId}
</update>
<!-- 置顶会话 -->
<update id="updateTopStatus">
UPDATE chat_sessions
SET top_status = #{topStatus},
updated_at = NOW()
WHERE user_id = #{userId}
AND target_user_id = #{targetUserId}
</update>
<!-- 静音会话 -->
<update id="updateMuteStatus">
UPDATE chat_sessions
SET mute_status = #{muteStatus},
updated_at = NOW()
WHERE user_id = #{userId}
AND target_user_id = #{targetUserId}
</update>
</mapper>

View 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>

View File

@@ -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 &lt; DATE_SUB(NOW(), INTERVAL #{days} DAY)
</delete>
</mapper>

View File

@@ -24,4 +24,8 @@
</foreach>
</delete>
<!-- 查询所有收藏动态 -->
<select id="showAllFavorites" resultType="com.bao.dating.pojo.entity.Post">
SELECT * FROM post WHERE post_id IN (SELECT post_id FROM post_favorite WHERE user_id = #{userid})
</select>
</mapper>

View File

@@ -41,6 +41,19 @@
</foreach>
AND user_id = #{userId}
</update>
<!--删除收藏记录-->
<delete id="deletePostFavoriteByPostId">
DELETE FROM post_favorite WHERE post_id = #{postId}
</delete>
<!--删除点赞记录-->
<delete id="deletePostLikeByPostId">
DELETE FROM post_like WHERE post_id = #{postId}
</delete>
<!--动态评论删除-->
<delete id="deleteCommentsByPostId">
DELETE FROM comments WHERE post_id = #{postId}
</delete>
<!--动态查询-->
<resultMap id="PostResultMap" type="com.bao.dating.pojo.entity.Post">

View File

@@ -0,0 +1,50 @@
<?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.UserBanMapper">
<!-- 向数据库中添加用户封禁信息 -->
<insert id="insertBan" useGeneratedKeys="true" keyProperty="id">
INSERT INTO user_ban
(user_id, reason, ban_start_time, ban_end_time, status)
VALUES
(#{userId}, #{reason}, #{banStartTime}, #{banEndTime}, #{status})
</insert>
<!-- 查询指定用户是否存在未过期的封禁信息 -->
<select id="existsActiveBan" resultType="int">
SELECT COUNT(1)
FROM user_ban
WHERE user_id = #{userId}
AND status = 1
LIMIT 1
</select>
<!-- 查询指定用户是否存在未过期的封禁信息 -->
<select id="selectActiveBan" resultType="com.bao.dating.pojo.entity.UserBan">
SELECT *
FROM user_ban
WHERE user_id = #{userId}
AND status = 1
LIMIT 1
</select>
<!-- 解封指定用户 -->
<update id="unbanUser">
UPDATE user_ban
SET status = 0
WHERE user_id = #{userId}
AND status = 1
</update>
<!-- 批量更新已过期的封禁信息 -->
<update id="updateExpiredBans">
UPDATE user_ban
SET status = 0
WHERE status = 1
AND ban_end_time IS NOT NULL
AND ban_end_time &lt; NOW()
</update>
</mapper>

View File

@@ -3,6 +3,10 @@
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.bao.dating.mapper.UserMapper">
<!-- 向数据库中添加用户 -->
<insert id="saveUser">
insert into user(user_id,user_name,password_hash,salt,created_at,updated_at) values (#{userId},#{userName},#{passwordHash},#{salt},#{createdAt},#{updatedAt})
</insert>
<!--根据用户名查询用户-->
<select id="getByUsername" resultType="com.bao.dating.pojo.entity.User">
@@ -28,6 +32,8 @@
<result property="signature" column="signature"/>
<result property="createdAt" column="created_at"/>
<result property="updatedAt" column="updated_at"/>
<result property="latitude" column="user_latitude"/>
<result property="longitude" column="user_longitude"/>
</resultMap>
<select id="selectByUserId" resultMap="UserResultMap">
SELECT
@@ -41,8 +47,20 @@
hobbies,
signature,
created_at,
updated_at
FROM user WHERE user_id = #{userId}
updated_at,
user_latitude,
user_longitude
FROM dating.user WHERE user_id = #{userId}
</select>
<!-- 查询最大用户id -->
<select id="selectMaxId" resultType="java.lang.Long">
SELECT MAX(user_id) FROM user
</select>
<!-- 根据邮箱查询用户信息 -->
<select id="selectByUserEmailUser" resultType="com.bao.dating.pojo.entity.User">
select * from user where user_email = #{userEmail}
</select>
<!--根据ID更新动态-->
@@ -74,4 +92,43 @@
</set>
WHERE user_id = #{userId}
</update>
<select id="selectByPhone" resultType="com.bao.dating.pojo.entity.User">
select * from dating.user where user_phone =#{phone}
</select>
<resultMap id="UserInfoVOResultMap" type="com.bao.dating.pojo.vo.UserInfoVO">
<id property="userId" column="user_id"/>
<result property="userName" column="user_name"/>
<result property="nickname" column="nickname"/>
<result property="avatarUrl" column="avatar_url"/>
<result property="backgroundUrl" column="background_url"/>
<result property="gender" column="gender"/>
<result property="birthday" column="birthday"/>
<result property="hobbies" column="hobbies" typeHandler="com.bao.dating.handler.ListToJsonTypeHandler"/>
<result property="signature" column="signature"/>
<result property="createdAt" column="created_at"/>
<result property="updatedAt" column="updated_at"/>
<result property="latitude" column="user_latitude"/>
<result property="longitude" column="user_longitude"/>
</resultMap>
<select id="findByLatLngRange" resultMap="UserInfoVOResultMap">
SELECT
user_id,
user_name,
nickname,
avatar_url,
background_url,
gender,
birthday,
hobbies,
signature,
created_at,
updated_at,
user_latitude,
user_longitude
FROM user WHERE user_latitude BETWEEN #{minLat} AND #{maxLat} AND user_longitude BETWEEN #{minLng} AND #{maxLng}
</select>
</mapper>