Merge branch 'feature/pdf-recognition' into test
This commit is contained in:
@@ -21,12 +21,14 @@ import (
|
||||
)
|
||||
|
||||
type UserService struct {
|
||||
userDao *dao.UserDao
|
||||
userDao *dao.UserDao
|
||||
emailSendLogDao *dao.EmailSendLogDao
|
||||
}
|
||||
|
||||
func NewUserService() *UserService {
|
||||
return &UserService{
|
||||
userDao: dao.NewUserDao(),
|
||||
userDao: dao.NewUserDao(),
|
||||
emailSendLogDao: dao.NewEmailSendLogDao(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -116,10 +118,10 @@ func (svc *UserService) GetUserInfo(ctx context.Context, uid int64) (*dao.User,
|
||||
return user, nil
|
||||
}
|
||||
|
||||
func (svc *UserService) SendEmailCode(ctx context.Context, emailAddr string) error {
|
||||
func (svc *UserService) SendEmailVerifyCode(ctx context.Context, emailAddr string) error {
|
||||
limit, err := cache.GetUserSendEmailLimit(ctx, emailAddr)
|
||||
if err != nil {
|
||||
log.Error(ctx, "func", "SendEmailCode", "msg", "get send email limit error", "error", err)
|
||||
log.Error(ctx, "func", "SendEmailVerifyCode", "msg", "get send email limit error", "error", err)
|
||||
return err
|
||||
}
|
||||
if limit >= cache.UserSendEmailLimitCount {
|
||||
@@ -128,33 +130,53 @@ func (svc *UserService) SendEmailCode(ctx context.Context, emailAddr string) err
|
||||
|
||||
code := fmt.Sprintf("%06d", rand.Intn(1000000))
|
||||
|
||||
subject := "TexPixel 邮箱验证码"
|
||||
body := fmt.Sprintf(`<p>您的验证码为:<strong>%s</strong>,10分钟内有效,请勿泄露。</p>`, code)
|
||||
if err = email.Send(ctx, emailAddr, subject, body); err != nil {
|
||||
log.Error(ctx, "func", "SendEmailCode", "msg", "send email error", "error", err)
|
||||
subject, body := email.BuildVerifyCodeEmail(emailAddr, code)
|
||||
if err := email.Send(ctx, emailAddr, subject, body); err != nil {
|
||||
log.Error(ctx, "func", "SendEmailVerifyCode", "msg", "send email error", "error", err)
|
||||
return err
|
||||
}
|
||||
|
||||
if cacheErr := cache.SetUserEmailCode(ctx, emailAddr, code); cacheErr != nil {
|
||||
log.Error(ctx, "func", "SendEmailCode", "msg", "set email code error", "error", cacheErr)
|
||||
log.Error(ctx, "func", "SendEmailVerifyCode", "msg", "set email code error", "error", cacheErr)
|
||||
}
|
||||
if cacheErr := cache.SetUserSendEmailLimit(ctx, emailAddr); cacheErr != nil {
|
||||
log.Error(ctx, "func", "SendEmailCode", "msg", "set send email limit error", "error", cacheErr)
|
||||
log.Error(ctx, "func", "SendEmailVerifyCode", "msg", "set send email limit error", "error", cacheErr)
|
||||
}
|
||||
|
||||
record := &dao.EmailSendLog{Email: emailAddr, Status: dao.EmailSendStatusSent}
|
||||
if logErr := svc.emailSendLogDao.Create(dao.DB.WithContext(ctx), record); logErr != nil {
|
||||
log.Error(ctx, "func", "SendEmailVerifyCode", "msg", "create email send log error", "error", logErr)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (svc *UserService) RegisterByEmail(ctx context.Context, emailAddr, password, code string) (uid int64, err error) {
|
||||
func (svc *UserService) RegisterByEmail(ctx context.Context, emailAddr, password, verifyCode string) (uid int64, err error) {
|
||||
storedCode, err := cache.GetUserEmailCode(ctx, emailAddr)
|
||||
if err != nil {
|
||||
log.Error(ctx, "func", "RegisterByEmail", "msg", "get email code error", "error", err)
|
||||
return 0, err
|
||||
}
|
||||
if storedCode == "" || storedCode != code {
|
||||
log.Warn(ctx, "func", "RegisterByEmail", "msg", "invalid email code", "email", emailAddr)
|
||||
|
||||
if storedCode == "" || storedCode != verifyCode {
|
||||
return 0, common.ErrEmailCodeError
|
||||
}
|
||||
|
||||
_ = cache.DeleteUserEmailCode(ctx, emailAddr)
|
||||
|
||||
uid, err = svc.registerByEmailInternal(ctx, emailAddr, password)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
if logErr := svc.emailSendLogDao.MarkRegistered(dao.DB.WithContext(ctx), emailAddr); logErr != nil {
|
||||
log.Error(ctx, "func", "RegisterByEmail", "msg", "mark email send log registered error", "error", logErr)
|
||||
}
|
||||
|
||||
return uid, nil
|
||||
}
|
||||
|
||||
func (svc *UserService) registerByEmailInternal(ctx context.Context, emailAddr, password string) (uid int64, err error) {
|
||||
existingUser, err := svc.userDao.GetByEmail(dao.DB.WithContext(ctx), emailAddr)
|
||||
if err != nil {
|
||||
log.Error(ctx, "func", "RegisterByEmail", "msg", "get user by email error", "error", err)
|
||||
|
||||
Reference in New Issue
Block a user