-
Notifications
You must be signed in to change notification settings - Fork 38
/
handler.go
133 lines (113 loc) · 3.67 KB
/
handler.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// Copyright 2022 a76yyyy && CloudWeGo Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/*
* @Author: a76yyyy q981331502@163.com
* @Date: 2022-06-10 14:47:34
* @LastEditors: a76yyyy q981331502@163.com
* @LastEditTime: 2022-06-19 00:01:30
* @FilePath: /tiktok/cmd/user/handler.go
* @Description: 定义 User RPC Server 端的相关接口
*/
package main
import (
"context"
"github.com/a76yyyy/tiktok/cmd/user/command"
"github.com/a76yyyy/tiktok/dal/pack"
"github.com/a76yyyy/tiktok/kitex_gen/user"
"github.com/a76yyyy/tiktok/pkg/errno"
"github.com/a76yyyy/tiktok/pkg/jwt"
)
// UserSrvImpl implements the last service interface defined in the IDL.
type UserSrvImpl struct{}
// Register implements the UserSrvImpl interface.
func (s *UserSrvImpl) Register(ctx context.Context, req *user.DouyinUserRegisterRequest) (resp *user.DouyinUserRegisterResponse, err error) {
if len(req.Username) == 0 || len(req.Password) == 0 {
resp = pack.BuilduserRegisterResp(errno.ErrBind)
return resp, nil
}
err = command.NewCreateUserService(ctx).CreateUser(req, Argon2Config)
if err != nil {
resp = pack.BuilduserRegisterResp(err)
return resp, nil
}
uid, err := command.NewCheckUserService(ctx).CheckUser(req)
if err != nil {
resp = pack.BuilduserRegisterResp(err)
return resp, nil
}
token, err := Jwt.CreateToken(jwt.CustomClaims{
Id: int64(uid),
})
if err != nil {
resp = pack.BuilduserRegisterResp(errno.ErrSignatureInvalid)
return resp, nil
}
resp = pack.BuilduserRegisterResp(errno.Success)
resp.UserId = uid
resp.Token = token
return resp, nil
}
// Login implements the UserSrvImpl interface.
func (s *UserSrvImpl) Login(ctx context.Context, req *user.DouyinUserRegisterRequest) (resp *user.DouyinUserRegisterResponse, err error) {
if len(req.Username) == 0 || len(req.Password) == 0 {
resp = pack.BuilduserRegisterResp(errno.ErrBind)
return resp, nil
}
uid, err := command.NewCheckUserService(ctx).CheckUser(req)
if err != nil {
resp = pack.BuilduserRegisterResp(err)
return resp, nil
}
token, err := Jwt.CreateToken(jwt.CustomClaims{
Id: int64(uid),
})
if err != nil {
resp = pack.BuilduserRegisterResp(errno.ErrSignatureInvalid)
return resp, nil
}
resp = pack.BuilduserRegisterResp(errno.Success)
resp.UserId = uid
resp.Token = token
return resp, nil
}
// GetUserById implements the UserSrvImpl interface.
func (s *UserSrvImpl) GetUserById(ctx context.Context, req *user.DouyinUserRequest) (resp *user.DouyinUserResponse, err error) {
claim, err := Jwt.ParseToken(req.Token)
if err != nil {
resp = pack.BuilduserUserResp(errno.ErrTokenInvalid)
return resp, nil
}
// else if claim.Id != int64(req.UserId) {
// resp = pack.BuilduserUserResp(errno.ErrValidation)
// return resp, nil
// }
if req.UserId < 0 {
resp = pack.BuilduserUserResp(errno.ErrBind)
return resp, nil
}
user, err := command.NewMGetUserService(ctx).MGetUser(req, claim.Id)
if err != nil {
resp = pack.BuilduserUserResp(err)
return resp, nil
}
if claim.Id == req.UserId {
user.IsFollow = true
} else {
// TODO 获取claim.id 是否已关注 req.userid
user.IsFollow = false
}
resp = pack.BuilduserUserResp(errno.Success)
resp.User = user
return resp, nil
}