-
Notifications
You must be signed in to change notification settings - Fork 6
/
user_info.go
57 lines (48 loc) · 1.18 KB
/
user_info.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
package service
import (
"context"
"errors"
"github.com/chenmengangzhi29/douyin/dal/db"
"github.com/chenmengangzhi29/douyin/dal/pack"
"github.com/chenmengangzhi29/douyin/kitex_gen/user"
"github.com/chenmengangzhi29/douyin/pkg/constants"
"github.com/chenmengangzhi29/douyin/pkg/jwt"
)
type UserInfoService struct {
ctx context.Context
}
// NewUserInfoService new UserInfoService
func NewUserInfoService(ctx context.Context) *UserInfoService {
return &UserInfoService{
ctx: ctx,
}
}
func (s *UserInfoService) UserInfo(req *user.UserInfoRequest) (*user.User, error) {
Jwt := jwt.NewJWT([]byte(constants.SecretKey))
currentId, err := Jwt.CheckToken(req.Token)
if err != nil {
return nil, err
}
userIds := []int64{req.UserId}
users, err := db.QueryUserByIds(s.ctx, userIds)
if err != nil {
return nil, err
}
if len(users) == 0 {
return nil, errors.New("user not exist")
}
user := users[0]
relationMap, err := db.QueryRelationByIds(s.ctx, currentId, userIds)
if err != nil {
return nil, err
}
var isFollow bool
_, ok := relationMap[req.UserId]
if ok {
isFollow = true
} else {
isFollow = false
}
userInfo := pack.UserInfo(user, isFollow)
return userInfo, nil
}