Skip to content

Commit

Permalink
Merge pull request #3
Browse files Browse the repository at this point in the history
feat:添加获取联系人列表接口
  • Loading branch information
danmuking authored Apr 7, 2024
2 parents cde18ac + bda8b5c commit bfd854c
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 1 deletion.
7 changes: 7 additions & 0 deletions domain/vo/resp/user_contact_resp.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package resp

type UserContactResp struct {
ID int64 `json:"ID"` // 用户ID
Name string `json:"name"` // 用户昵称
Avatar string `json:"avatar"` // 用户头像
}
2 changes: 1 addition & 1 deletion routes/init_router.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ func initGin() {
//同意好友申请
apiContact.PUT("/agree", service.Agree)
//获取好友列表
apiContact.GET("/getContactList", service.Login)
apiContact.GET("/getContactList", service.GetContactList)
//判断是否是好友
apiContact.GET("/isFriend/:friendUid", service.IsFriend)
//好友申请未读数量
Expand Down
45 changes: 45 additions & 0 deletions service/contact_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -329,3 +329,48 @@ func UnreadApplyNum(c *gin.Context) {
}
resp.SuccessResponse(c, num)
}

// GetContactList 获取好友列表
//
// @Summary 获取好友列表
// @Produce json
// @Success 200 {object} resp.ResponseData "成功"
// @Failure 500 {object} resp.ResponseData "内部错误"
// @Router /api/contact/getContactList [get]
func GetContactList(c *gin.Context) {
ctx := context.Background()
uid := c.GetInt64("uid")
// 获取好友列表
userFriend := query.UserFriend
// 获取 UserFriend 表中 uid = uid 的好友的uid组成的集合
// select friend_uid from user_friend where uid = ?
friendIDs, err := userFriend.WithContext(ctx).Select(userFriend.FriendUID).Where(userFriend.UID.Eq(uid)).Find()
// 将friendIDs转换为切片
// TODO 实现游标翻页
friendIDsSlice := make([]int64, 0)
for _, id := range friendIDs {
friendIDsSlice = append(friendIDsSlice, id.FriendUID)
}
if err != nil {
// todo 添加日志系统
log.Printf("SQL查询错误, 错误信息为 : %v", err)
resp.ErrorResponse(c, "出现错误,未能获取联系人列表")
return
}

// 获取好友信息
users := query.User
// select id , name , avatar from user where id in (...) and status = 0
friendList, err := users.WithContext(ctx).Select(users.ID, users.Name, users.Avatar).Where(users.ID.In(friendIDsSlice...), users.Status.Eq(0)).Find()
if err != nil {
// todo 添加日志系统
log.Printf("SQL查询错误, 错误信息为 : %v", err)
resp.ErrorResponse(c, "出现错误,未能获取联系人信息")
return
}

// 数据转换
friendListVO := make([]resp2.UserContactResp, 0)
_ = copier.Copy(&friendListVO, &friendList)
resp.SuccessResponse(c, friendListVO)
}

0 comments on commit bfd854c

Please sign in to comment.