-
Notifications
You must be signed in to change notification settings - Fork 0
/
favorite.go
106 lines (94 loc) · 2.94 KB
/
favorite.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package favorite
import (
"context"
"fmt"
"net/http"
"strconv"
"github.com/alph00/tiktok-tiny/cmd/api-gateway/handlers/myutil"
"github.com/alph00/tiktok-tiny/cmd/api-gateway/rpc"
kitex "github.com/alph00/tiktok-tiny/kitex_gen/favorite"
"github.com/alph00/tiktok-tiny/model"
"github.com/alph00/tiktok-tiny/pkg/viper"
"github.com/cloudwego/hertz/pkg/app"
"github.com/cloudwego/hertz/pkg/common/utils"
)
func FavoriteAction(ctx context.Context, c *app.RequestContext) {
fmt.Println("调用了api/handler/favorite下的user.go FavoriteAction函数")
token := c.Query("token")
videoId := c.Query("video_id")
actionType := c.Query("action_type")
// TODO:这里应该不需要判断是否合法吧
id, err := strconv.ParseInt(videoId, 10, 64)
if err != nil {
myutil.ResponseMsg(http.StatusOK, c, utils.H{
"status_code": -1,
"status_msg": "video_id 不合法", //res.StatusMsg,
})
return
}
acttype, err := strconv.Atoi(actionType)
if err != nil {
myutil.ResponseMsg(http.StatusOK, c, utils.H{
"status_code": -1,
"status_msg": "actiontype 不合法", //res.StatusMsg,
})
return
}
v, _ := c.Get(viper.Read("jwt").GetString("IdentityKey"))
fmt.Printf("登录用户ID为:%d\n", int64(v.(*model.User).ID))
req := &kitex.FavoriteActionRequest{
UserId: int64(v.(*model.User).ID),
Token: token,
VideoId: id,
ActionType: int32(acttype),
}
fmt.Println("进行rpc注册,调用handlers下的FavoriteAction函数")
res, _ := rpc.FavoriteAction(ctx, req)
fmt.Printf("进行rpc注册,调用handlers下的FavoriteAction函数完成,%v\n", res)
if res.StatusCode == -1 {
myutil.ResponseMsg(http.StatusOK, c, utils.H{
"status_code": -1,
"status_msg": res.StatusMsg, //res.StatusMsg,
})
return
}
myutil.ResponseMsg(http.StatusOK, c, utils.H{
"status_code": 0,
"status_msg": res.StatusMsg, //res.StatusMsg,
})
}
func FavoriteList(ctx context.Context, c *app.RequestContext) {
fmt.Println("调用了api/handler/favorite下的user.go FavoriteList函数")
token := c.Query("token")
userId := c.Query("user_id")
// actionType := c.Query("action_type")
// TODO:这里应该不需要判断是否合法吧
UserId, err := strconv.ParseInt(userId, 10, 64)
if err != nil {
myutil.ResponseMsg(http.StatusOK, c, utils.H{
"status_code": -1,
"status_msg": "user_id 不合法", //res.StatusMsg,
})
return
}
req := &kitex.FavoriteListRequest{
UserId: UserId,
Token: token,
}
fmt.Println("进行rpc注册,调用rpc/user下的FavoriteList函数")
res, _ := rpc.FavoriteList(ctx, req)
fmt.Printf("进行rpc注册,调用rpc/user下的FavoriteList函数完成,%v\n", res)
if res.StatusCode == -1 {
myutil.ResponseMsg(http.StatusOK, c, utils.H{
"status_code": -1,
"status_msg": res.StatusMsg, //res.StatusMsg,
"video_list": nil,
})
return
}
myutil.ResponseMsg(http.StatusOK, c, utils.H{
"status_code": 0,
"status_msg": res.StatusMsg, //res.StatusMsg,
"video_list": res.VideoList,
})
}