fengjiajia 1 week ago
parent
commit
8e50d0c5e3
26 changed files with 997 additions and 7 deletions
  1. 11 2
      pom.xml
  2. 8 0
      src/main/java/com/zhentao/user/controller/UserController.java
  3. 9 4
      src/main/java/com/zhentao/user/domain/UserLogin.java
  4. 7 1
      src/main/java/com/zhentao/user/service/UserLoginService.java
  5. 22 0
      src/main/java/com/zhentao/user/service/impl/UserLoginServiceImpl.java
  6. 62 0
      src/main/java/com/zhentao/userRelationships/controller/UserRelationshipsController.java
  7. 71 0
      src/main/java/com/zhentao/userRelationships/domain/UserRelationships.java
  8. 66 0
      src/main/java/com/zhentao/userRelationships/domain/UserRequest.java
  9. 50 0
      src/main/java/com/zhentao/userRelationships/domain/UserShouye.java
  10. 13 0
      src/main/java/com/zhentao/userRelationships/dto/FriendDto.java
  11. 13 0
      src/main/java/com/zhentao/userRelationships/dto/UserRelationshipsDto.java
  12. 18 0
      src/main/java/com/zhentao/userRelationships/mapper/UserRelationshipsMapper.java
  13. 18 0
      src/main/java/com/zhentao/userRelationships/mapper/UserRequestMapper.java
  14. 18 0
      src/main/java/com/zhentao/userRelationships/mapper/UserShouyeMapper.java
  15. 38 0
      src/main/java/com/zhentao/userRelationships/service/UserRelationshipsService.java
  16. 13 0
      src/main/java/com/zhentao/userRelationships/service/UserRequestService.java
  17. 13 0
      src/main/java/com/zhentao/userRelationships/service/UserShouyeService.java
  18. 206 0
      src/main/java/com/zhentao/userRelationships/service/impl/UserRelationshipsServiceImpl.java
  19. 22 0
      src/main/java/com/zhentao/userRelationships/service/impl/UserRequestServiceImpl.java
  20. 22 0
      src/main/java/com/zhentao/userRelationships/service/impl/UserShouyeServiceImpl.java
  21. 119 0
      src/main/java/com/zhentao/userRelationships/util/AppJwtUtil.java
  22. 78 0
      src/main/java/com/zhentao/userRelationships/util/SnowflakeUtil.java
  23. 30 0
      src/main/java/com/zhentao/userRelationships/util/UserUtils.java
  24. 26 0
      src/main/resources/mapper/UserRelationshipsMapper.xml
  25. 24 0
      src/main/resources/mapper/UserRequestMapper.xml
  26. 20 0
      src/main/resources/mapper/UserShouyeMapper.xml

+ 11 - 2
pom.xml

@@ -115,8 +115,17 @@
             <artifactId>aliyun-sdk-oss</artifactId>
             <version>3.15.1</version> <!-- 使用最新稳定版本 -->
         </dependency>
-
-
+<!--二维码-->
+        <dependency>
+            <groupId>com.google.zxing</groupId>
+            <artifactId>core</artifactId>
+            <version>3.5.1</version>
+        </dependency>
+        <dependency>
+            <groupId>com.google.zxing</groupId>
+            <artifactId>javase</artifactId>
+            <version>3.5.1</version>
+        </dependency>
 
     </dependencies>
     <dependencyManagement>

+ 8 - 0
src/main/java/com/zhentao/user/controller/UserController.java

@@ -112,4 +112,12 @@ public class UserController {
         System.err.println(userById);
         return Result.OK(userById, "查询成功");
     }
+
+    //查询好有-搜索
+    @GetMapping("searchFriends")
+    @NullLogin
+    public Result searchFriends(@RequestParam("keyword") String keyword) {
+        return Result.OK(userLoginService.searchFriends(keyword), "查询成功");
+    }
+
 }

+ 9 - 4
src/main/java/com/zhentao/user/domain/UserLogin.java

@@ -6,6 +6,8 @@ import com.baomidou.mybatisplus.annotation.TableId;
 import com.baomidou.mybatisplus.annotation.TableName;
 import java.io.Serializable;
 import java.util.Date;
+
+import com.fasterxml.jackson.annotation.JsonFormat;
 import lombok.Data;
 
 /**
@@ -19,6 +21,7 @@ public class UserLogin implements Serializable {
      * 用户ID
      */
     @TableId
+    @JsonFormat(shape = JsonFormat.Shape.STRING)
     private Long id;
 
     /**
@@ -122,20 +125,22 @@ public class UserLogin implements Serializable {
     private Date updatedTime;
 
     /**
-     * 
+     *
      */
     private String openId;
 
     /**
-     * 
+     *
      */
     private String sessionKey;
 
     /**
-     * 
+     *
      */
     private String uniId;
 
+
+
     @TableField(exist = false)
     private static final long serialVersionUID = 1L;
-}
+}

+ 7 - 1
src/main/java/com/zhentao/user/service/UserLoginService.java

@@ -6,6 +6,9 @@ import com.zhentao.user.dto.*;
 import com.zhentao.vo.Result;
 import org.springframework.web.multipart.MultipartFile;
 
+import java.util.List;
+import java.util.Optional;
+
 /**
 * @author 86183
 * @description 针对表【user_login(用户)】的数据库操作Service
@@ -29,5 +32,8 @@ public interface UserLoginService extends IService<UserLogin> {
     //根据id查询用户的信息
     UserLogin getUserById(Long id);
 
-    
+    //添加好友-搜索
+    List<UserLogin> searchFriends(String keyword);
+
+    Optional<UserLogin> findById(Long userId);
 }

+ 22 - 0
src/main/java/com/zhentao/user/service/impl/UserLoginServiceImpl.java

@@ -18,8 +18,11 @@ import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.data.redis.core.StringRedisTemplate;
 import org.springframework.stereotype.Service;
 import org.springframework.util.DigestUtils;
+import org.springframework.util.StringUtils;
 import org.springframework.web.multipart.MultipartFile;
 
+import java.util.List;
+import java.util.Optional;
 import java.util.UUID;
 import java.util.concurrent.TimeUnit;
 
@@ -271,7 +274,26 @@ public class UserLoginServiceImpl extends ServiceImpl<UserLoginMapper, UserLogin
         return userLogin;
     }
 
+    @Override
+    public List<UserLogin> searchFriends(String keyword) {
+        if(!StringUtils.hasLength(keyword))
+            return null;
+
+        //根据昵称、账号进行模糊查询
+        List<UserLogin> list=this.query()
+                .like("user_username",keyword)
+                .or()
+                .like("nick_name",keyword)
+                .list();
+
+        return list;
+    }
 
+    @Override
+    public Optional<UserLogin> findById(Long userId) {
+
+        return Optional.ofNullable(userLoginMapper.selectById(userId));
+    }
 }
 
 

+ 62 - 0
src/main/java/com/zhentao/userRelationships/controller/UserRelationshipsController.java

@@ -0,0 +1,62 @@
+package com.zhentao.userRelationships.controller;
+
+import com.zhentao.config.NullLogin;
+import com.zhentao.tool.TokenUtils;
+import com.zhentao.userRelationships.domain.UserRelationships;
+import com.zhentao.userRelationships.domain.UserRequest;
+import com.zhentao.userRelationships.dto.FriendDto;
+import com.zhentao.userRelationships.service.UserRelationshipsService;
+import com.zhentao.vo.Result;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.*;
+
+import java.util.List;
+
+@RestController
+@RequestMapping("/userRelationships")
+public class UserRelationshipsController {
+
+
+    @Autowired
+    private UserRelationshipsService userRelationshipsService;
+
+
+    @GetMapping("/getByUserId")
+    @NullLogin
+    public Result getByUserId(Long userId){
+        List<UserRelationships> friendIdsByUserId = userRelationshipsService.getFriendIdsByUserId(userId);
+        return Result.OK(friendIdsByUserId,"查询成功");
+    }
+
+    //添加好友发送申请
+    @PostMapping("/sendFriendRequest")
+    @NullLogin
+    public Result sendFriendRequest(@RequestHeader("token") String token, @RequestParam Long receiverId, @RequestParam String message) {
+        String userIdFromToken = TokenUtils.getUserIdFromToken(token);
+        return userRelationshipsService.sendFriendRequest(userIdFromToken, receiverId, message);
+    }
+
+
+    // 查询好友申请表
+    @GetMapping("/getFriendRequests")
+    @NullLogin
+    public Result getFriendRequests(@RequestHeader("token") String token) {
+        String userIdFromToken = TokenUtils.getUserIdFromToken(token);
+        System.err.println(userIdFromToken);
+        List<UserRequest> friendRequestsByUserId = userRelationshipsService.getFriendRequestsByUserId(userIdFromToken);
+        return Result.OK(friendRequestsByUserId, "查询成功");
+    }
+    // 查询待处理的申请
+//    @GetMapping("/getPendingFriendRequests")
+//    @NullLogin
+//    public Result getPendingFriendRequests(Long userId) {
+//        List<UserRequest> pendingRequests = userRelationshipsService.getPendingFriendRequests(userId);
+//        return Result.OK(pendingRequests, "查询成功");
+//    }
+    // 处理申请添加好友成功
+    @PostMapping("/handleFriendRequest")
+    @NullLogin
+    public Result handleFriendRequest(@RequestParam Long requestId, @RequestParam boolean isAccepted) {
+        return userRelationshipsService.handleFriendRequest(requestId, isAccepted);
+    }
+}

+ 71 - 0
src/main/java/com/zhentao/userRelationships/domain/UserRelationships.java

@@ -0,0 +1,71 @@
+package com.zhentao.userRelationships.domain;
+
+import com.baomidou.mybatisplus.annotation.IdType;
+import com.baomidou.mybatisplus.annotation.TableField;
+import com.baomidou.mybatisplus.annotation.TableId;
+import com.baomidou.mybatisplus.annotation.TableName;
+import java.io.Serializable;
+import java.util.Date;
+import lombok.Data;
+
+/**
+ * 用户好友关系表
+ * @TableName user_relationships
+ */
+@TableName(value ="user_relationships")
+@Data
+public class UserRelationships implements Serializable {
+    /**
+     * 关系ID
+     */
+    @TableId(type = IdType.AUTO)
+    private Integer id;
+
+    /**
+     * 用户ID
+     */
+    private Long userId;
+
+    /**
+     * 好友ID
+     */
+    private Long friendId;
+
+    /**
+     * 好友备注
+     */
+    private String remark;
+
+    /**
+     * 状态(0-待确认,1-已好友,2-已拉黑)
+     */
+    private Integer status;
+
+    /**
+     * 创建时间
+     */
+    private Date createdAt;
+
+    /**
+     * 更新时间
+     */
+    private Date updatedAt;
+
+    /**
+     * 0 没加入 1加入黑名单
+     */
+    private Integer isBlacklist;
+
+    /**
+     * 0没有删除 1已删除
+     */
+    private Integer isDel;
+
+    /**
+     * 0可以观看 1不能观看朋友圈
+     */
+    private Integer isMoments;
+
+    @TableField(exist = false)
+    private static final long serialVersionUID = 1L;
+}

+ 66 - 0
src/main/java/com/zhentao/userRelationships/domain/UserRequest.java

@@ -0,0 +1,66 @@
+package com.zhentao.userRelationships.domain;
+
+import com.baomidou.mybatisplus.annotation.IdType;
+import com.baomidou.mybatisplus.annotation.TableField;
+import com.baomidou.mybatisplus.annotation.TableId;
+import com.baomidou.mybatisplus.annotation.TableName;
+import java.io.Serializable;
+import java.util.Date;
+import lombok.Data;
+
+/**
+ * 好友请求表
+ * @TableName user_request
+ */
+@TableName(value ="user_request")
+@Data
+public class UserRequest implements Serializable {
+    /**
+     * 请求id
+     */
+    @TableId(type = IdType.AUTO)
+    private Integer id;
+
+    /**
+     * 发送者id
+     */
+    private String sendId;
+
+    /**
+     * 接收者id
+     */
+    private String receiverId;
+
+    /**
+     * 请求消息
+     */
+    private String message;
+
+    /**
+     * 状态(0待处理 1已同意 2已拒绝 3一户罗)
+     */
+    private Integer status;
+
+    /**
+     * 昵称
+     */
+    private String nickName;
+
+    /**
+     * 头像
+     */
+    private String avatar;
+
+    /**
+     * 创建时间
+     */
+    private Date createTime;
+
+    /**
+     * 处理时间
+     */
+    private Date handledTime;
+
+    @TableField(exist = false)
+    private static final long serialVersionUID = 1L;
+}

+ 50 - 0
src/main/java/com/zhentao/userRelationships/domain/UserShouye.java

@@ -0,0 +1,50 @@
+package com.zhentao.userRelationships.domain;
+
+import com.baomidou.mybatisplus.annotation.IdType;
+import com.baomidou.mybatisplus.annotation.TableField;
+import com.baomidou.mybatisplus.annotation.TableId;
+import com.baomidou.mybatisplus.annotation.TableName;
+import java.io.Serializable;
+import lombok.Data;
+
+/**
+ * 用户首页表
+ * @TableName user_shouye
+ */
+@TableName(value ="user_shouye")
+@Data
+public class UserShouye implements Serializable {
+    /**
+     * 主键
+     */
+    @TableId
+    private Long id;
+
+    /**
+     * 用户1
+     */
+    private Long uid1;
+
+    /**
+     * 用户2
+     */
+    private Long uid2;
+
+    /**
+     * 群id
+     */
+    private Long gid;
+
+    /**
+     * 状态0 1
+     */
+    private Integer status;
+
+    /**
+     * 排序
+     */
+    private Integer sort;
+
+    @TableField(exist = false)
+    private static final long serialVersionUID = 1L;
+}

+ 13 - 0
src/main/java/com/zhentao/userRelationships/dto/FriendDto.java

@@ -0,0 +1,13 @@
+package com.zhentao.userRelationships.dto;
+
+import lombok.Data;
+
+@Data
+public class FriendDto {
+    private String sendToken;//发送方token
+
+    private String jiePhone;//接收者手机号(通过手机号查出ID)
+    private String message;//请求消息
+    private String remark;//好友备注
+
+}

+ 13 - 0
src/main/java/com/zhentao/userRelationships/dto/UserRelationshipsDto.java

@@ -0,0 +1,13 @@
+package com.zhentao.userRelationships.dto;
+
+import lombok.Data;
+
+@Data
+public class UserRelationshipsDto {
+
+    private Long userId;
+
+    private String remark;
+
+    private Integer status;
+}

+ 18 - 0
src/main/java/com/zhentao/userRelationships/mapper/UserRelationshipsMapper.java

@@ -0,0 +1,18 @@
+package com.zhentao.userRelationships.mapper;
+
+import com.zhentao.userRelationships.domain.UserRelationships;
+import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+
+/**
+* @author lenovo
+* @description 针对表【user_relationships(用户好友关系表)】的数据库操作Mapper
+* @createDate 2025-06-10 20:29:55
+* @Entity com.zhentao.userRelationships.domain.UserRelationships
+*/
+public interface UserRelationshipsMapper extends BaseMapper<UserRelationships> {
+
+}
+
+
+
+

+ 18 - 0
src/main/java/com/zhentao/userRelationships/mapper/UserRequestMapper.java

@@ -0,0 +1,18 @@
+package com.zhentao.userRelationships.mapper;
+
+import com.zhentao.userRelationships.domain.UserRequest;
+import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+
+/**
+* @author lenovo
+* @description 针对表【user_request(好友请求表)】的数据库操作Mapper
+* @createDate 2025-06-12 16:56:37
+* @Entity com.zhentao.userRelationships.domain.UserRequest
+*/
+public interface UserRequestMapper extends BaseMapper<UserRequest> {
+
+}
+
+
+
+

+ 18 - 0
src/main/java/com/zhentao/userRelationships/mapper/UserShouyeMapper.java

@@ -0,0 +1,18 @@
+package com.zhentao.userRelationships.mapper;
+
+import com.zhentao.userRelationships.domain.UserShouye;
+import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+
+/**
+* @author lenovo
+* @description 针对表【user_shouye(用户首页表)】的数据库操作Mapper
+* @createDate 2025-06-13 17:11:43
+* @Entity com.zhentao.userRelationships.domain.UserShouye
+*/
+public interface UserShouyeMapper extends BaseMapper<UserShouye> {
+
+}
+
+
+
+

+ 38 - 0
src/main/java/com/zhentao/userRelationships/service/UserRelationshipsService.java

@@ -0,0 +1,38 @@
+package com.zhentao.userRelationships.service;
+
+import com.zhentao.user.domain.UserLogin;
+import com.zhentao.userRelationships.domain.UserRelationships;
+import com.baomidou.mybatisplus.extension.service.IService;
+import com.zhentao.userRelationships.domain.UserRequest;
+import com.zhentao.userRelationships.dto.FriendDto;
+import com.zhentao.userRelationships.dto.UserRelationshipsDto;
+import com.zhentao.vo.Result;
+import org.apache.catalina.User;
+
+import java.util.List;
+
+/**
+* @author lenovo
+* @description 针对表【user_relationships(用户好友关系表)】的数据库操作Service
+* @createDate 2025-06-10 20:29:55
+*/
+public interface UserRelationshipsService extends IService<UserRelationships> {
+
+    //查询好友
+    List<UserRelationships> getFriendIdsByUserId(Long userId);
+
+
+
+    //添加好友
+    Result sendFriendRequest(String senderId, Long receiverId, String message);
+
+
+    //查询好友的申请表
+    List<UserRequest> getFriendRequestsByUserId(String token);
+
+//    // 查询待处理的申请
+//    List<UserRequest> getPendingFriendRequests(Long userId);
+
+    // 处理申请添加好友成功
+    Result handleFriendRequest(Long requestId, boolean isAccepted);
+}

+ 13 - 0
src/main/java/com/zhentao/userRelationships/service/UserRequestService.java

@@ -0,0 +1,13 @@
+package com.zhentao.userRelationships.service;
+
+import com.zhentao.userRelationships.domain.UserRequest;
+import com.baomidou.mybatisplus.extension.service.IService;
+
+/**
+* @author lenovo
+* @description 针对表【user_request(好友请求表)】的数据库操作Service
+* @createDate 2025-06-12 16:56:37
+*/
+public interface UserRequestService extends IService<UserRequest> {
+
+}

+ 13 - 0
src/main/java/com/zhentao/userRelationships/service/UserShouyeService.java

@@ -0,0 +1,13 @@
+package com.zhentao.userRelationships.service;
+
+import com.zhentao.userRelationships.domain.UserShouye;
+import com.baomidou.mybatisplus.extension.service.IService;
+
+/**
+* @author lenovo
+* @description 针对表【user_shouye(用户首页表)】的数据库操作Service
+* @createDate 2025-06-13 17:11:43
+*/
+public interface UserShouyeService extends IService<UserShouye> {
+
+}

+ 206 - 0
src/main/java/com/zhentao/userRelationships/service/impl/UserRelationshipsServiceImpl.java

@@ -0,0 +1,206 @@
+package com.zhentao.userRelationships.service.impl;
+
+import cn.hutool.core.lang.Snowflake;
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import com.zhentao.user.domain.UserLogin;
+import com.zhentao.user.mapper.UserLoginMapper;
+import com.zhentao.userRelationships.domain.UserRelationships;
+import com.zhentao.userRelationships.domain.UserRequest;
+import com.zhentao.userRelationships.domain.UserShouye;
+import com.zhentao.userRelationships.dto.FriendDto;
+import com.zhentao.userRelationships.dto.UserRelationshipsDto;
+import com.zhentao.userRelationships.mapper.UserRequestMapper;
+import com.zhentao.userRelationships.mapper.UserShouyeMapper;
+import com.zhentao.userRelationships.service.UserRelationshipsService;
+import com.zhentao.userRelationships.mapper.UserRelationshipsMapper;
+import com.zhentao.userRelationships.util.AppJwtUtil;
+import com.zhentao.userRelationships.util.SnowflakeUtil;
+import com.zhentao.userRelationships.util.UserUtils;
+import com.zhentao.vo.Result;
+import io.jsonwebtoken.Claims;
+import org.apache.catalina.User;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+import org.springframework.util.StringUtils;
+
+import java.util.ArrayList;
+import java.util.Date;
+import java.util.List;
+
+/**
+* @author lenovo
+* @description 针对表【user_relationships(用户好友关系表)】的数据库操作Service实现
+* @createDate 2025-06-10 20:29:55
+*/
+@Service
+public class UserRelationshipsServiceImpl extends ServiceImpl<UserRelationshipsMapper, UserRelationships>
+    implements UserRelationshipsService{
+    @Autowired
+    private UserRelationshipsMapper userRelationshipsMapper;
+    @Autowired
+    private UserLoginMapper userLoginMapper;
+    @Autowired
+    private UserRequestMapper userRequestMapper;
+    @Autowired
+    private UserShouyeMapper userShouyeMapper;
+    //查询好友
+    @Override
+    public List<UserRelationships> getFriendIdsByUserId(Long userId) {
+        QueryWrapper<UserRelationships> queryWrapper = new QueryWrapper<>();
+        queryWrapper.eq("status", 1);
+        queryWrapper.eq("user_id", userId);
+        List<UserRelationships> list = this.list(queryWrapper);
+
+        return list;
+
+    }
+
+    //添加好友
+
+    @Override
+    public Result sendFriendRequest(String senderId, Long receiverId, String message) {
+        // 检查是否已经是好友关系
+        QueryWrapper<UserRelationships> relationshipQuery = new QueryWrapper<>();
+        relationshipQuery.eq("user_id", senderId);
+        relationshipQuery.eq("friend_id", receiverId);
+        relationshipQuery.eq("status", 1);
+        UserRelationships existingRelationship = userRelationshipsMapper.selectOne(relationshipQuery);
+
+        if (existingRelationship != null) {
+            return Result.error(null,"已经是好友关系,无需重复申请");
+        }
+
+        // 检查是否已经有未处理的申请
+        QueryWrapper<UserRequest> requestQuery = new QueryWrapper<>();
+        requestQuery.eq("send_id", senderId);
+        requestQuery.eq("receiver_id", receiverId);
+        requestQuery.eq("status", 0);
+        UserRequest existingRequest = userRequestMapper.selectOne(requestQuery);
+
+        if (existingRequest != null) {
+            return Result.error(null,"已经有未处理的好友申请,请勿重复发送");
+        }
+
+        UserLogin userLogin = userLoginMapper.selectById(senderId);
+        // 创建新的好友申请记录
+        UserRequest userRequest = new UserRequest();
+        userRequest.setSendId(senderId);
+        userRequest.setReceiverId(receiverId.toString());
+        userRequest.setMessage(message);
+        userRequest.setNickName(userLogin.getNickName());
+        userRequest.setAvatar(userLogin.getAvatar()); // 设置头像
+        userRequest.setStatus(0);
+        userRequest.setCreateTime(new Date());
+
+        // 插入记录
+        int result = userRequestMapper.insert(userRequest);
+
+        if (result > 0) {
+            return Result.OK(userRequest, "好友申请发送成功");
+        } else {
+            return Result.error(null,"好友申请发送失败");
+        }
+    }
+
+    // 查询好友申请表
+    @Override
+    public List<UserRequest> getFriendRequestsByUserId(String token) {
+        QueryWrapper<UserRequest> queryWrapper = new QueryWrapper<>();
+        queryWrapper.eq("receiver_id", token);
+//        queryWrapper.eq("status", 0); // 只查询待处理的申请
+        queryWrapper.orderByAsc("create_time"); // 按照创建时间倒序排列
+        System.err.println(queryWrapper);
+        return userRequestMapper.selectList(queryWrapper);
+    }
+
+    // 查询待处理的申请
+//    @Override
+//    public List<UserRequest> getPendingFriendRequests(Long userId) {
+//        QueryWrapper<UserRequest> queryWrapper = new QueryWrapper<>();
+//        queryWrapper.eq("receiver_id", userId.toString());
+//        queryWrapper.eq("status", 0);
+//        return userRequestMapper.selectList(queryWrapper);
+//    }
+
+    // 处理申请添加好友成功
+    @Override
+    public Result handleFriendRequest(Long requestId, boolean isAccepted) {
+        // 查询好友申请记录
+        UserRequest userRequest = userRequestMapper.selectById(requestId);
+
+        if (userRequest == null) {
+            return Result.error(null,"好友申请记录不存在");
+        }
+
+        if (userRequest.getStatus() != 0) {
+            return Result.error(null,"该申请已处理,请勿重复操作");
+        }
+
+        if (isAccepted) {
+            // 更新申请状态为已同意
+            userRequest.setStatus(1);
+            userRequest.setHandledTime(new Date());
+            userRequestMapper.updateById(userRequest);
+
+            // 创建好友关系记录
+            Long senderId = Long.parseLong(userRequest.getSendId());
+            Long receiverId = Long.parseLong(userRequest.getReceiverId());
+
+            // 插入发送者的好友关系记录
+            UserRelationships senderRelationship = new UserRelationships();
+            senderRelationship.setUserId(senderId);
+            senderRelationship.setFriendId(receiverId);
+            senderRelationship.setStatus(1);
+            senderRelationship.setCreatedAt(new Date());
+            senderRelationship.setUpdatedAt(new Date());
+            senderRelationship.setIsBlacklist(0);
+            senderRelationship.setIsDel(0);
+            senderRelationship.setIsMoments(0);
+            userRelationshipsMapper.insert(senderRelationship);
+
+            // 插入接收者的好友关系记录
+            UserRelationships receiverRelationship = new UserRelationships();
+            receiverRelationship.setUserId(receiverId);
+            receiverRelationship.setFriendId(senderId);
+            receiverRelationship.setStatus(1);
+            receiverRelationship.setCreatedAt(new Date());
+            receiverRelationship.setUpdatedAt(new Date());
+            receiverRelationship.setIsBlacklist(0);
+            receiverRelationship.setIsDel(0);
+            receiverRelationship.setIsMoments(0);
+            userRelationshipsMapper.insert(receiverRelationship);
+
+            //插入首页的发送者的好友关系记录表
+            UserShouye senduserShouye=new UserShouye();
+            senduserShouye.setId(Long.valueOf(String.valueOf(SnowflakeUtil.nextId())));
+            senduserShouye.setUid1(senderId);
+            senduserShouye.setUid2(receiverId);
+            senduserShouye.setStatus(0);
+            userShouyeMapper.insert(senduserShouye);
+
+            //插入接收者的好友关系记录
+            UserShouye receiveuserShouye=new UserShouye();
+            receiveuserShouye.setId(Long.valueOf(String.valueOf(SnowflakeUtil.nextId())));
+            receiveuserShouye.setUid1(receiverId);
+            receiveuserShouye.setUid2(senderId);
+            receiveuserShouye.setStatus(0);
+            userShouyeMapper.insert(receiveuserShouye);
+
+            return Result.OK(null, "好友申请已同意,添加好友成功");
+        } else {
+            // 更新申请状态为已拒绝
+            userRequest.setStatus(2);
+            userRequest.setHandledTime(new Date());
+            userRequestMapper.updateById(userRequest);
+            return Result.OK(null, "好友申请已拒绝");
+        }
+    }
+
+
+
+}
+
+
+
+

+ 22 - 0
src/main/java/com/zhentao/userRelationships/service/impl/UserRequestServiceImpl.java

@@ -0,0 +1,22 @@
+package com.zhentao.userRelationships.service.impl;
+
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import com.zhentao.userRelationships.domain.UserRequest;
+import com.zhentao.userRelationships.service.UserRequestService;
+import com.zhentao.userRelationships.mapper.UserRequestMapper;
+import org.springframework.stereotype.Service;
+
+/**
+* @author lenovo
+* @description 针对表【user_request(好友请求表)】的数据库操作Service实现
+* @createDate 2025-06-12 16:56:37
+*/
+@Service
+public class UserRequestServiceImpl extends ServiceImpl<UserRequestMapper, UserRequest>
+    implements UserRequestService{
+
+}
+
+
+
+

+ 22 - 0
src/main/java/com/zhentao/userRelationships/service/impl/UserShouyeServiceImpl.java

@@ -0,0 +1,22 @@
+package com.zhentao.userRelationships.service.impl;
+
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import com.zhentao.userRelationships.domain.UserShouye;
+import com.zhentao.userRelationships.service.UserShouyeService;
+import com.zhentao.userRelationships.mapper.UserShouyeMapper;
+import org.springframework.stereotype.Service;
+
+/**
+* @author lenovo
+* @description 针对表【user_shouye(用户首页表)】的数据库操作Service实现
+* @createDate 2025-06-13 17:11:43
+*/
+@Service
+public class UserShouyeServiceImpl extends ServiceImpl<UserShouyeMapper, UserShouye>
+    implements UserShouyeService{
+
+}
+
+
+
+

+ 119 - 0
src/main/java/com/zhentao/userRelationships/util/AppJwtUtil.java

@@ -0,0 +1,119 @@
+package com.zhentao.userRelationships.util;
+
+import io.jsonwebtoken.*;
+
+import javax.crypto.SecretKey;
+import javax.crypto.spec.SecretKeySpec;
+import java.util.*;
+
+public class AppJwtUtil {
+
+    // TOKEN的有效期一天(S)
+    private static final int TOKEN_TIME_OUT = 3_600;
+    // 加密KEY
+    private static final String TOKEN_ENCRY_KEY = "MDk4ZjZiY2Q0NjIxZDM3M2NhZGU0ZTgzMjYyN2I0ZjY";
+    // 最小刷新间隔(S)
+    private static final int REFRESH_TIME = 300;
+
+    // 生产ID
+    public static String getToken(Long id){
+        Map<String, Object> claimMaps = new HashMap<>();
+        claimMaps.put("id",id);
+        long currentTime = System.currentTimeMillis();
+        return Jwts.builder()
+                .setId(UUID.randomUUID().toString())
+                .setIssuedAt(new Date(currentTime))  //签发时间
+                .setSubject("system")  //说明
+                .setIssuer("") //签发者信息
+                .setAudience("app")  //接收用户
+                .compressWith(CompressionCodecs.GZIP)  //数据压缩方式
+                .signWith(SignatureAlgorithm.HS512, generalKey()) //加密方式
+                .setExpiration(new Date(currentTime + TOKEN_TIME_OUT * 1000))  //过期时间戳
+                .addClaims(claimMaps) //cla信息
+                .compact();
+    }
+
+    /**
+     * 获取token中的claims信息
+     *
+     * @param token
+     * @return
+     */
+    private static Jws<Claims> getJws(String token) {
+            return Jwts.parser()
+                    .setSigningKey(generalKey())
+                    .parseClaimsJws(token);
+    }
+
+    /**
+     * 获取payload body信息
+     *
+     * @param token
+     * @return
+     */
+    public static Claims getClaimsBody(String token) {
+        try {
+            return getJws(token).getBody();
+        }catch (ExpiredJwtException e){
+            return null;
+        }
+    }
+
+    /**
+     * 获取hearder body信息
+     *
+     * @param token
+     * @return
+     */
+    public static JwsHeader getHeaderBody(String token) {
+        return getJws(token).getHeader();
+    }
+
+    /**
+     * 是否过期
+     *
+     * @param claims
+     * @return -1:有效,0:有效,1:过期,2:过期
+     */
+    public static int verifyToken(Claims claims) {
+        if(claims==null){
+            return 1;
+        }
+        try {
+            claims.getExpiration()
+                    .before(new Date());
+            // 需要自动刷新TOKEN
+            if((claims.getExpiration().getTime()-System.currentTimeMillis())>REFRESH_TIME*1000){
+                return -1;
+            }else {
+                return 0;
+            }
+        } catch (ExpiredJwtException ex) {
+            return 1;
+        }catch (Exception e){
+            return 2;
+        }
+    }
+
+    /**
+     * 由字符串生成加密key
+     *
+     * @return
+     */
+    public static SecretKey generalKey() {
+        byte[] encodedKey = Base64.getEncoder().encode(TOKEN_ENCRY_KEY.getBytes());
+        SecretKey key = new SecretKeySpec(encodedKey, 0, encodedKey.length, "AES");
+        return key;
+    }
+
+    public static void main(String[] args) {
+       /* Map map = new HashMap();
+        map.put("id","11");*/
+        System.out.println(AppJwtUtil.getToken(1102L));
+        Jws<Claims> jws = AppJwtUtil.getJws("eyJhbGciOiJIUzUxMiIsInppcCI6IkdaSVAifQ.H4sIAAAAAAAAADWLQQqEMAwA_5KzhURNt_qb1KZYQSi0wi6Lf9942NsMw3zh6AVW2DYmDGl2WabkZgreCaM6VXzhFBfJMcMARTqsxIG9Z888QLui3e3Tup5Pb81013KKmVzJTGo11nf9n8v4nMUaEY73DzTabjmDAAAA.4SuqQ42IGqCgBai6qd4RaVpVxTlZIWC826QA9kLvt9d-yVUw82gU47HDaSfOzgAcloZedYNNpUcd18Ne8vvjQA");
+        Claims claims = jws.getBody();
+        System.out.println(claims.get("id"));
+
+    }
+
+}

+ 78 - 0
src/main/java/com/zhentao/userRelationships/util/SnowflakeUtil.java

@@ -0,0 +1,78 @@
+package com.zhentao.userRelationships.util;
+
+/**
+ * @Date 2025/4/21 19:25
+ * @Author gln
+ **/
+
+public class SnowflakeUtil {
+    // 起始时间戳,可自定义,这里以 2020-01-01 00:00:00 为例
+    private static final long START_TIMESTAMP = 1577836800000L;
+
+    // 数据中心 ID 所占位数
+    private static final long DATA_CENTER_ID_BITS = 5L;
+    // 机器 ID 所占位数
+    private static final long WORKER_ID_BITS = 5L;
+    // 序列号所占位数
+    private static final long SEQUENCE_BITS = 12L;
+
+    // 数据中心 ID 最大值
+    private static final long MAX_DATA_CENTER_ID = -1L ^ (-1L << DATA_CENTER_ID_BITS);
+    // 机器 ID 最大值
+    private static final long MAX_WORKER_ID = -1L ^ (-1L << WORKER_ID_BITS);
+    // 序列号最大值
+    private static final long SEQUENCE_MASK = -1L ^ (-1L << SEQUENCE_BITS);
+
+    // 机器 ID 向左移位数
+    private static final long WORKER_ID_SHIFT = SEQUENCE_BITS;
+    // 数据中心 ID 向左移位数
+    private static final long DATA_CENTER_ID_SHIFT = SEQUENCE_BITS + WORKER_ID_BITS;
+    // 时间戳向左移位数
+    private static final long TIMESTAMP_LEFT_SHIFT = SEQUENCE_BITS + WORKER_ID_BITS + DATA_CENTER_ID_BITS;
+
+    // 固定数据中心 ID 和机器 ID
+    private static final long DATA_CENTER_ID = 1;
+    private static final long WORKER_ID = 1;
+
+    private static long sequence = 0L;
+    private static long lastTimestamp = -1L;
+
+    public static synchronized long nextId() {
+        long timestamp = System.currentTimeMillis();
+
+        if (timestamp < lastTimestamp) {
+            throw new RuntimeException("Clock moved backwards. Refusing to generate id for " + (lastTimestamp - timestamp) + " milliseconds");
+        }
+
+        if (timestamp == lastTimestamp) {
+            sequence = (sequence + 1) & SEQUENCE_MASK;
+            if (sequence == 0) {
+                timestamp = waitForNextMillis(lastTimestamp);
+            }
+        } else {
+            sequence = 0L;
+        }
+
+        lastTimestamp = timestamp;
+
+        return ((timestamp - START_TIMESTAMP) << TIMESTAMP_LEFT_SHIFT) |
+                (DATA_CENTER_ID << DATA_CENTER_ID_SHIFT) |
+                (WORKER_ID << WORKER_ID_SHIFT) |
+                sequence;
+    }
+
+    private static long waitForNextMillis(long lastTimestamp) {
+        long timestamp = System.currentTimeMillis();
+        while (timestamp <= lastTimestamp) {
+            timestamp = System.currentTimeMillis();
+        }
+        return timestamp;
+    }
+
+    public static void main(String[] args) {
+        for (int i = 0; i < 10; i++) {
+            System.out.println(nextId());
+        }
+    }
+}
+

+ 30 - 0
src/main/java/com/zhentao/userRelationships/util/UserUtils.java

@@ -0,0 +1,30 @@
+package com.zhentao.userRelationships.util;
+
+/**
+ * @author feifan
+ * @QQ 2535289363
+ * @create 2023-10-26 18:49
+ * <p>
+ * 存储当前登录用户id的ThreadLocal
+ */
+public class UserUtils {
+
+    //新建一个ThreadLocal
+    private static ThreadLocal<Integer> uidThreadLocal = new ThreadLocal<>();
+
+    //存储uid
+    public static void setUid(Integer uid) {
+        uidThreadLocal.set(uid);
+    }
+
+    //取出uid
+    public static Integer getUid() {
+        return uidThreadLocal.get();
+    }
+
+    //清除(只清除当前线程的uid)
+    public static void clear() {
+        uidThreadLocal.remove();
+    }
+
+}

+ 26 - 0
src/main/resources/mapper/UserRelationshipsMapper.xml

@@ -0,0 +1,26 @@
+<?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.zhentao.userRelationships.mapper.UserRelationshipsMapper">
+
+    <resultMap id="BaseResultMap" type="com.zhentao.userRelationships.domain.UserRelationships">
+            <id property="id" column="id" jdbcType="BIGINT"/>
+            <result property="userId" column="user_id" jdbcType="BIGINT"/>
+            <result property="friendId" column="friend_id" jdbcType="BIGINT"/>
+            <result property="remark" column="remark" jdbcType="VARCHAR"/>
+            <result property="status" column="status" jdbcType="TINYINT"/>
+            <result property="createdAt" column="created_at" jdbcType="TIMESTAMP"/>
+            <result property="updatedAt" column="updated_at" jdbcType="TIMESTAMP"/>
+            <result property="isBlacklist" column="is_blacklist" jdbcType="INTEGER"/>
+            <result property="isDel" column="is_del" jdbcType="INTEGER"/>
+            <result property="isMoments" column="is_moments" jdbcType="INTEGER"/>
+    </resultMap>
+
+    <sql id="Base_Column_List">
+        id,user_id,friend_id,
+        remark,status,created_at,
+        updated_at,is_blacklist,is_del,
+        is_moments
+    </sql>
+</mapper>

+ 24 - 0
src/main/resources/mapper/UserRequestMapper.xml

@@ -0,0 +1,24 @@
+<?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.zhentao.userRelationships.mapper.UserRequestMapper">
+
+    <resultMap id="BaseResultMap" type="com.zhentao.userRelationships.domain.UserRequest">
+            <id property="id" column="id" jdbcType="INTEGER"/>
+            <result property="sendId" column="send_id" jdbcType="VARCHAR"/>
+            <result property="receiverId" column="receiver_id" jdbcType="VARCHAR"/>
+            <result property="message" column="message" jdbcType="VARCHAR"/>
+            <result property="status" column="status" jdbcType="INTEGER"/>
+            <result property="nickName" column="nick_name" jdbcType="VARCHAR"/>
+            <result property="avatar" column="avatar" jdbcType="VARCHAR"/>
+            <result property="createTime" column="create_time" jdbcType="TIMESTAMP"/>
+            <result property="handledTime" column="handled_time" jdbcType="TIMESTAMP"/>
+    </resultMap>
+
+    <sql id="Base_Column_List">
+        id,send_id,receiver_id,
+        message,status,nick_name,
+        avatar,create_time,handled_time
+    </sql>
+</mapper>

+ 20 - 0
src/main/resources/mapper/UserShouyeMapper.xml

@@ -0,0 +1,20 @@
+<?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.zhentao.userRelationships.mapper.UserShouyeMapper">
+
+    <resultMap id="BaseResultMap" type="com.zhentao.userRelationships.domain.UserShouye">
+            <id property="id" column="id" jdbcType="BIGINT"/>
+            <result property="uid1" column="uid1" jdbcType="BIGINT"/>
+            <result property="uid2" column="uid2" jdbcType="BIGINT"/>
+            <result property="gid" column="gid" jdbcType="BIGINT"/>
+            <result property="status" column="status" jdbcType="INTEGER"/>
+            <result property="sort" column="sort" jdbcType="INTEGER"/>
+    </resultMap>
+
+    <sql id="Base_Column_List">
+        id,uid1,uid2,
+        gid,status,sort
+    </sql>
+</mapper>