-
Notifications
You must be signed in to change notification settings - Fork 1
/
loginLogic.go
66 lines (55 loc) · 1.67 KB
/
loginLogic.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
package logic
import (
"context"
"douyin-tiktok/common/utils"
"douyin-tiktok/service/user/model"
"errors"
"golang.org/x/crypto/bcrypt"
"strconv"
"strings"
"time"
"douyin-tiktok/service/user/cmd/api/internal/svc"
"douyin-tiktok/service/user/cmd/api/internal/types"
"github.com/zeromicro/go-zero/core/logx"
)
type LoginLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LoginLogic {
return &LoginLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *LoginLogic) Login(req *types.LoginReq) (map[string]interface{}, error) {
var (
username = strings.TrimSpace(req.Username)
password = strings.TrimSpace(req.Password)
)
userInfo := &model.UserInfo{Username: username}
has, err := l.svcCtx.UserInfo.Cols("`id`", "`username`", "`password`", "`name`").Get(userInfo)
if err != nil || !has {
logx.Errorf("[DB ERROR] ")
return nil, errors.New("该账户不存在!")
}
err = bcrypt.CompareHashAndPassword([]byte(userInfo.Password), []byte(password))
if err != nil {
return nil, errors.New("帐号或密码错误!")
}
token, err := utils.GenToken(userInfo)
if err != nil {
return nil, errors.New("出错啦,请重试!")
}
key := utils.UserLogged + strconv.FormatInt(userInfo.Id, 10)
if err = l.svcCtx.Redis.Set(l.ctx, key, token, 7*24*time.Hour).Err(); err != nil {
logx.Errorf("[REDIS ERROR] Login 保存用户token失败,userid:%v %v\n", userInfo.Id, err)
l.svcCtx.Redis.Set(l.ctx, key, token, 7*24*time.Hour) // 重试
}
resp := utils.GenOkResp()
resp["user_id"] = userInfo.Id
resp["token"] = token
return resp, nil
}