156 lines
3.9 KiB
Go
156 lines
3.9 KiB
Go
package service
|
|
|
|
import (
|
|
"context"
|
|
"crypto/sha256"
|
|
"encoding/hex"
|
|
"encoding/json"
|
|
"errors"
|
|
"github.com/google/uuid"
|
|
"github.com/labstack/echo/v4"
|
|
"golang.org/x/time/rate"
|
|
"mylomen_server/common/constant"
|
|
"mylomen_server/common/dto"
|
|
"mylomen_server/common/email"
|
|
"mylomen_server/common/logs"
|
|
"mylomen_server/common/utils"
|
|
"mylomen_server/infrastructure/convert"
|
|
"mylomen_server/infrastructure/redis"
|
|
"mylomen_server/infrastructure/repository"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
type login struct {
|
|
}
|
|
|
|
var Login login
|
|
|
|
// 令牌桶大小为 100, 以每秒 10 个 Token 的速率向桶中放置 Token
|
|
var limiter = rate.NewLimiter(10, 10)
|
|
|
|
// Register 注册
|
|
func (l login) Register(ctx context.Context, req dto.RegisterReq) error {
|
|
acUser := repository.GUser.FindByReq(ctx, req.LoginReq)
|
|
if acUser != nil && acUser.Deleted == false {
|
|
return errors.New("user exist")
|
|
}
|
|
|
|
userDO := convert.UserReq2DO(req)
|
|
|
|
//密码加密
|
|
h := sha256.Sum256([]byte(req.Password))
|
|
passHash := hex.EncodeToString(h[:])
|
|
userDO.Pwd = &passHash
|
|
|
|
return repository.GUser.Create(ctx, &userDO)
|
|
}
|
|
|
|
func (l login) Login(ctx context.Context, req dto.LoginReq) (*dto.UserVO, error) {
|
|
start := time.Now().UnixMilli()
|
|
|
|
//1. 查询用户
|
|
acUser := repository.GUser.FindByReq(ctx, req)
|
|
if acUser == nil || acUser.Deleted == true {
|
|
return nil, errors.New("user not exist")
|
|
}
|
|
|
|
//2. email 和 手机号登录 和account 需要验证密码。其他情况不需要验证密码
|
|
if req.Account != nil || req.Email != nil || req.Phone != nil {
|
|
if acUser.Pwd == nil {
|
|
return nil, errors.New("password not set")
|
|
}
|
|
// 验证账号密码
|
|
h := sha256.Sum256([]byte(req.Password))
|
|
passHash := hex.EncodeToString(h[:])
|
|
if acUser.Pwd == nil || passHash != *acUser.Pwd {
|
|
return nil, errors.New("password is error")
|
|
}
|
|
}
|
|
|
|
//3. 组装数据
|
|
result := convert.UserDO2VO(*acUser)
|
|
|
|
//生成token todo 使用jwt
|
|
token := uuid.New().String()
|
|
token = strings.ReplaceAll(token, "-", "")
|
|
result.Token = token
|
|
|
|
diff := time.Now().UnixMilli() - start
|
|
logs.NewLog("").Infof("Login cost: %d", diff)
|
|
return &result, nil
|
|
}
|
|
|
|
func (l login) SendResetPwdCode(ctx context.Context, account string) error {
|
|
//1. 验证账号
|
|
acUser := repository.GUser.FindByAccount(ctx, account)
|
|
if acUser == nil || acUser.Deleted == true {
|
|
return errors.New("user not exist")
|
|
}
|
|
|
|
//2. 生成code & 发送
|
|
code := utils.GetPseudoRandomCode(6)
|
|
|
|
//3. save into redis
|
|
if err := redis.Set(constant.G_RESET_PWD_CODE+account, code, time.Duration(30)*time.Minute); err != nil {
|
|
return errors.New("cache illegal")
|
|
}
|
|
|
|
//频控
|
|
if !limiter.Allow() {
|
|
return errors.New("rate limit, please try again later")
|
|
}
|
|
|
|
//4. send code
|
|
if err := email.SendEmailVerifyCodeByEmail(code, account); err != nil {
|
|
return errors.New("send email code illegal")
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// ResetPwd 重置密码
|
|
func (l login) ResetPwd(ctx context.Context, req dto.ResetPwdReq) error {
|
|
//1. 验证code
|
|
redisStr, err := redis.Get(constant.G_RESET_PWD_CODE + req.Account)
|
|
if err != nil && redisStr != req.Code {
|
|
return errors.New("code is error")
|
|
}
|
|
|
|
//2. 查询用户
|
|
acUser := repository.GUser.FindByAccount(ctx, req.Account)
|
|
if acUser == nil || acUser.Deleted == true {
|
|
return errors.New("user not exist")
|
|
}
|
|
|
|
//3. 重置密码
|
|
h := sha256.Sum256([]byte(req.Password))
|
|
passHash := hex.EncodeToString(h[:])
|
|
acUser.Pwd = &passHash
|
|
repository.GUser.UpdateById(ctx, acUser)
|
|
|
|
//生成token
|
|
token := uuid.New().String()
|
|
token = strings.ReplaceAll(token, "-", "")
|
|
|
|
return nil
|
|
}
|
|
|
|
func (l login) GetLoginResult(ctx context.Context, c *echo.Context) *dto.UserVO {
|
|
accessToken := utils.GetAccessToken(c)
|
|
if accessToken == "" {
|
|
return nil
|
|
}
|
|
|
|
//redis
|
|
redisStr, err := redis.Get(constant.THIRD_LOGIN_TOKEN + accessToken)
|
|
if err != nil && redisStr != "" {
|
|
var loginInfo dto.UserLoginToken
|
|
if redisErr := json.Unmarshal([]byte(redisStr), &loginInfo); redisErr == nil {
|
|
return nil
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|