首次提交:初始化项目代码
This commit is contained in:
@@ -0,0 +1,62 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"cloudnest/internal/application/captcha"
|
||||
"cloudnest/internal/application/email"
|
||||
"cloudnest/internal/pkg/response"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// EmailHandler handles HTTP requests for email verification code operations.
|
||||
// EmailHandler 处理邮箱验证码操作的 HTTP 请求。
|
||||
type EmailHandler struct {
|
||||
emailService *email.EmailService
|
||||
captchaService *captcha.CaptchaService
|
||||
}
|
||||
|
||||
// NewEmailHandler creates a new EmailHandler.
|
||||
// NewEmailHandler 创建一个新的 EmailHandler。
|
||||
func NewEmailHandler(emailService *email.EmailService, captchaService *captcha.CaptchaService) *EmailHandler {
|
||||
return &EmailHandler{
|
||||
emailService: emailService,
|
||||
captchaService: captchaService,
|
||||
}
|
||||
}
|
||||
|
||||
// SendEmailCodeRequest represents the request body for sending email verification code.
|
||||
// SendEmailCodeRequest 表示发送邮箱验证码的请求体。
|
||||
type SendEmailCodeRequest struct {
|
||||
Email string `json:"email" binding:"required,email"`
|
||||
CaptchaID string `json:"captcha_id" binding:"required"`
|
||||
CaptchaAnswer string `json:"captcha_answer" binding:"required"`
|
||||
}
|
||||
|
||||
// SendEmailCode handles POST /api/v1/auth/email-code.
|
||||
// SendEmailCode 处理 POST /api/v1/auth/email-code。
|
||||
//
|
||||
// It first verifies the captcha, then sends the email verification code.
|
||||
// 先验证图形验证码,通过后才发送邮箱验证码。
|
||||
func (h *EmailHandler) SendEmailCode(c *gin.Context) {
|
||||
var req SendEmailCodeRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
response.BadRequest(c, "参数错误")
|
||||
return
|
||||
}
|
||||
|
||||
if !h.captchaService.Verify(req.CaptchaID, req.CaptchaAnswer) {
|
||||
response.Error(c, 400, "图形验证码错误")
|
||||
return
|
||||
}
|
||||
|
||||
_, err := h.emailService.SendVerifyCode(req.Email)
|
||||
if err != nil {
|
||||
if err.Error() == "邮件服务未配置" {
|
||||
response.Error(c, 503, "邮件服务未配置")
|
||||
return
|
||||
}
|
||||
response.InternalError(c, "邮件发送失败")
|
||||
return
|
||||
}
|
||||
|
||||
response.SuccessMsg(c, "验证码已发送")
|
||||
}
|
||||
Reference in New Issue
Block a user