diff --git a/biz/handler/relation/friend_list_service.go b/biz/handler/relation/friend_list_service.go index 255f360..90e2552 100755 --- a/biz/handler/relation/friend_list_service.go +++ b/biz/handler/relation/friend_list_service.go @@ -4,6 +4,8 @@ package relation import ( "context" + "github.com/ClubWeGo/douyin/kitex_server" + "github.com/ClubWeGo/douyin/tools" relation "github.com/ClubWeGo/douyin/biz/model/relation" "github.com/cloudwego/hertz/pkg/app" @@ -20,8 +22,34 @@ func FriendListMethod(ctx context.Context, c *app.RequestContext) { c.String(consts.StatusBadRequest, err.Error()) return } + resp := new(relation.FollowerListResp) - resp := new(relation.FriendListResp) + // 鉴权 + ifvalid, myUid, err := tools.ValidateToken(req.Token) + if err != nil { + msgFailed := "非法token" + resp.StatusCode = 1 + resp.StatusMsg = &msgFailed + c.JSON(consts.StatusOK, resp) + return + } + if !ifvalid { + msgFailed := "token无效" + resp.StatusCode = 1 + resp.StatusMsg = &msgFailed + c.JSON(consts.StatusOK, resp) + return + } + // 获取好友列表 + friendList, err := kitex_server.GetFriendList(myUid, req.UserID) + if err != nil { + errMsg := err.Error() + resp.StatusCode = 1 + resp.StatusMsg = &errMsg + c.JSON(consts.StatusOK, resp) + return + } + resp.UserList = friendList c.JSON(consts.StatusOK, resp) } diff --git a/kitex_server/relationservice.go b/kitex_server/relationservice.go index b91e433..b93c015 100755 --- a/kitex_server/relationservice.go +++ b/kitex_server/relationservice.go @@ -8,6 +8,7 @@ import ( "github.com/ClubWeGo/douyin/biz/model/relation" relationserver "github.com/ClubWeGo/relationmicro/kitex_gen/relation" "github.com/prometheus/common/log" + "strconv" ) // 响应码 @@ -98,20 +99,14 @@ func Follow(myUid int64, targetUid int64, actionType int32) error { func GetFollowList(myUid int64, targetUid int64) ([]*core.User, error) { resp, err := Relationclient.GetFollowListMethod(context.Background(), &relationserver.GetFollowListReq{MyId: &myUid, TargetId: targetUid}) if err != nil { - log.Errorf("rpc请求relation服务失败,详情:%s", err) + log.Errorf("GetFollowList rpc请求relation服务失败,详情:%s", err) return nil, fmt.Errorf("本次请求失败,请稍后重试") } switch resp.StatusCode { case SUCCESS: userList := make([]*core.User, len(resp.UserList)) for i, user := range resp.GetUserList() { - userList[i] = &core.User{ - ID: user.Id, - Name: user.Name, - FollowCount: user.FollowCount, - FollowerCount: user.FollowerCount, - IsFollow: user.IsFollow, - } + userList[i] = ConvertCoreUser(user) } return userList, nil case ERROR: @@ -130,20 +125,14 @@ func GetFollowList(myUid int64, targetUid int64) ([]*core.User, error) { func GetFollowerList(myUid int64, targetUid int64) ([]*core.User, error) { resp, err := Relationclient.GetFollowerListMethod(context.Background(), &relationserver.GetFollowerListReq{MyId: &myUid, TargetId: targetUid}) if err != nil { - log.Errorf("rpc请求relation服务失败,详情:%s", err) + log.Errorf("GetFollowerList rpc请求relation服务失败,详情:%s", err) return nil, fmt.Errorf("本次请求失败,请稍后重试") } switch resp.StatusCode { case SUCCESS: userList := make([]*core.User, len(resp.UserList)) for i, user := range resp.GetUserList() { - userList[i] = &core.User{ - ID: user.Id, - Name: user.Name, - FollowCount: user.FollowCount, - FollowerCount: user.FollowerCount, - IsFollow: user.IsFollow, - } + userList[i] = ConvertCoreUser(user) } return userList, nil case ERROR: @@ -158,8 +147,30 @@ func GetFollowerList(myUid int64, targetUid int64) ([]*core.User, error) { return nil, fmt.Errorf("本次请求失败,请稍后重试") } -func GetFriendList() { - +// 好友列表 +func GetFriendList(myUid int64, targetUid int64) ([]*core.User, error) { + resp, err := Relationclient.GetFriendListMethod(context.Background(), &relationserver.GetFriendListReq{MyUid: &myUid, TargetUid: targetUid}) + if err != nil { + log.Errorf("GetFriendList rpc请求relation服务失败,详情:%s", err) + return nil, fmt.Errorf("本次请求失败,请稍后重试") + } + switch resp.StatusCode { + case SUCCESS: + userList := make([]*core.User, len(resp.FriendList)) + for i, user := range resp.GetFriendList() { + userList[i] = ConvertCoreUser(user) + } + return userList, nil + case ERROR: + log.Errorf("relation服务异常,详情:%s", *resp.Msg) + break + case VERIFY: + log.Errorf("relation服务参数校验异常,详情:%s", *resp.Msg) + break + default: + break + } + return nil, fmt.Errorf("本次请求失败,请稍后重试") } // 校验关注参数 @@ -181,3 +192,21 @@ func VerifyFollowParam(myUid int64, targetUid int64, actionType int32) *string { func GetIsFollowSetByUserIdSet(idSet []int64) (isFollowSet []int64, err error) { return []int64{}, nil } + +// kitex relationserver 数据传输 user -> kitex 回显 core.User +func ConvertCoreUser(user *relationserver.User) *core.User { + totalFavourited := strconv.FormatInt(*user.TotalFavorited, 10) + return &core.User{ + ID: user.Id, + Name: user.Name, + FollowCount: *user.FollowCount, + FollowerCount: *user.FollowerCount, + IsFollow: user.IsFollow, + Avatar: *user.Avatar, + BackgroundImage: *user.BackgroundImage, + Signature: *user.Signature, + TotalFavourited: totalFavourited, + WorkCount: *user.WorkCount, + FavoriteCount: *user.FavoriteCount, + } +}