From aaf3293147052dde5da170f0525c2b40a32674da Mon Sep 17 00:00:00 2001 From: shenjiachi Date: Thu, 19 Dec 2024 18:23:31 +0800 Subject: [PATCH] add nickname and avatar when query friend circle --- app/hdl/friend_circle/hdl_friend_circle.go | 36 ++++++++++++++++++++++ app/md/friend_circles/md_friend_circle.go | 23 +++++++++----- app/svc/friend_circle/svc_firend_circle.go | 23 ++++++++++++-- 3 files changed, 72 insertions(+), 10 deletions(-) diff --git a/app/hdl/friend_circle/hdl_friend_circle.go b/app/hdl/friend_circle/hdl_friend_circle.go index 20d42ab..492fad6 100644 --- a/app/hdl/friend_circle/hdl_friend_circle.go +++ b/app/hdl/friend_circle/hdl_friend_circle.go @@ -47,6 +47,24 @@ func MySelfList(c *gin.Context) { e.OutErr(c, e.ERR, err.Error()) return } + imUserIds := make([]int64, len(resp.List)) + for i, friendCircle := range resp.List { + imUserIds[i] = friendCircle.ImUid + } + + nicknameAndAvatarMap, err := svc2.GetImUserNicknameAndAvatar(imUserIds) + if err != nil { + e.OutErr(c, e.ERR, err.Error()) + return + } + for _, friendCircle := range resp.List { + val, ok := nicknameAndAvatarMap[friendCircle.ImUid] + if ok { + friendCircle.ImNickname = val.NickName + friendCircle.ImAvatar = val.Avatar + } + } + resp.Page = args.Page resp.PageSize = args.PageSize e.OutSuc(c, resp, nil) @@ -189,6 +207,24 @@ func RecommendList(c *gin.Context) { e.OutErr(c, e.ERR, err.Error()) return } + + imUserIds := make([]int64, len(resp.List)) + for i, friendCircle := range resp.List { + imUserIds[i] = friendCircle.ImUid + } + + nicknameAndAvatarMap, err := svc2.GetImUserNicknameAndAvatar(imUserIds) + if err != nil { + e.OutErr(c, e.ERR, err.Error()) + return + } + for _, friendCircle := range resp.List { + val, ok := nicknameAndAvatarMap[friendCircle.ImUid] + if ok { + friendCircle.ImNickname = val.NickName + friendCircle.ImAvatar = val.Avatar + } + } e.OutSuc(c, resp, nil) } diff --git a/app/md/friend_circles/md_friend_circle.go b/app/md/friend_circles/md_friend_circle.go index 7af0320..67c6cb0 100644 --- a/app/md/friend_circles/md_friend_circle.go +++ b/app/md/friend_circles/md_friend_circle.go @@ -6,10 +6,10 @@ type RecommendListReq struct { } type RecommendListResp struct { - Page int `json:"page"` // 页码 - PageSize int `json:"page_size"` // 每页数量 - Total int64 `json:"total"` // 总量 - List []EggFriendCircleEsStruct `json:"list"` + Page int `json:"page"` // 页码 + PageSize int `json:"page_size"` // 每页数量 + Total int64 `json:"total"` // 总量 + List []*EggFriendCircleEsStruct `json:"list"` } type CommentListReq struct { @@ -32,16 +32,18 @@ type MySelfListReq struct { PageSize int `json:"page_size"` // 每页数量 } type MySelfListResp struct { - Page int `json:"page"` // 页码 - PageSize int `json:"page_size"` // 每页数量 - Total int64 `json:"total"` // 总量 - List []EggFriendCircleEsStruct `json:"list"` + Page int `json:"page"` // 页码 + PageSize int `json:"page_size"` // 每页数量 + Total int64 `json:"total"` // 总量 + List []*EggFriendCircleEsStruct `json:"list"` } type EggFriendCircleEsStruct struct { CircleIndexId string `json:"circle_index_id"` //朋友圈文档记录 Uid int64 `json:"uid" label:"uid"` ImUid int64 `json:"im_uid" label:"im_uid"` + ImNickname string `json:"im_nickname" label:"im_nickname"` + ImAvatar string `json:"im_avatar" label:"im_avatar"` Kind int32 `json:"kind" label:"类型(1:普通 2:官方)"` //类型(1:普通 2:官方) Content string `json:"content" label:"文本内容"` //文本内容 Image string `json:"image" label:"图片"` //图片 @@ -107,3 +109,8 @@ type EggFriendCircleLikeReq struct { type EggFriendCircleCancelLikeReq struct { CircleIndexId string `json:"circle_index_id"` // 文章索引 } + +type ImUserNicknameAndAvatar struct { + NickName string `json:"nickname"` + Avatar string `json:"avatar"` +} diff --git a/app/svc/friend_circle/svc_firend_circle.go b/app/svc/friend_circle/svc_firend_circle.go index 25acd7e..2107e69 100644 --- a/app/svc/friend_circle/svc_firend_circle.go +++ b/app/svc/friend_circle/svc_firend_circle.go @@ -130,7 +130,7 @@ func MySelfList(c *gin.Context, req friend_circles.MySelfListReq) (resp friend_c if likeDoc != nil && likeDoc.Found { // 表示已点赞 doc.IsLike = true } - resp.List = append(resp.List, doc) + resp.List = append(resp.List, &doc) } resp.Total = searchResult.TotalHits() @@ -170,7 +170,7 @@ func GetRecommendList(req friend_circles.RecommendListReq) (resp friend_circles. return } doc.CircleIndexId = hit.Id - resp.List = append(resp.List, doc) + resp.List = append(resp.List, &doc) } resp.Total = searchResult.TotalHits() @@ -333,3 +333,22 @@ func DelFriendCircleComment(circleId string) (err error) { } return } + +func GetImUserNicknameAndAvatar(imUserIds []int64) (map[int64]friend_circles.ImUserNicknameAndAvatar, error) { + var imUsers []model.User + err := db.DbIm.In("id", imUserIds).Find(&imUsers) + if err != nil { + return nil, err + } + scheme, domain := svc.ImageBucket(db.Db) + info := make(map[int64]friend_circles.ImUserNicknameAndAvatar, len(imUsers)) + for _, user := range imUsers { + tempUrl := svc.ImageFormatWithBucket(scheme, domain, user.AvatarUrl) + temp := friend_circles.ImUserNicknameAndAvatar{ + NickName: user.Nickname, + Avatar: tempUrl, + } + info[user.Id] = temp + } + return info, nil +}