update user captcha

This commit is contained in:
aichy126 2022-12-18 00:14:50 +08:00
parent c63fcf1dc9
commit 1c374b842b
7 changed files with 162 additions and 2 deletions

View File

@ -4717,6 +4717,41 @@ const docTemplate = `{
}
}
},
"/answer/api/v1/user/register/captcha": {
"get": {
"description": "UserRegisterCaptcha",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"User"
],
"summary": "UserRegisterCaptcha",
"responses": {
"200": {
"description": "OK",
"schema": {
"allOf": [
{
"$ref": "#/definitions/handler.RespBody"
},
{
"type": "object",
"properties": {
"data": {
"$ref": "#/definitions/schema.GetUserResp"
}
}
}
]
}
}
}
}
},
"/answer/api/v1/user/register/email": {
"post": {
"description": "UserRegisterByEmail",
@ -7712,6 +7747,14 @@ const docTemplate = `{
"pass"
],
"properties": {
"captcha_code": {
"description": "captcha_code",
"type": "string"
},
"captcha_id": {
"description": "captcha_id",
"type": "string"
},
"e_mail": {
"description": "email",
"type": "string",

View File

@ -4705,6 +4705,41 @@
}
}
},
"/answer/api/v1/user/register/captcha": {
"get": {
"description": "UserRegisterCaptcha",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"User"
],
"summary": "UserRegisterCaptcha",
"responses": {
"200": {
"description": "OK",
"schema": {
"allOf": [
{
"$ref": "#/definitions/handler.RespBody"
},
{
"type": "object",
"properties": {
"data": {
"$ref": "#/definitions/schema.GetUserResp"
}
}
}
]
}
}
}
}
},
"/answer/api/v1/user/register/email": {
"post": {
"description": "UserRegisterByEmail",
@ -7700,6 +7735,14 @@
"pass"
],
"properties": {
"captcha_code": {
"description": "captcha_code",
"type": "string"
},
"captcha_id": {
"description": "captcha_id",
"type": "string"
},
"e_mail": {
"description": "email",
"type": "string",

View File

@ -1815,6 +1815,12 @@ definitions:
type: object
schema.UserRegisterReq:
properties:
captcha_code:
description: captcha_code
type: string
captcha_id:
description: captcha_id
type: string
e_mail:
description: email
maxLength: 500
@ -4729,6 +4735,26 @@ paths:
summary: get user ranking
tags:
- User
/answer/api/v1/user/register/captcha:
get:
consumes:
- application/json
description: UserRegisterCaptcha
produces:
- application/json
responses:
"200":
description: OK
schema:
allOf:
- $ref: '#/definitions/handler.RespBody'
- properties:
data:
$ref: '#/definitions/schema.GetUserResp'
type: object
summary: UserRegisterCaptcha
tags:
- User
/answer/api/v1/user/register/email:
post:
consumes:

View File

@ -227,6 +227,15 @@ func (uc *UserController) UserRegisterByEmail(ctx *gin.Context) {
return
}
req.IP = ctx.ClientIP()
captchaPass := uc.actionService.UserRegisterVerifyCaptcha(ctx, req.CaptchaID, req.CaptchaCode)
if !captchaPass {
errFields := append([]*validator.FormErrorField{}, &validator.FormErrorField{
ErrorField: "captcha_code",
ErrorMsg: translator.GlobalTrans.Tr(handler.GetLang(ctx), reason.CaptchaVerificationFailed),
})
handler.HandleResponse(ctx, errors.BadRequest(reason.CaptchaVerificationFailed), errFields)
return
}
resp, err := uc.userService.UserRegisterByEmail(ctx, req)
handler.HandleResponse(ctx, err, resp)
@ -405,6 +414,19 @@ func (uc *UserController) ActionRecord(ctx *gin.Context) {
handler.HandleResponse(ctx, err, resp)
}
// UserRegisterCaptcha godoc
// @Summary UserRegisterCaptcha
// @Description UserRegisterCaptcha
// @Tags User
// @Accept json
// @Produce json
// @Success 200 {object} handler.RespBody{data=schema.GetUserResp}
// @Router /answer/api/v1/user/register/captcha [get]
func (uc *UserController) UserRegisterCaptcha(ctx *gin.Context) {
resp, err := uc.actionService.UserRegisterCaptcha(ctx)
handler.HandleResponse(ctx, err, resp)
}
// UserNoticeSet godoc
// @Summary UserNoticeSet
// @Description UserNoticeSet

View File

@ -99,6 +99,7 @@ func (a *AnswerAPIRouter) RegisterMustUnAuthAnswerAPIRouter(r *gin.RouterGroup)
// user
r.POST("/user/login/email", a.userController.UserEmailLogin)
r.POST("/user/register/email", a.userController.UserRegisterByEmail)
r.GET("/user/register/captcha", a.userController.UserRegisterCaptcha)
r.POST("/user/email/verification", a.userController.UserVerifyEmail)
r.PUT("/user/email", a.userController.UserChangeEmailVerify)
r.GET("/user/action/record", a.userController.ActionRecord)

View File

@ -232,8 +232,10 @@ type UserRegisterReq struct {
// email
Email string `validate:"required,email,gt=0,lte=500" json:"e_mail" `
// password
Pass string `validate:"required,gte=8,lte=32" json:"pass"`
IP string `json:"-" `
Pass string `validate:"required,gte=8,lte=32" json:"pass"`
IP string `json:"-" `
CaptchaID string `json:"captcha_id"` // captcha_id
CaptchaCode string `json:"captcha_code"` // captcha_code
}
func (u *UserRegisterReq) Check() (errFields []*validator.FormErrorField, err error) {

View File

@ -48,6 +48,26 @@ func (cs *CaptchaService) ActionRecord(ctx context.Context, req *schema.ActionRe
return
}
func (cs *CaptchaService) UserRegisterCaptcha(ctx context.Context) (resp *schema.ActionRecordResp, err error) {
resp = &schema.ActionRecordResp{}
resp.CaptchaID, resp.CaptchaImg, err = cs.GenerateCaptcha(ctx)
resp.Verify = true
return
}
func (cs *CaptchaService) UserRegisterVerifyCaptcha(
ctx context.Context, id string, VerifyValue string,
) bool {
if id == "" || VerifyValue == "" {
return false
}
pass, err := cs.VerifyCaptcha(ctx, id, VerifyValue)
if err != nil {
return false
}
return pass
}
// ActionRecordVerifyCaptcha
// Verify that you need to enter a CAPTCHA, and that the CAPTCHA is correct
func (cs *CaptchaService) ActionRecordVerifyCaptcha(
@ -58,6 +78,9 @@ func (cs *CaptchaService) ActionRecordVerifyCaptcha(
return true
}
if num >= 3 {
if id == "" || VerifyValue == "" {
return false
}
pass, err := cs.VerifyCaptcha(ctx, id, VerifyValue)
if err != nil {
return false