|
|
|
@ -21,6 +21,7 @@ import (
|
|
|
|
|
"github.com/gogf/gf/v2/util/grand" |
|
|
|
|
"github.com/mssola/user_agent" |
|
|
|
|
"log" |
|
|
|
|
"time" |
|
|
|
|
"tyj_admin/api/v1/system" |
|
|
|
|
"tyj_admin/internal/consts" |
|
|
|
|
"tyj_admin/internal/dao" |
|
|
|
@ -64,13 +65,20 @@ type CodeModel struct {
|
|
|
|
|
Time *gtime.Time |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
type UserStatus struct { |
|
|
|
|
Account string |
|
|
|
|
Times int |
|
|
|
|
Time *gtime.Time |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
var ( |
|
|
|
|
notCheckAuthAdminIds *gset.Set //无需验证权限的用户id
|
|
|
|
|
userService = userImpl{ |
|
|
|
|
CasBinUserPrefix: "u_", |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
codes map[string]CodeModel |
|
|
|
|
codes map[string]CodeModel |
|
|
|
|
userStatus map[string]*UserStatus |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
func User() IUser { |
|
|
|
@ -79,6 +87,38 @@ func User() IUser {
|
|
|
|
|
|
|
|
|
|
func init() { |
|
|
|
|
codes = make(map[string]CodeModel) |
|
|
|
|
userStatus = make(map[string]*UserStatus) |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
const freeze = 5 |
|
|
|
|
const expTime = 10 |
|
|
|
|
|
|
|
|
|
func getUserStatus(account string) *UserStatus { |
|
|
|
|
initUserStatus() |
|
|
|
|
data, ok := userStatus[account] |
|
|
|
|
if !ok { |
|
|
|
|
userStatus[account] = &UserStatus{ |
|
|
|
|
Account: account, |
|
|
|
|
Times: 1, |
|
|
|
|
} |
|
|
|
|
} else if data.Times < 5 { |
|
|
|
|
data.Times += 1 |
|
|
|
|
if data.Times == 5 { |
|
|
|
|
data.Time = gtime.Now() |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
//log.Println("getUserStatus: ", gjson.MustEncodeString(data))
|
|
|
|
|
return data |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func initUserStatus() { |
|
|
|
|
keys := make([]string, len(userStatus)) |
|
|
|
|
for k, v := range userStatus { |
|
|
|
|
if v.Time != nil && v.Time.Add(freeze*time.Minute).Unix() < gtime.Now().Unix() { |
|
|
|
|
keys = append(keys, k) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func (s *userImpl) NotCheckAuthAdminIds(ctx context.Context) *gset.Set { |
|
|
|
@ -91,6 +131,11 @@ func (s *userImpl) NotCheckAuthAdminIds(ctx context.Context) *gset.Set {
|
|
|
|
|
|
|
|
|
|
func (s *userImpl) GetAdminUserByUsernamePassword(ctx context.Context, req *system.UserLoginReq) (user *model2.LoginUserRes, err error) { |
|
|
|
|
err = g.Try(ctx, func(ctx context.Context) { |
|
|
|
|
status := getUserStatus(req.Username) |
|
|
|
|
if status != nil && status.Time != nil { |
|
|
|
|
err = gerror.New("账号被冻结," + fmt.Sprint(status.Time.Add(freeze*time.Minute).Layout(consts.TIME_FORMAT)) + "后解冻。") |
|
|
|
|
return |
|
|
|
|
} |
|
|
|
|
user, err = s.GetUserByUsername(ctx, req.Username) |
|
|
|
|
liberr.ErrIsNil(ctx, err) |
|
|
|
|
liberr.ValueIsNil(user, "账号密码错误") |
|
|
|
@ -109,6 +154,11 @@ func (s *userImpl) GetAdminUserByUsernamePassword(ctx context.Context, req *syst
|
|
|
|
|
|
|
|
|
|
func (s *userImpl) GetAdminUserByPhone(ctx context.Context, req *system.UserLoginMobileReq) (user *model2.LoginUserRes, err error) { |
|
|
|
|
err = g.Try(ctx, func(ctx context.Context) { |
|
|
|
|
status := getUserStatus(req.Phone) |
|
|
|
|
if status != nil && status.Time != nil { |
|
|
|
|
err = gerror.New("账号被冻结," + fmt.Sprint(status.Time.Add(freeze*time.Minute).Layout(consts.TIME_FORMAT)) + "后解冻。") |
|
|
|
|
return |
|
|
|
|
} |
|
|
|
|
user, err = s.GetUserByMobile(ctx, req.Phone) |
|
|
|
|
liberr.ErrIsNil(ctx, err) |
|
|
|
|
liberr.ValueIsNil(user, "手机验证码错误") |
|
|
|
@ -119,8 +169,10 @@ func (s *userImpl) GetAdminUserByPhone(ctx context.Context, req *system.UserLogi
|
|
|
|
|
log.Printf("GetAdminUserByPhone >>> Phone:%s", req.Phone) |
|
|
|
|
//验证密码
|
|
|
|
|
model, ok := codes[req.Phone] |
|
|
|
|
log.Printf("GetAdminUserByPhone >>> Phone:%s, time %s ", req.Phone, fmt.Sprint(model.Time.Unix())+"--"+fmt.Sprint(gtime.Now().Unix())) |
|
|
|
|
if !ok || model.Time.Unix()+600 < gtime.Now().Unix() { |
|
|
|
|
if !ok || model.Time.Add(expTime*time.Minute).Unix() < gtime.Now().Unix() { |
|
|
|
|
if ok { |
|
|
|
|
log.Printf("GetAdminUserByPhone >>> Phone:%s, time %s ", req.Phone, fmt.Sprint(model.Time.Unix())+"--"+fmt.Sprint(gtime.Now().Unix())) |
|
|
|
|
} |
|
|
|
|
liberr.ErrIsNil(ctx, gerror.New("手机验证码错误")) |
|
|
|
|
} |
|
|
|
|
if libUtils.EncryptPassword(req.Code+fmt.Sprint(model.Time.Unix()), user.UserSalt) != model.Password { |
|
|
|
@ -132,6 +184,11 @@ func (s *userImpl) GetAdminUserByPhone(ctx context.Context, req *system.UserLogi
|
|
|
|
|
|
|
|
|
|
func (s *userImpl) GetAdminUserByEmail(ctx context.Context, req *system.UserLoginEmailReq) (user *model2.LoginUserRes, err error) { |
|
|
|
|
err = g.Try(ctx, func(ctx context.Context) { |
|
|
|
|
status := getUserStatus(req.Email) |
|
|
|
|
if status != nil && status.Time != nil { |
|
|
|
|
err = gerror.New("账号被冻结," + fmt.Sprint(status.Time.Add(freeze*time.Minute).Layout(consts.TIME_FORMAT)) + "后解冻。") |
|
|
|
|
return |
|
|
|
|
} |
|
|
|
|
user, err = s.GetUserByEmail(ctx, req.Email) |
|
|
|
|
liberr.ErrIsNil(ctx, err) |
|
|
|
|
liberr.ValueIsNil(user, "验证码错误") |
|
|
|
@ -139,11 +196,13 @@ func (s *userImpl) GetAdminUserByEmail(ctx context.Context, req *system.UserLogi
|
|
|
|
|
if user.UserStatus == 0 { |
|
|
|
|
liberr.ErrIsNil(ctx, gerror.New("账号已被冻结")) |
|
|
|
|
} |
|
|
|
|
log.Printf("GetAdminUserByPhone >>> Email:%s", req.Email) |
|
|
|
|
log.Printf("GetAdminUserByEmail >>> Email:%s", req.Email) |
|
|
|
|
//验证密码
|
|
|
|
|
model, ok := codes[req.Email] |
|
|
|
|
log.Printf("GetAdminUserByPhone >>> Email:%s, time %s ", req.Email, fmt.Sprint(model.Time.Unix())+"--"+fmt.Sprint(gtime.Now().Unix())) |
|
|
|
|
if !ok || model.Time.Unix()+600 < gtime.Now().Unix() { |
|
|
|
|
if !ok || model.Time.Add(expTime*time.Minute).Unix() < gtime.Now().Unix() { |
|
|
|
|
if !ok { |
|
|
|
|
log.Printf("GetAdminUserByEmail >>> Email:%s, time %s ", req.Email, fmt.Sprint(model.Time.Unix())+"--"+fmt.Sprint(gtime.Now().Unix())) |
|
|
|
|
} |
|
|
|
|
liberr.ErrIsNil(ctx, gerror.New("验证码错误")) |
|
|
|
|
} |
|
|
|
|
if libUtils.EncryptPassword(req.Code+fmt.Sprint(model.Time.Unix()), user.UserSalt) != model.Password { |
|
|
|
|