搜索好友接口优化
This commit is contained in:
@@ -43,7 +43,7 @@ func ContactListHandler(c *gin.Context) {
|
||||
|
||||
/**
|
||||
* SearchUsersHandler
|
||||
* 功能:搜索用户
|
||||
* 功能:搜索用户(返回带有 is_friend 标识的结果)
|
||||
* 路径:GET /api/contacts/search
|
||||
*/
|
||||
func SearchUsersHandler(c *gin.Context) {
|
||||
@@ -58,18 +58,25 @@ func SearchUsersHandler(c *gin.Context) {
|
||||
limit = 20
|
||||
}
|
||||
|
||||
users, err := service.ContactSvc.SearchUsers(keyword, limit)
|
||||
// 获取当前用户ID(用于判断好友关系)
|
||||
var currentUserID string
|
||||
if userID, exists := c.Get("user_id"); exists {
|
||||
currentUserID = userID.(string)
|
||||
}
|
||||
|
||||
// 调用服务层搜索用户
|
||||
result, err := service.ContactSvc.SearchUsersWithFriendStatus(currentUserID, keyword, limit)
|
||||
if err != nil {
|
||||
utils.InternalError(c, "搜索失败")
|
||||
return
|
||||
}
|
||||
|
||||
// 确保返回空数组而不是null
|
||||
if users == nil {
|
||||
users = []model.User{}
|
||||
if result == nil {
|
||||
result = []map[string]interface{}{}
|
||||
}
|
||||
|
||||
utils.SuccessWithData(c, users, "搜索成功")
|
||||
utils.SuccessWithData(c, result, "搜索成功")
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -79,7 +86,7 @@ func SearchUsersHandler(c *gin.Context) {
|
||||
*/
|
||||
func AddFriendHandler(c *gin.Context) {
|
||||
userID, _ := c.Get("user_id")
|
||||
|
||||
|
||||
var req struct {
|
||||
ToUserID string `json:"to_user_id" binding:"required"`
|
||||
Message string `json:"message"`
|
||||
@@ -272,31 +279,20 @@ func DeleteGroupHandler(c *gin.Context) {
|
||||
|
||||
/**
|
||||
* GetContactDetailHandler
|
||||
* 功能:获取好友详情
|
||||
* 功能:获取用户详情(包含好友关系)
|
||||
* 路径:GET /api/contacts/:id
|
||||
*/
|
||||
func GetContactDetailHandler(c *gin.Context) {
|
||||
userID, _ := c.Get("user_id")
|
||||
contactID := c.Param("id")
|
||||
|
||||
contact, err := service.ContactSvc.GetContactDetail(userID.(string), contactID)
|
||||
if err != nil {
|
||||
utils.NotFound(c, "好友不存在")
|
||||
return
|
||||
}
|
||||
|
||||
// 获取联系人用户信息
|
||||
user, err := service.UserSvc.GetUserByID(contactID)
|
||||
// 调用服务层获取用户详情(包含好友关系)
|
||||
result, err := service.ContactSvc.GetUserDetailWithFriendStatus(userID.(string), contactID)
|
||||
if err != nil {
|
||||
utils.NotFound(c, "用户不存在")
|
||||
return
|
||||
}
|
||||
|
||||
result := map[string]interface{}{
|
||||
"contact": contact,
|
||||
"user": user,
|
||||
}
|
||||
|
||||
utils.SuccessWithData(c, result, "获取成功")
|
||||
}
|
||||
|
||||
@@ -359,4 +355,3 @@ func DeleteContactHandler(c *gin.Context) {
|
||||
|
||||
utils.Success(c, "已删除好友")
|
||||
}
|
||||
|
||||
|
||||
@@ -451,3 +451,81 @@ func (s *ContactService) GetFriendUserIDs(userID string) ([]string, error) {
|
||||
|
||||
return friendIDs, nil
|
||||
}
|
||||
|
||||
/**
|
||||
* SearchUsersWithFriendStatus
|
||||
* 功能:搜索用户并返回带有 is_friend 标识的结果
|
||||
* 参数:
|
||||
* - currentUserID: 当前登录用户ID(用于判断好友关系,可为空)
|
||||
* - keyword: 搜索关键词
|
||||
* - limit: 返回数量限制
|
||||
*/
|
||||
func (s *ContactService) SearchUsersWithFriendStatus(currentUserID, keyword string, limit int) ([]map[string]interface{}, error) {
|
||||
// 搜索用户
|
||||
users, err := UserSvc.SearchUsers(keyword, limit)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 获取当前用户的好友ID集合
|
||||
friendIDSet := make(map[string]bool)
|
||||
if currentUserID != "" {
|
||||
friendIDs, err := s.GetFriendUserIDs(currentUserID)
|
||||
if err == nil {
|
||||
for _, id := range friendIDs {
|
||||
friendIDSet[id] = true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 构建带有 is_friend 标识的结果
|
||||
result := make([]map[string]interface{}, 0, len(users))
|
||||
for _, user := range users {
|
||||
item := map[string]interface{}{
|
||||
"id": user.ID,
|
||||
"email": user.Email,
|
||||
"phone": user.Phone,
|
||||
"name": user.Name,
|
||||
"avatar": user.Avatar,
|
||||
"desc": user.Desc,
|
||||
"region": user.Region,
|
||||
"created_at": user.CreatedAt,
|
||||
"updated_at": user.UpdatedAt,
|
||||
"is_friend": friendIDSet[user.ID],
|
||||
}
|
||||
result = append(result, item)
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
/**
|
||||
* GetUserDetailWithFriendStatus
|
||||
* 功能:获取用户详情(包含好友关系)
|
||||
* 参数:
|
||||
* - currentUserID: 当前登录用户ID
|
||||
* - targetUserID: 目标用户ID
|
||||
* 返回:
|
||||
* - map 包含 is_friend、contact、user 字段
|
||||
*/
|
||||
func (s *ContactService) GetUserDetailWithFriendStatus(currentUserID, targetUserID string) (map[string]interface{}, error) {
|
||||
// 获取目标用户信息
|
||||
user, err := UserSvc.GetUserByID(targetUserID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 查询联系人关系,判断是否是好友
|
||||
var isFriend bool
|
||||
var contact *model.UserContact
|
||||
contact, err = s.GetContactDetail(currentUserID, targetUserID)
|
||||
if err == nil && contact != nil {
|
||||
isFriend = true
|
||||
}
|
||||
|
||||
return map[string]interface{}{
|
||||
"is_friend": isFriend,
|
||||
"contact": contact,
|
||||
"user": user,
|
||||
}, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user