mirror of https://gitee.com/answerdev/answer.git
feat: change the original single form error return to multiple form error return
This commit is contained in:
parent
83ad1a185a
commit
8084870096
|
@ -23,6 +23,8 @@ const (
|
||||||
DisallowFollow = "error.object.disallow_follow"
|
DisallowFollow = "error.object.disallow_follow"
|
||||||
DisallowVoteYourSelf = "error.object.disallow_vote_your_self"
|
DisallowVoteYourSelf = "error.object.disallow_vote_your_self"
|
||||||
CaptchaVerificationFailed = "error.object.captcha_verification_failed"
|
CaptchaVerificationFailed = "error.object.captcha_verification_failed"
|
||||||
|
OldPasswordVerificationFailed = "error.object.old_password_verification_failed"
|
||||||
|
NewPasswordSameAsPreviousSetting = "error.object.new_password_same_as_previous_setting"
|
||||||
UserNotFound = "error.user.not_found"
|
UserNotFound = "error.user.not_found"
|
||||||
UsernameInvalid = "error.user.username_invalid"
|
UsernameInvalid = "error.user.username_invalid"
|
||||||
UsernameDuplicate = "error.user.username_duplicate"
|
UsernameDuplicate = "error.user.username_duplicate"
|
||||||
|
|
|
@ -9,6 +9,7 @@ import (
|
||||||
"github.com/answerdev/answer/internal/base/middleware"
|
"github.com/answerdev/answer/internal/base/middleware"
|
||||||
"github.com/answerdev/answer/internal/base/reason"
|
"github.com/answerdev/answer/internal/base/reason"
|
||||||
"github.com/answerdev/answer/internal/base/translator"
|
"github.com/answerdev/answer/internal/base/translator"
|
||||||
|
"github.com/answerdev/answer/internal/base/validator"
|
||||||
"github.com/answerdev/answer/internal/schema"
|
"github.com/answerdev/answer/internal/schema"
|
||||||
"github.com/answerdev/answer/internal/service"
|
"github.com/answerdev/answer/internal/service"
|
||||||
"github.com/answerdev/answer/internal/service/action"
|
"github.com/answerdev/answer/internal/service/action"
|
||||||
|
@ -106,24 +107,22 @@ func (uc *UserController) UserEmailLogin(ctx *gin.Context) {
|
||||||
|
|
||||||
captchaPass := uc.actionService.ActionRecordVerifyCaptcha(ctx, schema.ActionRecordTypeLogin, ctx.ClientIP(), req.CaptchaID, req.CaptchaCode)
|
captchaPass := uc.actionService.ActionRecordVerifyCaptcha(ctx, schema.ActionRecordTypeLogin, ctx.ClientIP(), req.CaptchaID, req.CaptchaCode)
|
||||||
if !captchaPass {
|
if !captchaPass {
|
||||||
resp := schema.UserVerifyEmailErrorResponse{
|
errFields := append([]*validator.FormErrorField{}, &validator.FormErrorField{
|
||||||
Key: "captcha_code",
|
ErrorField: "captcha_code",
|
||||||
Value: "error.object.verification_failed",
|
ErrorMsg: translator.GlobalTrans.Tr(handler.GetLang(ctx), reason.CaptchaVerificationFailed),
|
||||||
}
|
})
|
||||||
resp.Value = translator.GlobalTrans.Tr(handler.GetLang(ctx), resp.Value)
|
handler.HandleResponse(ctx, errors.BadRequest(reason.CaptchaVerificationFailed), errFields)
|
||||||
handler.HandleResponse(ctx, errors.BadRequest(reason.CaptchaVerificationFailed), resp)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
resp, err := uc.userService.EmailLogin(ctx, req)
|
resp, err := uc.userService.EmailLogin(ctx, req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
_, _ = uc.actionService.ActionRecordAdd(ctx, schema.ActionRecordTypeLogin, ctx.ClientIP())
|
_, _ = uc.actionService.ActionRecordAdd(ctx, schema.ActionRecordTypeLogin, ctx.ClientIP())
|
||||||
resp := schema.UserVerifyEmailErrorResponse{
|
errFields := append([]*validator.FormErrorField{}, &validator.FormErrorField{
|
||||||
Key: "e_mail",
|
ErrorField: "e_mail",
|
||||||
Value: reason.EmailOrPasswordWrong,
|
ErrorMsg: translator.GlobalTrans.Tr(handler.GetLang(ctx), reason.EmailOrPasswordWrong),
|
||||||
}
|
})
|
||||||
resp.Value = translator.GlobalTrans.Tr(handler.GetLang(ctx), resp.Value)
|
handler.HandleResponse(ctx, errors.BadRequest(reason.EmailOrPasswordWrong), errFields)
|
||||||
handler.HandleResponse(ctx, errors.BadRequest(reason.EmailOrPasswordWrong), resp)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
uc.actionService.ActionRecordDel(ctx, schema.ActionRecordTypeLogin, ctx.ClientIP())
|
uc.actionService.ActionRecordDel(ctx, schema.ActionRecordTypeLogin, ctx.ClientIP())
|
||||||
|
@ -146,12 +145,11 @@ func (uc *UserController) RetrievePassWord(ctx *gin.Context) {
|
||||||
}
|
}
|
||||||
captchaPass := uc.actionService.ActionRecordVerifyCaptcha(ctx, schema.ActionRecordTypeFindPass, ctx.ClientIP(), req.CaptchaID, req.CaptchaCode)
|
captchaPass := uc.actionService.ActionRecordVerifyCaptcha(ctx, schema.ActionRecordTypeFindPass, ctx.ClientIP(), req.CaptchaID, req.CaptchaCode)
|
||||||
if !captchaPass {
|
if !captchaPass {
|
||||||
resp := schema.UserVerifyEmailErrorResponse{
|
errFields := append([]*validator.FormErrorField{}, &validator.FormErrorField{
|
||||||
Key: "captcha_code",
|
ErrorField: "captcha_code",
|
||||||
Value: "error.object.verification_failed",
|
ErrorMsg: translator.GlobalTrans.Tr(handler.GetLang(ctx), reason.CaptchaVerificationFailed),
|
||||||
}
|
})
|
||||||
resp.Value = translator.GlobalTrans.Tr(handler.GetLang(ctx), resp.Value)
|
handler.HandleResponse(ctx, errors.BadRequest(reason.CaptchaVerificationFailed), errFields)
|
||||||
handler.HandleResponse(ctx, errors.BadRequest(reason.CaptchaVerificationFailed), resp)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
_, _ = uc.actionService.ActionRecordAdd(ctx, schema.ActionRecordTypeFindPass, ctx.ClientIP())
|
_, _ = uc.actionService.ActionRecordAdd(ctx, schema.ActionRecordTypeFindPass, ctx.ClientIP())
|
||||||
|
@ -277,12 +275,11 @@ func (uc *UserController) UserVerifyEmailSend(ctx *gin.Context) {
|
||||||
captchaPass := uc.actionService.ActionRecordVerifyCaptcha(ctx, schema.ActionRecordTypeEmail, ctx.ClientIP(),
|
captchaPass := uc.actionService.ActionRecordVerifyCaptcha(ctx, schema.ActionRecordTypeEmail, ctx.ClientIP(),
|
||||||
req.CaptchaID, req.CaptchaCode)
|
req.CaptchaID, req.CaptchaCode)
|
||||||
if !captchaPass {
|
if !captchaPass {
|
||||||
resp := schema.UserVerifyEmailErrorResponse{
|
errFields := append([]*validator.FormErrorField{}, &validator.FormErrorField{
|
||||||
Key: "captcha_code",
|
ErrorField: "captcha_code",
|
||||||
Value: "error.object.verification_failed",
|
ErrorMsg: translator.GlobalTrans.Tr(handler.GetLang(ctx), reason.CaptchaVerificationFailed),
|
||||||
}
|
})
|
||||||
resp.Value = translator.GlobalTrans.Tr(handler.GetLang(ctx), resp.Value)
|
handler.HandleResponse(ctx, errors.BadRequest(reason.CaptchaVerificationFailed), errFields)
|
||||||
handler.HandleResponse(ctx, errors.BadRequest(reason.CaptchaVerificationFailed), resp)
|
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -314,22 +311,19 @@ func (uc *UserController) UserModifyPassWord(ctx *gin.Context) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if !oldPassVerification {
|
if !oldPassVerification {
|
||||||
resp := schema.UserVerifyEmailErrorResponse{
|
errFields := append([]*validator.FormErrorField{}, &validator.FormErrorField{
|
||||||
Key: "old_pass",
|
ErrorField: "old_pass",
|
||||||
Value: "error.object.old_password_verification_failed",
|
ErrorMsg: translator.GlobalTrans.Tr(handler.GetLang(ctx), reason.OldPasswordVerificationFailed),
|
||||||
}
|
})
|
||||||
resp.Value = translator.GlobalTrans.Tr(handler.GetLang(ctx), resp.Value)
|
handler.HandleResponse(ctx, errors.BadRequest(reason.OldPasswordVerificationFailed), errFields)
|
||||||
handler.HandleResponse(ctx, errors.BadRequest(reason.CaptchaVerificationFailed), resp)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if req.OldPass == req.Pass {
|
if req.OldPass == req.Pass {
|
||||||
|
errFields := append([]*validator.FormErrorField{}, &validator.FormErrorField{
|
||||||
resp := schema.UserVerifyEmailErrorResponse{
|
ErrorField: "pass",
|
||||||
Key: "pass",
|
ErrorMsg: translator.GlobalTrans.Tr(handler.GetLang(ctx), reason.NewPasswordSameAsPreviousSetting),
|
||||||
Value: "error.object.new_password_same_as_previous_setting",
|
})
|
||||||
}
|
handler.HandleResponse(ctx, errors.BadRequest(reason.NewPasswordSameAsPreviousSetting), errFields)
|
||||||
resp.Value = translator.GlobalTrans.Tr(handler.GetLang(ctx), resp.Value)
|
|
||||||
handler.HandleResponse(ctx, errors.BadRequest(reason.CaptchaVerificationFailed), resp)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
err = uc.userService.UserModifyPassword(ctx, req)
|
err = uc.userService.UserModifyPassword(ctx, req)
|
||||||
|
@ -502,19 +496,18 @@ func (uc *UserController) UserChangeEmailSendCode(ctx *gin.Context) {
|
||||||
|
|
||||||
captchaPass := uc.actionService.ActionRecordVerifyCaptcha(ctx, schema.ActionRecordTypeEmail, ctx.ClientIP(), req.CaptchaID, req.CaptchaCode)
|
captchaPass := uc.actionService.ActionRecordVerifyCaptcha(ctx, schema.ActionRecordTypeEmail, ctx.ClientIP(), req.CaptchaID, req.CaptchaCode)
|
||||||
if !captchaPass {
|
if !captchaPass {
|
||||||
resp := schema.UserVerifyEmailErrorResponse{
|
errFields := append([]*validator.FormErrorField{}, &validator.FormErrorField{
|
||||||
Key: "captcha_code",
|
ErrorField: "captcha_code",
|
||||||
Value: "error.object.verification_failed",
|
ErrorMsg: translator.GlobalTrans.Tr(handler.GetLang(ctx), reason.CaptchaVerificationFailed),
|
||||||
}
|
})
|
||||||
resp.Value = translator.GlobalTrans.Tr(handler.GetLang(ctx), resp.Value)
|
handler.HandleResponse(ctx, errors.BadRequest(reason.CaptchaVerificationFailed), errFields)
|
||||||
handler.HandleResponse(ctx, errors.BadRequest(reason.CaptchaVerificationFailed), resp)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
_, _ = uc.actionService.ActionRecordAdd(ctx, schema.ActionRecordTypeEmail, ctx.ClientIP())
|
_, _ = uc.actionService.ActionRecordAdd(ctx, schema.ActionRecordTypeEmail, ctx.ClientIP())
|
||||||
resp, err := uc.userService.UserChangeEmailSendCode(ctx, req)
|
resp, err := uc.userService.UserChangeEmailSendCode(ctx, req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if resp != nil {
|
if resp != nil {
|
||||||
resp.Value = translator.GlobalTrans.Tr(handler.GetLang(ctx), resp.Value)
|
resp.ErrorMsg = translator.GlobalTrans.Tr(handler.GetLang(ctx), resp.ErrorMsg)
|
||||||
}
|
}
|
||||||
handler.HandleResponse(ctx, err, resp)
|
handler.HandleResponse(ctx, err, resp)
|
||||||
return
|
return
|
||||||
|
|
|
@ -417,8 +417,3 @@ type UserVerifyEmailSendReq struct {
|
||||||
CaptchaID string `validate:"omitempty,gt=0,lte=500" json:"captcha_id"`
|
CaptchaID string `validate:"omitempty,gt=0,lte=500" json:"captcha_id"`
|
||||||
CaptchaCode string `validate:"omitempty,gt=0,lte=500" json:"captcha_code"`
|
CaptchaCode string `validate:"omitempty,gt=0,lte=500" json:"captcha_code"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type UserVerifyEmailErrorResponse struct {
|
|
||||||
Key string `json:"key"`
|
|
||||||
Value string `json:"value"`
|
|
||||||
}
|
|
||||||
|
|
|
@ -12,6 +12,7 @@ import (
|
||||||
"github.com/Chain-Zhang/pinyin"
|
"github.com/Chain-Zhang/pinyin"
|
||||||
"github.com/answerdev/answer/internal/base/reason"
|
"github.com/answerdev/answer/internal/base/reason"
|
||||||
"github.com/answerdev/answer/internal/base/translator"
|
"github.com/answerdev/answer/internal/base/translator"
|
||||||
|
"github.com/answerdev/answer/internal/base/validator"
|
||||||
"github.com/answerdev/answer/internal/entity"
|
"github.com/answerdev/answer/internal/entity"
|
||||||
"github.com/answerdev/answer/internal/schema"
|
"github.com/answerdev/answer/internal/schema"
|
||||||
"github.com/answerdev/answer/internal/service/activity"
|
"github.com/answerdev/answer/internal/service/activity"
|
||||||
|
@ -481,7 +482,7 @@ func (us *UserService) encryptPassword(ctx context.Context, Pass string) (string
|
||||||
|
|
||||||
// UserChangeEmailSendCode user change email verification
|
// UserChangeEmailSendCode user change email verification
|
||||||
func (us *UserService) UserChangeEmailSendCode(ctx context.Context, req *schema.UserChangeEmailSendCodeReq) (
|
func (us *UserService) UserChangeEmailSendCode(ctx context.Context, req *schema.UserChangeEmailSendCodeReq) (
|
||||||
resp *schema.UserVerifyEmailErrorResponse, err error) {
|
resp *validator.FormErrorField, err error) {
|
||||||
userInfo, exist, err := us.userRepo.GetByUserID(ctx, req.UserID)
|
userInfo, exist, err := us.userRepo.GetByUserID(ctx, req.UserID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
@ -495,9 +496,9 @@ func (us *UserService) UserChangeEmailSendCode(ctx context.Context, req *schema.
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if exist {
|
if exist {
|
||||||
resp = &schema.UserVerifyEmailErrorResponse{
|
resp = &validator.FormErrorField{
|
||||||
Key: "e_mail",
|
ErrorField: "e_mail",
|
||||||
Value: reason.EmailDuplicate,
|
ErrorMsg: reason.EmailDuplicate,
|
||||||
}
|
}
|
||||||
return resp, errors.BadRequest(reason.EmailDuplicate)
|
return resp, errors.BadRequest(reason.EmailDuplicate)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue