Merge branch 'fix/0.5.0/email-duplicate' into 'test'

fix: prompt form error when user enters email repeatedly

See merge request opensource/answer!280
This commit is contained in:
linkinstar 2022-11-28 08:50:03 +00:00
commit 85bd1f554c
3 changed files with 16 additions and 7 deletions

View File

@ -1,6 +1,8 @@
package handler
import (
"context"
"github.com/answerdev/answer/internal/base/constant"
"github.com/gin-gonic/gin"
"github.com/segmentfault/pacman/i18n"
@ -18,3 +20,12 @@ func GetLang(ctx *gin.Context) i18n.Language {
return i18n.DefaultLang
}
}
// GetLangByCtx get language from header
func GetLangByCtx(ctx context.Context) i18n.Language {
acceptLanguage, ok := ctx.Value(constant.AcceptLanguageFlag).(i18n.Language)
if ok {
return acceptLanguage
}
return i18n.DefaultLang
}

View File

@ -441,9 +441,6 @@ func (uc *UserController) UserChangeEmailSendCode(ctx *gin.Context) {
_, _ = uc.actionService.ActionRecordAdd(ctx, schema.ActionRecordTypeEmail, ctx.ClientIP())
resp, err := uc.userService.UserChangeEmailSendCode(ctx, req)
if err != nil {
if resp != nil {
resp.ErrorMsg = translator.GlobalTrans.Tr(handler.GetLang(ctx), resp.ErrorMsg)
}
handler.HandleResponse(ctx, err, resp)
return
}

View File

@ -10,6 +10,7 @@ import (
"strings"
"github.com/Chain-Zhang/pinyin"
"github.com/answerdev/answer/internal/base/handler"
"github.com/answerdev/answer/internal/base/reason"
"github.com/answerdev/answer/internal/base/translator"
"github.com/answerdev/answer/internal/base/validator"
@ -486,7 +487,7 @@ func (us *UserService) encryptPassword(ctx context.Context, Pass string) (string
// UserChangeEmailSendCode user change email verification
func (us *UserService) UserChangeEmailSendCode(ctx context.Context, req *schema.UserChangeEmailSendCodeReq) (
resp *validator.FormErrorField, err error) {
resp []*validator.FormErrorField, err error) {
userInfo, exist, err := us.userRepo.GetByUserID(ctx, req.UserID)
if err != nil {
return nil, err
@ -500,10 +501,10 @@ func (us *UserService) UserChangeEmailSendCode(ctx context.Context, req *schema.
return nil, err
}
if exist {
resp = &validator.FormErrorField{
resp = append([]*validator.FormErrorField{}, &validator.FormErrorField{
ErrorField: "e_mail",
ErrorMsg: reason.EmailDuplicate,
}
ErrorMsg: translator.GlobalTrans.Tr(handler.GetLangByCtx(ctx), reason.EmailDuplicate),
})
return resp, errors.BadRequest(reason.EmailDuplicate)
}