Skip to content

A Q&A platform software for teams at any scales. Whether it's a community forum, help center, or knowledge management platform, you can always count on Apache Answer.

update user captcha

aichy1261c374b842bd5
diff --git a/docs/docs.go b/docs/docs.go
index e0fab59c..b36ca65c 100644
--- a/docs/docs.go
+++ b/docs/docs.go
@@ -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",
diff --git a/docs/swagger.json b/docs/swagger.json
index c117ec39..b7c418e6 100644
--- a/docs/swagger.json
+++ b/docs/swagger.json
@@ -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",
diff --git a/docs/swagger.yaml b/docs/swagger.yaml
index e3c68553..79d2ed30 100644
--- a/docs/swagger.yaml
+++ b/docs/swagger.yaml
@@ -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:
diff --git a/internal/controller/user_controller.go b/internal/controller/user_controller.go
index 5ab30a52..e418acac 100644
--- a/internal/controller/user_controller.go
+++ b/internal/controller/user_controller.go
@@ -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
diff --git a/internal/router/answer_api_router.go b/internal/router/answer_api_router.go
index 3fb10bcc..950f6e2e 100644
--- a/internal/router/answer_api_router.go
+++ b/internal/router/answer_api_router.go
@@ -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)
diff --git a/internal/schema/user_schema.go b/internal/schema/user_schema.go
index 492eee24..520cb2a8 100644
--- a/internal/schema/user_schema.go
+++ b/internal/schema/user_schema.go
@@ -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) {
diff --git a/internal/service/action/captcha_service.go b/internal/service/action/captcha_service.go
index 9d79c0fe..a6ee8e04 100644
--- a/internal/service/action/captcha_service.go
+++ b/internal/service/action/captcha_service.go
@@ -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