好友功能完成 #2

Merged
hnu202326010419 merged 1 commits from zhaowenqi_branch into develop 2 months ago

@ -301,7 +301,9 @@ export const FriendsPageWithAPI: React.FC<FriendsPageProps> = ({
setEmailSearch('');
} catch (error) {
console.error('发送好友请求失败:', error);
alert('发送失败,请稍后重试');
// 显示后端返回的具体错误消息
const errorMessage = error instanceof Error ? error.message : '发送失败,请稍后重试';
alert(errorMessage);
}
};

@ -389,7 +389,15 @@ export const FriendsPageWithRealAPI: React.FC<FriendsPageProps> = ({
setSearchResult(null);
} catch (error) {
console.error('发送好友请求失败:', error);
alert('发送好友请求失败');
console.log('错误类型:', typeof error);
console.log('是否为 Error 实例:', error instanceof Error);
if (error instanceof Error) {
console.log('错误消息:', error.message);
}
// 显示后端返回的具体错误消息
const errorMessage = error instanceof Error ? error.message : '发送好友请求失败';
console.log('最终显示的错误消息:', errorMessage);
alert(errorMessage);
}
};

@ -1,13 +1,21 @@
package com.example.springboot_demo.controller;
import java.time.LocalDateTime;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.example.springboot_demo.common.Result;
import com.example.springboot_demo.entity.mysql.FriendRequest;
import com.example.springboot_demo.service.FriendRequestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.time.LocalDateTime;
import java.util.List;
@RestController
@RequestMapping("/friend-request")
@ -33,12 +41,17 @@ public class FriendRequestController {
@PostMapping
public Result<FriendRequest> save(@RequestBody FriendRequest friendRequest) {
friendRequest.setCreateTime(LocalDateTime.now());
if (friendRequest.getStatus() == null) {
friendRequest.setStatus(0);
try {
friendRequest.setCreateTime(LocalDateTime.now());
if (friendRequest.getStatus() == null) {
friendRequest.setStatus(0);
}
friendRequestService.save(friendRequest);
return Result.success(friendRequest);
} catch (RuntimeException e) {
System.out.println("发送好友请求失败: " + e.getMessage());
return Result.error(e.getMessage());
}
friendRequestService.save(friendRequest);
return Result.success(friendRequest);
}
@PutMapping

@ -2,7 +2,9 @@ package com.example.springboot_demo.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.springboot_demo.entity.mysql.FriendRelation;
import com.example.springboot_demo.entity.mysql.FriendRequest;
import com.example.springboot_demo.mapper.FriendRelationMapper;
import com.example.springboot_demo.mapper.FriendRequestMapper;
import com.example.springboot_demo.service.FriendRelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@ -15,6 +17,9 @@ public class FriendRelationServiceImpl implements FriendRelationService {
@Autowired
private FriendRelationMapper friendRelationMapper;
@Autowired
private FriendRequestMapper friendRequestMapper;
@Override
public List<FriendRelation> listByUserId(Long userId) {
QueryWrapper<FriendRelation> wrapper = new QueryWrapper<>();
@ -46,9 +51,29 @@ public class FriendRelationServiceImpl implements FriendRelationService {
@Override
public boolean removeByUserIdAndFriendId(Long userId, Long friendId) {
QueryWrapper<FriendRelation> wrapper = new QueryWrapper<>();
wrapper.eq("user_id", userId).eq("friend_id", friendId);
return friendRelationMapper.delete(wrapper) > 0;
// 删除双向好友关系
// 1. 删除 userId -> friendId
QueryWrapper<FriendRelation> wrapper1 = new QueryWrapper<>();
wrapper1.eq("user_id", userId).eq("friend_id", friendId);
friendRelationMapper.delete(wrapper1);
// 2. 删除 friendId -> userId
QueryWrapper<FriendRelation> wrapper2 = new QueryWrapper<>();
wrapper2.eq("user_id", friendId).eq("friend_id", userId);
friendRelationMapper.delete(wrapper2);
// 3. 删除所有相关的好友请求记录(双向)
// 删除 userId -> friendId 的请求
QueryWrapper<FriendRequest> requestWrapper1 = new QueryWrapper<>();
requestWrapper1.eq("applicant_id", userId).eq("recipient_id", friendId);
friendRequestMapper.delete(requestWrapper1);
// 删除 friendId -> userId 的请求
QueryWrapper<FriendRequest> requestWrapper2 = new QueryWrapper<>();
requestWrapper2.eq("applicant_id", friendId).eq("recipient_id", userId);
friendRequestMapper.delete(requestWrapper2);
return true;
}
}

@ -49,6 +49,20 @@ public class FriendRequestServiceImpl implements FriendRequestService {
@Override
public boolean save(FriendRequest friendRequest) {
// 1. 检查是否给自己发送好友请求
if (friendRequest.getApplicantId().equals(friendRequest.getRecipientId())) {
throw new RuntimeException("不能添加自己为好友");
}
// 2. 检查是否已经是好友关系
FriendRelation existingRelation = friendRelationService.getByUserIdAndFriendId(
friendRequest.getApplicantId(),
friendRequest.getRecipientId()
);
if (existingRelation != null) {
throw new RuntimeException("已经是好友,无需添加");
}
return friendRequestMapper.insert(friendRequest) > 0;
}

Loading…
Cancel
Save