github.com/Go-To-Byte/DouSheng/user_center@v0.0.0-20230524130918-ad531c1a3f6a/apps/user/impl/compose_sync.go (about)

     1  // Package impl @Author: Ciusyan 2023/2/28
     2  package impl
     3  
     4  import (
     5  	"context"
     6  	"github.com/Go-To-Byte/DouSheng/api_rooter/apps/token"
     7  	"github.com/Go-To-Byte/DouSheng/dou_kit/constant"
     8  	"github.com/Go-To-Byte/DouSheng/interaction_service/apps/favorite"
     9  	"github.com/Go-To-Byte/DouSheng/relation_service/apps/relation"
    10  	"github.com/Go-To-Byte/DouSheng/user_center/apps/user"
    11  	"github.com/Go-To-Byte/DouSheng/video_service/apps/video"
    12  )
    13  
    14  // get follow list count, user += followListCount, user += followerListCount
    15  func (s *userServiceImpl) composeRelation(ctx context.Context, uResp *user.User) (errs []error) {
    16  
    17  	// 携程内部必须捕获 panic()
    18  	defer func() {
    19  		if r := recover(); r != nil {
    20  			s.l.Errorf("user composeRelation:panic %v", r)
    21  			return
    22  		}
    23  	}()
    24  
    25  	relationCountReq := relation.NewListCountRequest()
    26  	relationCountReq.UserId = uResp.Id
    27  	relationCountReq.Type = relation.CountType_ALL
    28  	relationCount, err := s.relation.ListCount(ctx, relationCountReq)
    29  	if err != nil {
    30  		s.l.Error(err)
    31  		return append(errs, err)
    32  	}
    33  
    34  	uResp.FollowCount = &relationCount.FollowCount
    35  	uResp.FollowerCount = &relationCount.FollowerCount
    36  
    37  	loginUId, err := s.getLoginUID(ctx)
    38  	if err != nil {
    39  		s.l.Errorf("user composeRelation:根据Token获取ID失败,%s", err.Error())
    40  		return append(errs, err)
    41  	}
    42  
    43  	// 是否关注查询用户
    44  	req := relation.NewUserFollowPo()
    45  	req.UserId = loginUId
    46  	req.FollowId = uResp.Id
    47  	isFollow, err := s.relation.IsFollower(ctx, req)
    48  	if err != nil {
    49  		s.l.Error(err)
    50  		return append(errs, err)
    51  	}
    52  
    53  	uResp.IsFollow = isFollow.MyFollower
    54  
    55  	return
    56  }
    57  
    58  func (s *userServiceImpl) getLoginUID(ctx context.Context) (int64, error) {
    59  	// user += isFollow (relation 添加 isFollow 方法)
    60  
    61  	loginUserId := ctx.Value(constant.USER_ID)
    62  	if loginUserId == nil {
    63  		// 从Token中获取
    64  		tkValue := ctx.Value(constant.REQUEST_TOKEN)
    65  		if tkValue == nil {
    66  			tkValue = ""
    67  		}
    68  
    69  		tkReq := token.NewValidateTokenRequest(tkValue.(string))
    70  		uIdResp, err := s.tokenService.GetUIDFromTk(ctx, tkReq)
    71  		loginUserId = uIdResp.UserId
    72  		if err != nil {
    73  			return 0, err
    74  		}
    75  
    76  	}
    77  
    78  	return loginUserId.(int64), nil
    79  }
    80  
    81  // get publish list, user += publishCount
    82  func (s *userServiceImpl) composeVideo(ctx context.Context, uResp *user.User) (errs []error) {
    83  
    84  	// 携程内部必须捕获 panic()
    85  	defer func() {
    86  		if r := recover(); r != nil {
    87  			s.l.Errorf("user composeRelation:panic %v", r)
    88  			return
    89  		}
    90  	}()
    91  
    92  	publishCountReq := video.NewPublishListCountRequest(uResp.Id)
    93  	publishCount, err := s.video.ComposeVideoCount(ctx, publishCountReq)
    94  	if err != nil {
    95  		s.l.Error(err)
    96  		errs = append(errs, err)
    97  	}
    98  
    99  	uResp.WorkCount = &publishCount.PublishCount
   100  	uResp.TotalFavorited = &publishCount.AcquireTotalFavorite
   101  
   102  	return
   103  }
   104  
   105  // get favorite list, user += favoriteCount
   106  func (s *userServiceImpl) composeFavorite(ctx context.Context, uResp *user.User) (errs []error) {
   107  
   108  	// 携程内部必须捕获 panic()
   109  	defer func() {
   110  		if r := recover(); r != nil {
   111  			s.l.Errorf("user composeRelation:panic %v", r)
   112  			return
   113  		}
   114  	}()
   115  
   116  	favoriteListReq := favorite.NewFavoriteCountRequest()
   117  	favoriteListReq.UserId = uResp.Id
   118  	favoriteCountResp, err := s.favorite.FavoriteCount(ctx, favoriteListReq)
   119  	if err != nil {
   120  		s.l.Error(err)
   121  		errs = append(errs, err)
   122  	}
   123  
   124  	uResp.FavoriteCount = &favoriteCountResp.FavoriteCount
   125  
   126  	// TODO:获赞数量
   127  
   128  	return
   129  }