Skip to content

Commit 49a2f33

Browse files
authored
Merge pull request #36 from OriTsuruHime/develop
修改游客状态的feed
2 parents b9032a0 + cc3d783 commit 49a2f33

File tree

3 files changed

+25
-52
lines changed

3 files changed

+25
-52
lines changed

server/gateway-center/controller/videoController.go

+24-7
Original file line numberDiff line numberDiff line change
@@ -81,14 +81,15 @@ func PublishAction(c *gin.Context) {
8181
VBResponse: baseResponse.VBResponse{StatusCode: e.Success, StatusMsg: e.GetMsg(e.Success)},
8282
})
8383
//投稿成功之后重新查询用户投稿列表,更新缓存
84+
value, _ := c.Get("UserId")
85+
userId := int64(value.(uint))
8486
//先删除Key
85-
token := grpcClient.ValidateToken(params.Token)
86-
var PLKey = fmt.Sprintf("PersonVideoList:%d", token)
87+
var PLKey = fmt.Sprintf("PersonVideoList:%d", userId)
8788
cache.RedisDeleteKey(context.Background(), PLKey)
8889
var videoList baseResponse.VideoArray
8990
//调用grpc
9091
videos, err := grpcClient.PublishList(context.Background(), &pb.DouyinPublishListRequest{
91-
UserId: token,
92+
UserId: int64(userId),
9293
Token: params.Token,
9394
})
9495
if err != nil {
@@ -225,7 +226,7 @@ func Feed(c *gin.Context) {
225226
}
226227
isLogin := false
227228
isFollow := false
228-
userId := grpcClient.ValidateToken(params.Token)
229+
userId := validateToken(params.Token) //验证token是否有效,有效则为用户状态,无效则为游客状态
229230
if userId != -1 {
230231
isLogin = true
231232
}
@@ -255,15 +256,15 @@ func Feed(c *gin.Context) {
255256

256257
for _, video := range videos {
257258
var info *pb.DouyinUserResponse
258-
if isLogin {
259+
if isLogin { //用户模式
259260
info, err = grpcClient.GetUserById(context.Background(), uint(video.Author.Id), uint(userId), params.Token)
260261
if err != nil {
261262
util.LogrusObj.Errorf("获取User失败 UserId:%d UserToken:%d", video.Author.Id, &params.Token)
262263
continue
263264
}
264265
isFollow = info.User.IsFollow
265-
} else {
266-
info, err = grpcClient.GetUserById(context.Background(), uint(video.Author.Id), 0, "")
266+
} else { //游客模式
267+
info, err = grpcClient.GetUserById(context.Background(), uint(video.Author.Id), uint(video.Author.Id), "")
267268
if err != nil {
268269
util.LogrusObj.Errorf("获取User失败 UserId:%d UserToken:%d", video.Author.Id, &params.Token)
269270
continue
@@ -501,3 +502,19 @@ func ListFav(c *gin.Context) {
501502
}
502503
c.JSON(http.StatusOK, response)
503504
}
505+
506+
// 如果token验证失败则返回user为-1
507+
func validateToken(token string) int64 {
508+
if len(token) == 0 {
509+
return -1
510+
}
511+
parseToken, err := util.ParseToken(token)
512+
if err != nil {
513+
return -1
514+
}
515+
// 判断 token 是否过期
516+
if parseToken.ExpiresAt < time.Now().Unix() {
517+
return -1
518+
}
519+
return int64(parseToken.ID)
520+
}

server/gateway-center/grpcClient/VideoClient.go

-44
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,10 @@ package grpcClient
22

33
import (
44
"context"
5-
"fmt"
6-
"gateway-center/cache"
75
"gateway-center/pkg/e"
8-
"gateway-center/util"
96
"github.com/OrionLi/douyin-backend/pkg/pb"
107
"google.golang.org/grpc"
118
"google.golang.org/grpc/metadata"
12-
"time"
139
)
1410

1511
// ResetVideoStreamClient 重置VideoStreamClient
@@ -114,30 +110,6 @@ func Feed(ctx context.Context, req *pb.DouyinFeedRequest) ([]*pb.Video, int64, e
114110
return videos, *r.NextTime, nil
115111
}
116112

117-
// GetPublishListCount 通过Token获取userId对应的VideoCount
118-
func GetPublishListCount(ctx context.Context, token string) (count int64, err error) {
119-
userId := ValidateToken(token)
120-
PACountKey := fmt.Sprintf("publishlist:%d", userId)
121-
vCount, err := cache.RedisGetKey(ctx, PACountKey)
122-
if err != nil { //出错,没找到,查询数据库,并写入cache
123-
util.LogrusObj.Errorf("Cache get PublishListCount Error ErrorMSG:%s", err.Error())
124-
req := &pb.DouyinPublishListRequest{UserId: userId, Token: token}
125-
r, err := VideoClient.PublishList(ctx, req)
126-
if err != nil {
127-
return 0, err
128-
}
129-
if r.StatusCode != 0 {
130-
return 0, e.NewCustomError(int64(r.StatusCode), *r.StatusMsg)
131-
}
132-
err = cache.RedisSetKey(ctx, PACountKey, int64(len(r.VideoList)))
133-
if err != nil {
134-
util.LogrusObj.Errorf("Cache set PublishListCount Error ErrorMSG:%s", err.Error())
135-
}
136-
return int64(len(r.VideoList)), nil
137-
} //找到返回结果
138-
return util.StringToInt64(vCount), nil
139-
}
140-
141113
// PublishAction 投稿
142114
func PublishAction(ctx context.Context, req *pb.DouyinPublishActionRequest) error {
143115
err := VideoStreamClient.Send(req)
@@ -199,19 +171,3 @@ func ActionComment(ctx context.Context, req *pb.DouyinCommentActionRequest) (*pb
199171
func ListComment(ctx context.Context, req *pb.DouyinCommentListRequest) (*pb.DouyinCommentListResponse, error) {
200172
return VideoInteractionClient.ListComment(ctx, req)
201173
}
202-
203-
// ValidateToken 校验Token
204-
func ValidateToken(token string) int64 {
205-
if len(token) == 0 {
206-
return -1
207-
}
208-
parseToken, err := util.ParseToken(token)
209-
if err != nil {
210-
return -1
211-
}
212-
// 判断 token 是否过期
213-
if parseToken.ExpiresAt < time.Now().Unix() {
214-
return -1
215-
}
216-
return int64(parseToken.ID)
217-
}

server/video-center/handler/videoHandler.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ func (s *VideoServer) Feed(ctx context.Context, req *pb.DouyinFeedRequest) (*pb.
104104
var userId int64
105105
userId = 0
106106
//判断token,并获取userId
107-
util.LogrusObj.Infof("FeedRequest Token:%s lastest_time%d:", *req.Token, req.LatestTime)
107+
util.LogrusObj.Infof("FeedRequest Token:%s lastest_time:%d", *req.Token, req.LatestTime)
108108
if len(req.GetToken()) != 0 {
109109
token, err := util.ParseToken(req.GetToken())
110110
if err != nil {

0 commit comments

Comments
 (0)