-
Notifications
You must be signed in to change notification settings - Fork 1
/
user.go
40 lines (34 loc) · 1.23 KB
/
user.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
package user
import (
"context"
"fmt"
"github.com/go-redis/redis/v8"
UserConstant "github.com/herman-hang/herman/application/constants/admin/user"
"github.com/herman-hang/herman/application/repositories"
"github.com/herman-hang/herman/kernel/core"
utils2 "github.com/herman-hang/herman/kernel/utils"
"time"
)
// Login 用户登录
// @param map data 前端请求数据
// @return interface{} 返回一个token值
func Login(data map[string]interface{}) interface{} {
user := repositories.User().GetUserInfo(fmt.Sprintf("%s", data["user"]))
// 设置上下文
ctx := context.Background()
// 设置Redis错误密码的key
key := fmt.Sprintf("user_password_error:%d", user.Id)
// 获取错误登录次数
errorNumber, err := core.Redis().Get(ctx, key).Int()
// 判断是否登录次数过多
if err != redis.Nil && errorNumber > UserConstant.LoginErrorLimitNumber {
panic(UserConstant.ErrorLoginOverload)
}
// 密码验证
if !utils2.ComparePasswords(user.Password, fmt.Sprintf("%s", data["password"])) {
core.Redis().Set(ctx, key, errorNumber+UserConstant.Increment, time.Minute*UserConstant.KeyValidity)
panic(UserConstant.PasswordError)
}
// 返回token
return utils2.GenerateToken(&utils2.Claims{Uid: user.Id, Guard: "user"})
}