feat: add email verify code endpoint and require code on register

- POST /v1/user/email/code sends a 6-digit verify code via email (rate-limited, 10min TTL)
- RegisterByEmail now validates verify_code before creating the account
- Added email code cache helpers mirroring SMS pattern
- Added error codes 1007 (email code error) and 1008 (send limit)

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-03-27 09:50:23 +08:00
parent 135a8151e4
commit f594a3e9fb
7 changed files with 202 additions and 14 deletions

View File

@@ -43,6 +43,7 @@ func SetupRouter(engine *gin.RouterGroup) {
userRouter := v1.Group("/user")
{
userRouter.POST("/sms", userEndpoint.SendVerificationCode)
userRouter.POST("/email/code", userEndpoint.SendEmailVerifyCode)
userRouter.POST("/register", userEndpoint.RegisterByEmail)
userRouter.POST("/login", userEndpoint.LoginByEmail)
userRouter.GET("/oauth/google/url", userEndpoint.GetGoogleOAuthUrl)

View File

@@ -106,6 +106,26 @@ func (h *UserEndpoint) GetUserInfo(ctx *gin.Context) {
}))
}
func (h *UserEndpoint) SendEmailVerifyCode(ctx *gin.Context) {
req := model.EmailVerifyCodeRequest{}
if err := ctx.ShouldBindJSON(&req); err != nil {
ctx.JSON(http.StatusOK, common.ErrorResponse(ctx, common.CodeParamError, common.CodeParamErrorMsg))
return
}
if err := h.userService.SendEmailVerifyCode(ctx, req.Email); err != nil {
if bizErr, ok := err.(*common.BusinessError); ok {
ctx.JSON(http.StatusOK, common.ErrorResponse(ctx, int(bizErr.Code), bizErr.Message))
return
}
log.Error(ctx, "func", "SendEmailVerifyCode", "msg", "发送邮件验证码失败", "error", err)
ctx.JSON(http.StatusOK, common.ErrorResponse(ctx, common.CodeSystemError, common.CodeSystemErrorMsg))
return
}
ctx.JSON(http.StatusOK, common.SuccessResponse(ctx, model.EmailVerifyCodeResponse{}))
}
func (h *UserEndpoint) RegisterByEmail(ctx *gin.Context) {
req := model.EmailRegisterRequest{}
if err := ctx.ShouldBindJSON(&req); err != nil {
@@ -113,7 +133,7 @@ func (h *UserEndpoint) RegisterByEmail(ctx *gin.Context) {
return
}
uid, err := h.userService.RegisterByEmail(ctx, req.Email, req.Password)
uid, err := h.userService.RegisterByEmail(ctx, req.Email, req.Password, req.VerifyCode)
if err != nil {
if bizErr, ok := err.(*common.BusinessError); ok {
ctx.JSON(http.StatusOK, common.ErrorResponse(ctx, int(bizErr.Code), bizErr.Message))